Converting string to double keeps rounding to the whole number - c++

I am trying to convert a string decimal number into a double, however when I use the atof() function, my number ends up rounding to the whole number.
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
string num = "135427.7000";
double r = atof(num.c_str());
cout << r << endl;
}
The output is:
135428
I want:
135427.7

cout does that, not atof().
More precisely, operator<<, which inserts the formated data into the std::ostream.
You can use std::setprecision() from the <iomanip> standard library to print the decimals:
cout << setprecision(7) << r << endl;
or
cout << fixed << setprecision(1) << r << endl;
If you want to print the whole 135427.7000:
cout << fixed << setprecision(4) << r << endl;

Related

Print a decimal number in a particular format using setfill() in c++

I am trying to print a decimal no. in following format : "#####+3.01"
Case: There is a decimal no (let say 3.01 in this case). I have to print it with its sign +/- preceding with y no. of #, with some fix total width. (let say x = 10 in this case).
I tried do something like this :
double no = 3.01;
cout << setfill('#') << setw(10) ;
cout << setiosflags(ios::showpos);
cout << fixed << setprecision(2) << no << endl;
But i am getting followinfg output :
+#####3.01
Expected Output :
#####+3.01
Your code gave me correct result. I am using a Linux machine.
Just in case it is a OS dependent problem, try this code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double no = 3.01;
cout << setfill('#') << std::right<< setw(10) ;
cout << setiosflags(ios::showpos);
cout << fixed << setprecision(2) << no << endl;
}

Objectives: using the iomanip library to format screen output

Hello everyone this is my code and I just help I managed to correct the first 3 questions but the rest I am still getting errors.
Below is the all question :
Complete the provided main() program with statements to accomplish each of the following. In each case you must use the appropriate I/O stream manipulators to produce the appropriate output wherever possible.
Output first first as an integer value, followed by a space, then in
its written form.
Output second as a base ten value, followed by a space, then as a
hexadecimal
value, followed by a space, then as an octal value. Make sure the
appropriate base indicator prefix is shown in the output.
Output third.
Output fourth with four digits, with the sign shown at the left, and
the value right aligned. The decimal point must also appear.
Output fourth with four significant figures.
Output fifth with seven significant figures. (Note: use left alignment here)
Output fifth with three digits to the right of the decimal point.
Output third.
Output fourth with two digits to the right of the decimal point.
Output sixth with no decimal portion showing
Output fourth with eight digits to the right of the decimal point.
Output sixth with six digits.
Here is my code so far :
#include <iostream>
#include <iomanip>
using namespace std;
int
main0()
{
bool first;
int second;
long third;
float fourth;
float fifth;
double sixth;
cout << "Enter bool, int, long, float, float, and double values: ";
cin >> first >> second >> third >> fourth >> fifth >> sixth;
cout << endl;
cout << noboolalpha << first;
cout << " ";
cout << boolalpha << first << endl;
cout <<left << dec << showbase;
cout << second;
cout << " ";
cout << internal << hex << showbase;
cout << second;
cout << " ";
cout <<right << oct <<showbase;
cout << second << endl;
cout << third<< scientific<< endl;
cout <<left << setw(4)<<fixed<< fourth <<endl;
cout <<setprecision(4)<< fourth <<endl;
cout <<left<<setw(7)<< fifth << endl;
cout <<right<<setprecision(3)<< fifth;
cout <<third<<endl;
cout <<right<<setw(2)<<fourth<<endl;
cout << fixed<<sixth<< endl;
cout << right << fixed<<setprecision(8)<< fourth<< endl;
cout <<left<<showpoint <<setprecision(6)<<sixth;
// ***** Solution ends here ****
cin.get();
return 0;
}
I answered 4-6:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
long third = 123654123654123LL;
float fourth = 12335.67890;
std::ios initialState(nullptr);
initialState.copyfmt(std::cout);
// 4
cout << third << scientific<< endl;
// 5
cout << showpoint << fixed << setprecision(4) << right << showpos << fourth << endl;
cout.copyfmt(initialState);
// 6
cout << setprecision(4) << fourth << endl;
return 0;
}
Good luck with the rest.

restore original value after setprecision? [duplicate]

This question already has answers here:
Set back default floating point print precision in C++
(5 answers)
Closed 7 years ago.
I have search the web but couldn't find what I need.
Some people recommend using
streamsize ss = std::cout.precision();
but I couldn't get it to work.
How do I set a double value back to the original state after setprecision?
#include <iostream>
using namespace std;
int main()
{
double a;
cout << "enter a double value: ";
cin >> a;
cout << "your value in 3 decimals is " << setprecision(3) << fixed << a << endl;
cout << "your original value is " << a << endl;
return 0;
}
Obviously the code above will not return the original value of a.
My intended output is: if user enter 1.267432
your value in 3 decimals is 1.267
your original value is 1.267432
How do I set a double value back to the original state after
setprecision?
To do so, you have to get the precision before you use setprecision(). In your question you already mentioned it by the following line:
streamsize ss = std::cout.precision();
but I couldn't get it to work.
Here how you use it:
streamsize ss = std::cout.precision();
double a = 1.267432;
std::cout << "a = " << a << '\n';
std::cout.precision (3);
std::cout << "a becomes = " << a << '\n';
std::cout.precision (ss);
std::cout << "Original a= " << a << '\n';
And the output will be like:
a = 1.26743
a becomes = 1.27
Original a= 1.26743
Reference: setprecision.
Run live.
You can try like this:
#include <iomanip>
#include <iostream>
int main()
{
double a = 1.267432;
std::cout << std::fixed << std::showpoint;
std::cout << std::setprecision(3);
std::cout << a << endl;
return 0;
}

How to set the output precision to 2 decimal places in C++?

I want to globally set the ouptut precision to 2 decimal places.
I already tried to use iomanip and setprecision, however I keep getting output with 'e' in it.
This is my example code:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double pay=16.78;
double hours;
double total;
cout.precision(2);
cout << "Please enter how many hours you worked : " << endl;
cin >> hours;
total=hours*pay;
cout << "You earned: " << total << endl;
}
You can use something like this:
double pay = 393.2993;
std::cout << std::fixed << std::setprecision(2) << pay;
You will need to include iomanip for this to work.
#include <iomanip>
I'm not familiar with "cout.precision(2)". I would use std::setprecision(4) if I wanted my output to have 4 significant figures.
If your e is a positive value, you cannot ride of them because your value is too large. This code
std::cout << std::setprecision(3) << 3e45;
//output
3e+45
If it's a negative number, your precision is not enough. Like the following code
std::cout << std::setprecision(3) << 3e-45; //
//output
3e-45
A simple way will be to use std::fixed
std::cout << std::fixed << std::setprecision(3) << 3.63e-2;
//output
0.036
But the disavantage you have with the std::fixed is it shows the zero after the last none-zero number until it reach the setprecision qunatity you set previously.

c++ passing pointers to a function

I am trying to create a console app in C++ that prompts the user to enter a floating point number and then takes that number and separates out the integer part and the fraction part.
Example output would be:-
Please enter a floating point number:
800.589
The integer part is 800 and the fraction part is .589
My solution is shown below:
#include <iostream>
#include <cmath>
using namespace std;
void spliceAnyNumber (double anyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
fractionPart = fmod(anyNumber,1);
integerPart = anyNumber - fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " << *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}
int main()
{
cout << "Please enter a floating point number: ";
double anyNumber = 0;
cin >> anyNumber;
cout << endl;
spliceAnyNumber(anyNumber);
system("Pause");
return 0;
}
I wrote the program but I am also being asked to pass pointers to the function and manipulate the dereferenced values. I tried to do that below but I am getting a bunch of errors back from the compiler.
#include <iostream>
#include <cmath>
using namespace std;
void spliceAnyNumber (double *pAnyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
&fractionPart = fmod(&anyNumber,1);
&integerPart = &anyNumber - &fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " << *pFractionPart << "\n"; *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}
int main()
{
cout << "Please enter a floating point number: ";
double *pAnyNumber = &anyNumber;
cin >> *pAnyNumber;
cout << endl;
spliceAnyNumber(*pAnyNumber);
system("Pause");
return 0;
}
Where am I going wrong with adding in pointers? Version 1 works but version 2 does not.
I've notated this inline.
#include <iostream>
#include <cmath>
using namespace std;
void spliceAnyNumber (double *pAnyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
&fractionPart = fmod(&anyNumber,1); // <- you should dereference pAnyNumber instead, and assign to fractionPart (i.e. "fractionPart = fmod(*pAnyNymber, 1);
&integerPart = &anyNumber - &fractionPart; // <- similar as above
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " << *pFractionPart << "\n"; *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}
int main()
{
cout << "Please enter a floating point number: ";
double *pAnyNumber = &anyNumber; // <- you haven't declared an 'anyNumber' variable to take the address of
cin >> *pAnyNumber;
cout << endl;
spliceAnyNumber(*pAnyNumber);
system("Pause");
return 0;
}
The & operator takes the address of a variable, so typeof(&anyNumber) == double**. You want the * operator instead.
You should read double *pAnyNumber as "When I apply the * operator, I get a double". (You actually get an lvalue reference, but that doesn't roll off the tongue and will probably confuse you...)
Your main function is a mess; leave it the same as the original and change spliceAnyNumber(pAnyNumber); to spliceAnyNumber(&pAnyNumber);.
I assume when you wrote anyNumber you actually meant pAnyNumber. If you have a pointer
double* p;
You dereference by *p, not &p. The former gives you a double while the latter gives you a double**.
You have to declare anyNumber before you can dereference it:
double *pAnyNumber = &anyNumber; // references an undeclared variable
Just take the address when passing to the function. Before that, you can use normal variables - no need for pointers:
double anyNumber;
cin >> anyNumber;
cout << endl;
spliceAnyNumber(&anyNumber);
Additionally, you're using the wrong operator in your function. It should be like this:
*pFractionPart = fmod(*pAnyNumber,1);
*pIntegerPart = *pAnyNumber - fractionPart;
The other thing is invalid syntax: &variable = ... literally means "address of variable = ", which results in a double**.
So the only change you have to make is the function parameter, and accessing it. No need for all those pointers inside the function..