Akardian
Akardian

Reputation: 3

While loops within while loops for a beginning programmer in C++

I'm in an introduction to C++ programming class. I am suppose to create a program that loops the whole program if a character y is inputted at the end of the program. I cannot seem to get the loop to loop even when I input the value for y I have defined the variables as follows:

char value, y;
float percent;
value=y;
y=value;
while (value==y)

It checks the condition the condition and runs the program the first time, however it does not loop. The ending statement looks as follows:

"cin<< value;"

The brackets check out, too.
Is there a rule I'm missing about having multiple while loops within while loops (I have two other loops that work fine inside the bigger loop), or is it because I cannot have the "while (input==y)" as a condition?

Thank you very much

Upvotes: 0

Views: 102

Answers (2)

Mikhail Solomatin
Mikhail Solomatin

Reputation: 26

I think you should do something like

int main() {
    char value = 'a', y;
    do {
        // do something
        cout << "hello" << endl;
        cin >> y;
    } while (y == value);

    return 0;
}

It runs the loop once, checks input character at the end and repeats if y equals to the specified value.

Upvotes: 1

MacGruber
MacGruber

Reputation: 162

Doesn't cin works that way? : cin>>value; http://www.cplusplus.com/doc/tutorial/basic_io/ And your condition is good, but if it loop once, its because the value doesn't change (maybe because the cin didn't work because of the syntax?)

Upvotes: 0

Related Questions