BASIC c++ question
Please do not try to fix this, I just have a question regarding the function of this code.
I understand this is an infinite loop, however, why is it that when I enter '0', it continues to ask the user for an input, but when I enter a letter, it continues infinitiy. And when it does infinitely loop, why is it that it continues to add 1 to each number? Doesn't the loop go through each line within the loop and if so, why does it continue to infinitely loop without asking the user for an input, since that is part of the loop
1 #include <iostream>
2 using namespace std;
3 int main ()
4 {
5 int z = 0;
6 int c = 'y';
7
8 while (c = 'y') {
9 cout << z << " ";
10 cin >> c;
11 z = z + 1;
12 }
13
14 return 0;
15 }
~
Because you are using single = operator in while loop which will alwyas remain true irrespective condition you added that is why you see that 0 is printed on screen mean control has entered while loop. next thing is that you have declared c varable as integer so when you input int value to c it will work as expected but when you enter char variable it will stuck in while loop because loop will remain true because of single = operator. since you have entered char value to int variable there is no way for compiler to redo that, so it will stuck while loop and cout<<z and z=z+1 statement will continue to work as usual but till char value is not removed from c variable it will not let user to input another value to it and will run infinitely in loop.
Get Answers For Free
Most questions answered within 1 hours.