do-while loop problem

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;


const double GRAVITY = 9.80665;
double initialA(double intialA);
double initialV(double intialV);
double height(double height);

int main()
{

const double GRAVITY = 9.80665;
double initialA;
double initialV;
double height;

cout<<"Lets see how far a projectile can be launched!"<< endl;

cout<< endl;

cout<<"You are on the surface of Earth at a gravity of 9.81 meters per secound squared."<<endl;

cout<<endl;

// This is where my problem is
do
{
cout<<"Please enter the initial angle of your projectile (0.0 to 90.0):";
cin>>initialA;

}
while (initialA != 0.0,90.0);





cout<<"Please enter the initial velocity your projectile (0.0 to 999.99):";
cin>>initialV;




return 0;
}
Last edited on
closed account (zb0S216C)
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/
Last edited on
try this man =)

1
2
3
4
5
6
 
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):";
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int main()
{
    const float 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");
}
Last edited on
closed account (zb0S216C)
Metl Wolf, your condition state that the value must either be less than 0.0 or greater than 90.0. You got it the wrong way around or I read it wrong.

Khaltazar, you don't need the valid variable. It's just a wasteful variable on the stack. Conditions and/or equality operators are only needed here.
Last edited on
@Framework

Metl Wolf, your condition state that the value must either be less than 0.0 or greater than 90.0. You got it the wrong way around or I read it wrong.


you read it wrong. it makes you re-enter the value if it is less than 0.0 or greater than 90.0 =P
Last edited on
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.
closed account (zb0S216C)
Can you post the code? I'll have a better understanding that way.
I just got it metl wolf's method worked.
my example works better >_> haha =P


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):";
}


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.)
closed account (zb0S216C)
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?
@Framework
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
Last edited on
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.
closed account (zb0S216C)
Metl Wolf, your for loop asks for input twice. Why? Personally, I would never use your for loop. No offense intended.
Last edited on
@Framework

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
Last edited on
closed account (zb0S216C)
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.
Last edited on
congrats on 420 posts btw =P
and i edited out that last part of my last post >_> it was unnecessary... sorry
and i would have preffered this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main()
{

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 ) );
}


considering your while loop accepts numbers outside of the range instead of inside.. >_> compile and run your code man.. * facepalm *
Topic archived. No new replies allowed.