Loops

I am working on while, do while, and for loops. The program below works properly until I get to Activity 4. This portion once it is run should read 4 second to go, 3 seconds to go, etc., but it is not showing anything at all. Here is the code I have so far

3 #include <iostream>
4 using namespace std;
5
6 int main()
7 {
8 cout << "Jessica Garza \n";
9 cout << "\nActivity 1 \n==========\n";
10 // Change the following do-while loop to a while loop.
11 int inputNum;
12
13 while (inputNum != 0)
14 {
15 cout << "Enter a number (or 0 to quit): ";
16 cin >> inputNum;
17 }
18
19 cout << "\nActivity 2 \n==========\n";
20 // Change the following while loop to a do-while loop.
21 char doAgain = 'y';
22
23 do
24 { cout << "Do you want to loop again? (y/n) ";
25 cin >> doAgain;
26 } while (doAgain == 'Y' || doAgain == 'y');
27
28 cout << "\nActivity 3 \n==========\n";
29 // Change the following while loop to a for loop.
30 for (int count = 1; count <= 5; count++)
31
32 cout << "Count is " << count << endl;
33
34 cout << "\nActivity 4 \n==========\n";
35 // Change the following for loop to a while loop.
36 {
37 int x = 0;
38
39 while (x <= 5)
40
41 x > 0; x--;
42
43 cout << x << " seconds to go. \n";
44 }
You have an infinite loop. Only the first statement after the loop condition is part of the loop so what you have is this:
1
2
while (x <= 5)
	x > 0;

x never change so there is no way the loop condition could become false.
Like Peter said X never
1
2
3
4
5
6
7
 int x = 0;
while (x <= 5)
{
   x > 0;
   cout << x << " seconds to go. \n";
  x++;
} 


Output

0 seconds to go. 
1 seconds to go. 
2 seconds to go. 
3 seconds to go. 
4 seconds to go. 
5 seconds to go. 
Last edited on
Thank you both this was very helpful
Topic archived. No new replies allowed.