Sorry, another question about loop and "While"

I tried making a program where it tells the user to input the "secret password". If you write any number that is not "0", it will just repeat itself and keep asking for the password.
If you DO write 0, it will countdown from 10 to 0 and "self-destruct".

I can't seem to get the countdown working. I can get it working independently, but not in this program. please help!

#include <iostream>
using namespace std;
int main()
{
int n;
int countdown = 10;
do{
cout << "Please enter the secret number\n";
cout << "WARNING, DO NOT INPUT 0; it WILL self-destruct.\n";
cin >> n;
cout << "You have entered " << n << ".\n";
}
while ( n != 0 );
{

cout << "PROGRAM FAILTURE, SELF DESTRUCT IN: \n";
countdown = 10;
while ( countdown > 0 );


cout << "t-" << countdown << "\n";
--countdown;

}


while ( countdown > 0 );
{
cout << "KABLAMMMM... you lost an arm\n";
system ("PAUSE");
return 0;
}
}
use code tags and proper indentation, because you incorrect braces which is causing the confusion, and also block scope braces, not loop braces.

indentation would show this. the code tags are the button that look like <>

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
31
int main()
{
   int n;
   int countdown = 10;
   do{
      cout << "Please enter the secret number\n";
      cout << "WARNING, DO NOT INPUT 0; it WILL self-destruct.\n";
      cin >> n;
      cout << "You have entered " << n << ".\n";
   }
   while ( n != 0 );


   {    // this is a block scope you have here... not a loop.
      cout << "PROGRAM FAILTURE, SELF DESTRUCT IN: \n";
      countdown = 10;
      while ( countdown > 0 );  // this while starts and ends here

      cout << "t-" << countdown << "\n";
      --countdown;  // this is not part of the loop on line 17

   }   // end of block scope


   while ( countdown > 0 );   // will this ever evaluate to true ?
   {    // another block scope.... not part of the while loop you need to stop using ';' so much
      cout << "KABLAMMMM... you lost an arm\n";
      system ("PAUSE");
      return 0;
   }
}


the ; is used to end a command

while (true); <--- this starts and stops here anything afterwards is not part of the loop.
Last edited on
Topic archived. No new replies allowed.