0

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

2
  • Ok, value, y, and percent are all uninitialized and using them before you set a value is undefinded behavior. Then you show "cin << value;", which is a string, not a line of code. Please show a minimal, complete example code that reproduces the problem you are having. Commented Sep 18, 2015 at 18:03
  • Thank you very much Mikhail! I was completely forgetting the single quotes. I added them in and now everything works smoothly! Thanks again! Commented Sep 18, 2015 at 19:44

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Guess it should be char value = 'y' but the principle is correct. I was about to write similar answer when your answer came. So I'll just +1 you instead. BTW: The variable value isn't needed at all.
0

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?)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.