I want to set precision to 2 with the results. Does that mean that I have to put << setprecision (2) << after every weight on each planet or is there something easier I can do?

``````#include<iostream>
#include<cmath>

using namespace std;

int CheckWeight(int weight, string Planet);

int main()
{
int weight,res;
string planetName;

do{
cout << "Please enter your weight of your object to the nearest whole number." <<endl;
cin >> weight;}
while(cin.good()==false);
cout << " " << endl;
do{
cout << "Please enter a planet name from the following list." << endl;
cout << "Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune" << endl;
cin >> planetName;
cout << " " << endl;
// Call function to check weight
res = CheckWeight(weight, planetName);
}while(res==1);
system("PAUSE");
return 0;
}// end main()

// Function for calculating weight

int CheckWeight(int weight, string Planet)
{
int Result=0;
if(Planet== "Mercury"){
cout << "Your weight on Mercury is " << (weight*0.4155)<< endl;
}
else if(Planet== "Venus"){
cout << "Your weight on Venus is " << (weight*0.8975)<< endl;
}
else if(Planet== "Earth"){
cout << "Your weight on Earth is " << (weight*1.0)<< endl;
}
else if(Planet== "Mars"){
cout << "Your weight on Mars is " << (weight*0.3507)<< endl;
}
else if(Planet== "Jupiter"){
cout << "Your weight on Jupiter is " << (weight*2.5374)<< endl;
}
else if(Planet== "Saturn"){
cout << "Your weight on Saturn is " << (weight*1.0677)<< endl;
}
else if(Planet== "Uranus"){
cout << "Your weight on Uranus is " << (weight*0.8947)<< endl;
}
else if(Planet== "Neptune"){
cout << "Your weight on Neptune is " << (weight*1.1794)<< endl;
}
else{
cout << "You entered a wrong planet name. Please try again " << endl;
cout << "Please try again " << endl;
Result=1;
}
return Result;``````
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by determine

The precision function could be useful here:

``````cout.precision(2);
cout<<2.234134<<endl;
cout<<2.343144<<endl;``````

Outputs:
2.2
2.3

In other words, using cout.precision(X) sets the precision of cout to X and it stays that way until cout.precision(Y) is called, then the precision is set to Y.

Alse try to avoid using system("pause"), try using cin.get() instead.

Edited by Labdabeta: n/a

The precision function could be useful here:

``````cout.precision(2);
cout<<2.234134<<endl;
cout<<2.343144<<endl;``````

Outputs:
2.2
2.3

In other words, using cout.precision(X) sets the precision of cout to X and it stays that way until cout.precision(Y) is called, then the precision is set to Y.

Alse try to avoid using system("pause"), try using cin.get() instead.

thanks alot. Very helpful.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.