So I've seen the few other posts here and elsewhere regarding increasing decimal precision, but for some odd reason I cannot for the life of me get it to work. Here's what I've got:
//Euler's Method Approximation
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int n;
int i=1;
double h;
double x_n;
double y_n;
cout << "Enter n: " ;
cin >> n;
cout << endl << "Enter h: " ;
cin >> h;
cout << endl << "Enter x_0: ";
cin >> x_n;
cout << endl << "Enter y_0: ";
cin >> y_n;
cout << "n=" << i << " x=" << x_n << " y=" << y_n << endl;
++i;
while(i<n+1)
{
y_n = y_n + 3*x_n*h*y_n;
x_n = x_n + h;
cout << "n=" << i << " x=" << x_n << " y=" << setprecision(6) << y_n << endl;
++i;
}
return(0);
}
I apologize for the mess. Just threw this down under a time crunch, but now I've got more time to look at it. What could I be doing wrong? The mathematics definitely generates decimals to arbitrary precision depending on input, and all the variable types are as they should be. Even have the iomanip library in there. Any pointers? ( Still only prints out to the millionths )
Put std::fixed at the end of cout << "n=" << i << " x=" << x_n << " y=" << setprecision(6) << y_n << fixed << endl; as suggested by #40two in comments.
For ofstream you can use the precision method, http://www.cplusplus.com/reference/ios/ios_base/precision/
Related
I want to display my result aligned on the column by the decimal place.
I have tried to just put setw(7) and setw(6) in parts of the display but it seems to not change the output at all.
int main()
{
double x;
char more = 'y';
while(more=='y' || more=='Y')
{
cout << "\n\t\t\tInput x:";
cin >> x;
cout << "\n\n\t\t\t LibraryResult\tMyResult" << endl;
cout << setprecision(2) << fixed << "\n\t\tsin(" << x << ")\t"
<< setprecision(6) << sin(x) << "\t" << mySin(x) << endl;
cout << setprecision(2) << fixed << "\n\t\tcos(" << x << ")\t"
<< setprecision(6) << cos(x) << "\t" << myCos(x) << endl;
cout << setprecision(2) << fixed << "\n\t\texp(" << x << ")\t"
<< setprecision(6) << exp(x) << "\t" << myExp(x) << endl;
}
}
I want the resultants of the program to be aligned by decimal, so when you put in a number like 2 the decimals are all in the same column.
If you set a precision of 6, a width of 6 or 7 is simply not enough to contain the number. You have to either reduce the precision or increase the width.
Try playing around with the stream manipulators in the following snippet
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
int main()
{
const double period = 2 * 3.141592653589793;
const int steps = 20;
std::cout << std::string(44, '=') << '\n';
std::cout << std::setw(3) << "x" << std::setw(12) << "sin(x)"
<< std::setw(12) << "cos(x)" << std::setw(14) << "tan(x)" << '\n';
std::cout << std::string(44, '-') << '\n';
for(int i = 0; i <= steps; ++i)
{
double x = i * period / steps;
std::cout << std::setprecision(2) << std::fixed << x
<< std::setprecision(6) << std::setw(12) << std::sin(x)
<< std::setprecision(6) << std::setw(12) << std::cos(x)
<< std::setprecision(6)
<< std::setw(16) // <- Try to decrease it
<< std::scientific // <- Try to keep it as std::fixed
<< std::tan(x) << '\n';
}
std::cout << std::string(44, '-') << '\n';
}
You must use several manipulators (one is not enough)
You might want to try with the following combination, that right-aligns your numbers, with a fixed number of decimals.
std::cout.width(15);
std::cout << std::fixed << std::setprecision(6) << std::right << exp(x) << std::endl;
You can find information about manipulators here
I'm trying to create a simple calculator and i already encountered an issue when addition is being used. I created a function for addition and whenever i pass in two values i get a different answer. For Example when i add 4,5 i would expect to get 9 but the answer i get is 0029144C . Im still a beginner, so at first i wasn't sure if using type bool for the adding function would affect my result, but i changed it to type float and still getting the same result (in case anyone asks).
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void SimCalcMenu();
void additionSign();
bool makeSum(float num1, float num2);
int main() {
float firstNum, SecondNum;
char operationLetter;
SimCalcMenu();
cout << " Please Select an Operation You Would Like to Perform ";
cin >> operationLetter;
if (operationLetter == 'a' || operationLetter == 'A')
{
additionSign();
cout << " Enter the First Number : ";
cin >> firstNum;
cout << " Enter the Second Number: ";
cin >> SecondNum;
makeSum(firstNum, SecondNum);
cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum << endl;
}
else
{
cout << " Error ";
}
return 0;
}
void SimCalcMenu() {
cout << "------------------------------------------------------------------------------" << endl;
cout << " WELCOME TO SIM CALCULATOR " << endl;
cout << "------------------------------------------------------------------------------" << endl;
cout << endl;
cout << " Please Select an Operation : " << endl;
cout << " A.) Addition " << endl;
cout << " B.) Subtraction " << endl;
cout << " C.) Multiplication " << endl;
cout << " D.) Division " << endl;
cout << " E.) Roots ( Only Positive Number)" << endl;
cout << " F.) Power ( Only Positive Number " << endl;
cout << " G.) Percentage " << endl;
cout << " H.) Display functions execution " << endl;
cout << " I.) Quit " << endl;
cout << "------------------------------------------------------------------------------" << endl;
}
void additionSign() {
cout << "------------------------------------------------------------------------------" << endl;
cout << " ADDITION " << endl;
cout << "------------------------------------------------------------------------------" << endl;
}
bool makeSum(float num1, float num2) {
float totSum;
totSum = num1 + num2;
return totSum;
}
makeSum() should return float, because you are returning the sum of two floats.
You are not getting the right result because you are printing makeSum, which is the address of the function. You want to print the value of makeSum(firstNum, SecondNum).
this line
cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum << endl;
IS 'printing' 'makesum', makesum is a function so its printing the address of makesum
you need
cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum(firstNum, SecondNum) << endl;
now at least it will print the result of makesum. As other have pointerd out that function is wrong (it returns a bool).
should be
float makeSum(float num1, float num2) {
float totSum;
totSum = num1 + num2;
return totSum;
}
I've been working on this homework assignment for a while and am about ready to pull my hair out.
I need help rounding a float to the tenths place while still showing a 0 in the hundredths place and nothing I do seems to do that.
i.e. 2.47 = 2.50
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float MPH;
float seconds;
const float MPH2MPS = (1609.00 / 3600.00);
float a;
cout << " Acceleration calculator" << endl;
cout << "" << endl;
cout << "Please enter the velocity in miles per hour: ";
cin >> MPH;
cout << "" << endl;
cout << "Please enter the time in secounds: ";
cin >> seconds;
cout << "" << endl;
cout << "" << endl;
cout << "" << endl;
a = MPH2MPS * ( MPH / seconds );
cout << showpoint << fixed << setprecision(2);
cout << "The acceleration required by a vehicle to reach" << endl;
cout << "" << endl;
cout << "a velocity of " << MPH << " miles per hour in " << seconds << " seconds" << endl;
cout << "" << endl;
cout << "is " << setprecision(1) << a << " meters per second" << endl;
cout << "" << endl;
cout << "" << endl;
system("pause");
return 0;
Any ideas?
Reading #asterite's answer here:
cout << "is " << static_cast<double>(std::round(a * 10)) / 10 << " meters per second" << endl;
Note the round function only works if you #include <cmath>
Update:
Modified math.h with cmath, thanks to #user4581301
assignment at school asks me to find the present value using double, and void. i was able to write my code up to a certain degree but the result is not what i was expecting.. i ended up separating the present value into different section so at the end i'd multiply the amount given with the rest.. any tips on how to make the code actually work the way its supposed to?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double payment,year_term, interest;
double sum;
double Power;
double presentv;
double present;
cout << "Hello, how are you doing?" << endl;
cout << "Please insert a payment amount" << endl;
cin >> payment;
cout << " amount inserted: " << payment << endl;
cout << "Enter number of years" << endl;
cin >> year_term;
cout << " number of years: " << year_term << endl;
cout << "Enter interest rate" << endl;
cin >> interest;
cout << " the interest is: " << interest << "%" << endl;
Presentv = ((1 - (pow((1 + interest),year_term))))/interest;
cout << " the value: " << Presentv << endl;
presentva = payment * Presentv;
cout << " the present value is: " << presentva << endl;
}
I want to input data from txt file.
the file contains 2-d array [5][5]
how can i print out the any value i want?
i don't want to print out the whole 5*5 data
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double distance[5][5] ;
string line;
ifstream ratefile;
ratefile.open("a.txt");
ofstream file;
if (ratefile.is_open())
{
while (! ratefile.eof() )
{
getline (ratefile,line);
ratefile.getline(distance, 25, '*');
cout << "\nDistance [0][0]" << ": " << distance[0][0];
cout << "\nDistance [0][1]" << ": " << distance[0][1];
cout << "\nDistance [0][2]" << ": " << distance[0][2];
cout << "\nDistance [0][3]" << ": " << distance[0][3];
cout << "\nDistance [1][0]" << ": " << distance[1][0];
cout << "\nDistance [1][1]" << ": " << distance[1][1];
cout << "\nDistance [1][2]" << ": " << distance[1][2];
cout << "\nDistance [1][3]" << ": " << distance[1][3];
cout << endl;
cin.get();
return 0;
}
If you only want to output one value and the user should be able to choose a value, you can do something like this:
int x, y;
cin >> x;
cin >> y;
cout << "\nDistance [" << x << "][" << y << "]" << ": " << distance[x][y];
But you should check if the user enter valid numbers (0 <= x < 4 and 0 <= y < 4)
There is part of the code missing, but you are printing values you want. Simply remove the lines you don't want to print.
Of course you can also use variables:
int x = 2,y = 2;
cout << endl << "Distance [" << x << "][" << y << "] : " << distance[x][y];