Hello this is my first forum on this website. Am looking for a little help on my code. Am trying to validate input using the do-while loop but I can't seem to make it work. I just need it to check if the user input is from 0.0 to 90.0 and if not to repeat the user for a correct input.Then move on to the next line of code. I have tried many different ways to make it function they way I want it to and have failed. Any advice on how to resolve this would help thank you.
#include<iostream>
#include<cmath>
#include<iomanip>
#include<math.h>
#include<stdio.h>
using namespace std;
Your problem resides within the parentheses of while. Try changing it to: while( (initialA >= 0.0 ) && ( initialA <= 90.0 ) ). I do believe that this will solve your problem.
Note: Conditions must be separated with a logical, and/or equality operators, not a comma. See: http://www.cplusplus.com/doc/tutorial/operators/
double initialA;
for (cin >> initialA; initialA < 0.0 || initialA > 90.0; cin >> initialA)
{
cout<<"Please enter the initial angle of your projectile (0.0 to 90.0):";
}
#include <iostream>
usingnamespace std;
int main()
{
constfloat Gravity = 9.80665;
float InitialA = 0.0;
float InitialV = 0.0;
float Height = 0.0;
bool Valid = false;
cout << "Let us see how far a projectile can be launched!\n" << endl;
cout << "You are on the surface of Earth at a gravity of 9.81 meters per second squared.\n" << endl;
do
{
cout << "Please enter the initial angle of your projectile (0.0 to 90.0): ";
cin >> InitialA;
if (InitialA >= 0.0 && InitialA <= 90.0) Valid = true;
else Valid = false;
}
while (! Valid);do
{
cout << "Please enter the initial velocity of your projectile (0.0 to 999.99): ";
cin >> InitialV;
if (InitialV >= 0.0 && InitialV <= 999.99) Valid = true;
else Valid = false;
}
while (! Valid);return 0;
system("pause");
}
I tired framework's approch and it works perfectly. Only problem was the code kept validating until I/user typed in a number that made the statement false. I couldn't go to my next line of code unless I made the statement false. Sorry if I wasn't clear am a beginner at coding.
for (cin >> initialA; initialA < 0.0 || initialA > 90.0; cin >> initialA)
{
cout<<"Please enter the initial angle of your projectile (0.0 to 90.0):";
}
or
1 2 3 4
for (cin >> initialA; initialA <= 0.0 || initialA >= 90.0; cin >> initialA)
{
cout<<"Please enter the initial angle of your projectile (0.0 to 90.0):";
}
whichever. depending on whether or not you want to accept 0.0 or 90.0 as valid input or not.
The posting is the code. Unless you mean something else bear with me here am just starting to get the hang of this. P.S thanks for the link I felt like an idiot can't believe I didn't figure that (Conditions must be separated with a logical, and/or equality operators, not a comma.)
I don't get it. Jorgein state: I just need it to check if the user input is from 0.0 to 90.0. However, Metl Wolf's condition is only true if initialA's value is outside the range of 0.0 and 90.0. How is that correct?
I don't get it. Jorgein state: I just need it to check if the user input is from 0.0 to 90.0. However, Metl Wolf's condition is only true if initialA's value is outside the range of 0.0 and 90.0. How is that correct?
if the condition is true, user is forced to enter in a new value for initialA, thats how it works =P
compile it and run it if you dont believe me
That was my problem besides the condition being seperated the wrong way. If I inputed any were between 0.0 to 90.0. The loop would keep looping asking me over and over for a value only when I inputed something less or greater then 0.0 to 90.0 would the code then move on to the next line. P.S Thank you guys I was starting to lose it.
Metl Wolf, your for loop asks for input twice. Why? Personally, I would never use your for loop. No offense intended.
nah, it asks for input once on entry of the loop, then it asks for it again for everytime you enter in an invalid number =D
1 2 3 4
for (cin >> initialA; initialA < 0.0 || initialA > 90.0; cin >> initialA)
{
cout<<"Please enter the initial angle of your projectile (0.0 to 90.0):";
}
the first " cin >> initialA " initializes initialA. the other "cin >> initialA" is used whenever initialA is not between 0.0 - 90.0
EDIT:: and like i said, compile and run it yourself =P
not my fault you dont understand the workings of a for loop
Firstly, I know how a for loop works. Secondly, your for loop solution is awkward.
I would have favored:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
double initialA( 0.0 );
// Other code...
do
{
cout << "Enter a number between 0.0 and 90.0: ";
cin >> initialA;
if( cin.good( ) == false )
{
cout << "That's not even a number." << endl;
break;
}
} while( ( initialA >= 0.0 ) && ( initialA <= 90.0 ) );
This code clearly states that the while loop will only stop if the entered numerical value is greater than 90.0 or less than 0.0. In addition, non-numerical values are handled, unlike yours.