Boolen funcion trouble

I have to write a program that tests to see if the correct input is being given during prompts. If non integers or negative numbers are entered it's supposed to say that's wrong. Then, when an integer is entered, it's supposed to move on to the next question. But, my program quits after the first one is correctly entered. I'm in an intro C++ class and this is confusong me.
Any help, please?
Here's my code:

#include <iostream>
#include <cmath>
using namespace std;
const double G=6.673*pow(10.0,-11.0);
int main()
{
double m1=0.0,m2=0.0,D=0.0,r1=0.0,r2=0.0,F=0.0,inc=0.0;
//Input
bool done=false;
while (done==false){
cout<<"Enter the mass of planet #1 in kg: ";
cin>>m1;
if(cin.fail()==0){
done=true;}
else{
cout<<"Huh? Type a number genius.. You know, 1 or 2 or 10...\n";
cin.clear();
cin.ignore(256,'\n');
}
}
while (done==false){
cout<<"Enter the mass of planet #2 in kg: ";
cin>>m2;
if(cin.fail()==0){
done=true;}
else{
cout<<"Huh? Type a number genius..\n";
cin.clear();
cin.ignore(256,'\n');
}
}
while (done==false){
cout<<"Enter the radius of planet #1 in meters: ";
cin>>r1;
if(cin.fail()==0){
done=true;}
else{
cout<<"Huh? Type a number genius..\n";
cin.clear();
cin.ignore(256,'\n');
}
}
while (done==false){
cout<<"Enter the radius of planet #2 in meters: ";
cin>>r2;
if(cin.fail()==0){
done=true;}
else{
cout<<"Huh? Type a number genius..\n";
cin.clear();
cin.ignore(256,'\n');
}
}
while (done==false){
cout<<"Enter the distance between planet #1 and planet #2 in meters: ";
cin>>D;
if(cin.fail()==0){
done=true;}
else{
cout<<"Huh? Type a number genius..\n";
cin.clear();
cin.ignore(256,'\n');
}
}
inc=D/20.0;
double curDist=D;
while(curDist>0)
{
F=((m1*m2)/pow((curDist+r1+r2),2.0))*G;
//Output
cout<<"The force of attraction between planet #1 & planet #2 =\t"<<F<<" Newtons\n"<<endl;
curDist=curDist-inc;
}
return 0;
}

Thanks!
1
2
3
4
5
6
7
8
9
10
bool done = false;
while ( false == done )
{
  done = true;
}
// assert( true == done );
while ( false == done )
{
  // not going to happen
}


PS. You do repeat essentially same code block. Make the block a function that takes a string (for message to show) and returns a double. Call that function as needed.
Hi njrobby,

You a few things going on here - the first one is to please always use code tags, Select your code then press the <> button on the right.

using namespace std;

This brings in the whole std namespace which is huge, into the global namespace which can cause lots of conflicts with variable & function names. For example there is a std::left, if you had a function with that name you have a conflict.

To fix this, put std:: before each std thing as in std::cout, std::cin, std::endl, std::vector, std::string etc.

This code:
const double G=6.673*pow(10.0,-11.0);

should be written:
const double G=6.673e-11.0

All doubles & floats can take this exponent format.

double m1=0.0,m2=0.0,D=0.0,r1=0.0,r2=0.0,F=0.0,inc=0.0;

It is good practice to declare & initialise each variable on it's own line, so you can comment them - explain what each variable means and any valid ranges etc.

while (done==false){

while (!done){

! is the not operator - it reverses logic.

Although I reckon you might not need the done variable at all, and this code which is repeated 5 times could be reorganized into a function:

1
2
3
4
5
6
7
8
9
10
11
while (done==false){
cout<<"Enter the mass of planet #1 in kg: ";
cin>>m1;
if(cin.fail()==0){
done=true;}
else{
cout<<"Huh? Type a number genius.. You know, 1 or 2 or 10...\n";
cin.clear();
cin.ignore(256,'\n');
}
}


The above code sets done to true the first time, causing all the rest to be skipped, this is the crux of your problem.

F=((m1*m2)/pow((curDist+r1+r2),2.0))*G;

With this, this pow function uses a series to calc the value, and it is very expensive operation to square a value. So invent a new variable and multiply it by itself.

Hope all goes well :+)
Topic archived. No new replies allowed.