Friday, July 31, 2009

Whats wrong with this c++ program?

When you run this program, you are asked to enter a amount. After entering the amount, it prints the amount you enterd. My problem is if I enter 1.00. The result come up as 1 The .00 does not come up. Anyone know if I have the code wrong or know a way to make this work.





Here is the program





#include%26lt;iostream%26gt;


using namespace std;


int main(void)


{


float purchase;


cout %26lt;%26lt; "Enter the amount purchase $";


cin%26gt;%26gt;purchase;


cout%26lt;%26lt;"the number is $"%26lt;%26lt;purchase%26lt;%26lt;endl;


system("pause");


return 0;


}

Whats wrong with this c++ program?
try using setprecision


cout %26lt;%26lt;"the number is $"%26lt;%26lt;setprecision(2)%26lt;%26lt;purchase%26lt;%26lt;endl;





You might also need:


cout.setf(ios::showpoint);
Reply:Try this:





#include%26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


using namespace std;


int main(void)


{


float purchase;


cout %26lt;%26lt; "Enter the amount purchase $";


cin%26gt;%26gt;purchase;


cout %26lt;%26lt; fixed %26lt;%26lt; showpoint %26lt;%26lt; setprecision(2);


cout%26lt;%26lt;"the number is $"%26lt;%26lt;purchase%26lt;%26lt;endl;


system("pause");


return 0;


}
Reply:That is because of data types. Try using


double purchase;


instead of


float purchase;


and possibly enforce this by saying, after taking input:


purchase=(double) purchase;


or


purchase = purchase / 1.00;


No comments:

Post a Comment