-
ok, well what i have done is when i enter n, i store that value as a string.
now then, i read from that file, an integer q, and read the same value, a float p.
then i set up a while loop saying that when q-p is not equal to zero, then it brings about an error.
thanks for all the advice.
-
In your posted code, you use while(!(cin >> a)), but you didn't put the cin.clear() and cin.ignore() inside that while loop. It won't work without them.
If you want to make sure n is an int, you can change your loop to the following. It checks to see if there is anything typed after the integer is read in, so if the user types 3.6, the 3 will be read in and the . will cause it to give the error:
Code:
while(!(cin >> n) || cin.get() != '\n') // If the stream fails from bad input
{
cin.clear(); // Clear the fail state
cin.ignore(1000, '\n'); // Clear the buffer
cout << "Invalid statement, please re-enter: "; // Reprompt
}
-
You want to make a cast...
Code:
float x = p - (float)q;
or put your intenger on a float
Code:
float aux = q;
result = p - aux;
that is...
so bb..
------------------------------------------
Sorry for my poor english...
-
Or for another simple way of doing it, you could read in a string, and check for the presence of a '.' indicating a floating point number or any other non-numeric character, and reject it as you see it.
Really though, I've not seen a valid use of goto's that come to mind, I am sure in some rare case they could be useful, but in every instance I've been tempted to use them, I find a loop much easier to read in any event.
-
aside to qqqqxxxx: What you really wanted to do is use a do-while loop.
Code:
do {
cout<<"enter a number";
cin>>c;
cout<<"\n";
d=c;
} while ((d-c)!=0);