My program closes

During two spots, when you input numbers, as soon as you input them, the program closes...



#include <iostream>

using namespace std;

int main()

{
int thisnumber, thisnumber2, x, thing;
string password;
cout<< "What is your password?\n";
cin>> password;
if (password == "Open")
{
cout<< "Please enter a number between 1 and 100. If you enter a letter, this will shut down.\n";
cin >> thisnumber;
}
while (password != "Open")
{
string answer;
cout<< "Are you a hacker? (Yes or No)\n";
cin >> answer;
if (answer== "yes")
{
cout<< "COME OUT OF THE BUILDING WITH YOUR HANDS UP!\n";
getchar();
}
else if (answer == "no")
{
cout<<"You must just be an idiot.\n";
getchar();
}
while (thisnumber == 72)
//MY PROGRAM CLOSES HERE
{
cout<< "Good Job!\n";
getchar();
}
while (thisnumber != 72)
//MY PROGRAM CLOSES HERE
{
cout<< "FAIL.\n";
}}}
Last edited on
Please edit your post and put it in the code tags. This is a short code so the tags aren't a big deal but just out of principle I don't read code that's not in the right format.
code tags? I haven't taken any formal lessons, so I don't know what a lot of things are.
Well your program logic is a bit off, from how I am assuming you want to work, but regardless:

Basically put system("pause"); return 0; at the end of your main block (or a cin.get() rather than pause) and your console wont close on success...... Additionally

use if's instead of whiles for what you are doing I'd think and when you print "FAIL" and "Good Job!" you need to also system("pause"); return 0; wherever you want the main logic to terminate (unless you like that endless fail loop on failure :P)
It still doesnt work, it still closes. And what is the difference between if and while?
Put your code in between [code]code tags[/code] so that there is syntax highlighting. This is for these forums and has nothing to do with the C++ language.

if runs the code once if the condition is true, and you can add else after the closing brace to have code run if the condition was false.
while loops the code over and over and over, and each time it loops, it checks the condition to make sure it is true. If it is false, the loop ends and it goes on to the rest of the code.

Please Read: http://www.cplusplus.com/doc/tutorial/control/
Last edited on
boboman wrote:
Basically put system("pause"); return 0; at the end of your main block (or a cin.get() rather than pause) and your console wont close on success...... Additionally
Using system("pause") is considered bad practice (see: http://www.cplusplus.com/forum/beginner/1988/ ). This is a solution that you will find in the afforementioned link, provided by the forum member Duoas:
1
2
3
4
5
6
7
 ...

  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

  return 0;
  }
Duoas wrote:
You'll need to #include <limits>
kyranstar wrote:
And what is the difference between if and while?
I see that some members have already posted some advice regarding if and while control structures, but I would add that it would be advisable at this stage for you to complete addtional tutorials to fully familiarise yourself with these concepts. This website has a few great tutorials, you can find them here: http://www.cplusplus.com/doc/tutorial/

I hope this helps!
Last edited on
Topic archived. No new replies allowed.