Do While loops without prompting.

Jan 31, 2013 at 3:37am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
double A,B,C,S,D;
char choice;
cout<<"Hello, I am HeronBot.\nMy purpose in life is to inaccurately compute area's of triangles for you.\nPlease input the lengths of the sides of your triangle.";

do
{
cout<<"\nSide 1: ";
cin>>A;
do
{
if(A <= 0)
{cout<<"\nLength of a side must be positive\n"<<"Retry Side 1: ";
cin>>A;
}}while(A<=0);

// ^^ that repeated a couple times and then the formula.

cout<<"\nWould you like to incorrectly find the area of another triangle?\n(Yes, No)"<<endl;
cin>>choice;
}while(choice = 'Yes');
return 0;
}


Whether I type Yes or No it loops over and over without waiting for new values for A etc. and just uses the values inputted the first time.
Last edited on Jan 31, 2013 at 3:39am
Jan 31, 2013 at 3:44am
char choice;

choice is a char. A single char. You can't stuff an entire word into it. And what doesn't get stuffed into it remains in the input stream.

Also, = is for assignment. == is for comparison. Pay attention to the warnings your compiler generates -- 'Yes' is not a valid character, and we indicate character literals by enclosing them in single quotes.

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
#include <iostream>
#include <string>

using namespace std; 

int main()
{
    double A,B,C,S,D;
    string choice;
    cout<<"Hello, I am HeronBot.\nMy purpose in life is to inaccurately compute area's of triangles for you.\nPlease input the lengths of the sides of your triangle.";

    do
    {
        cout<<"\nSide 1: ";
        cin>>A;
        do
        {
            if(A <= 0)
            {cout<<"\nLength of a side must be positive\n"<<"Retry Side 1: ";
            cin>>A;
            }}while(A<=0);

            // ^^ that repeated a couple times and then the formula.

            cout<<"\nWould you like to incorrectly find the area of another triangle?\n(Yes, No)"<<endl;
            cin>>choice;
    }while(choice == "Yes");
    return 0;
}
Jan 31, 2013 at 4:04am
Ahhh so if I wanted to use char it would be:

1
2
3
4
5
6
char choice

...

cout<<"\nWould you like to incorrectly find the area of another triangle?\n(Y/N)"<<endl;
}while(choice == 'Y');


And I tried string earlier and couldn't get it to work. I just figured out it was because I was missing #include <string> ...

Thank you!
Last edited on Jan 31, 2013 at 4:05am
Topic archived. No new replies allowed.