I need to stop the program from closing at certain points.

The program is closes at certain points marked with //. can you help?

#include <iostream>

using namespace std;

int hackeralert()
{
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();
}

}
int main()

{
int thisnumber, thisnumber2, x, thing;
string password;
cout<< "What is your password?\n";
//PROGRAM CLOSES IF YOU ANSWER WRONG?
cin>> password;
if (password == "Open")
{
cout<< "Please enter a number between 1 and 100. If you enter a letter, this will shut down.\n";
//PROGRAM CLOSES HERE
cin >> thisnumber;
}
if (thisnumber == 72)
{
cout<< "Good Job!\n";
getchar();
}
else if (thisnumber != 72)
{
cout<< "FAIL.\n";
}}

The program will close because you are not giving the user a second chance. Put all these things in a while loop which will end if the user say's he want's to end the program. Give a facility, if the user answer's wrong, the code reaches at the top of the loop and give the user another chance.

Also, when the answer is wrong, thisnumber will not get a chance to initialize itself.
Well I want the program to display "COME OUT OF THE BUILDING WITH YOUR HANDS UP!", but it doesn't show that. It just shuts down.
Take a look at your main() function. Where will the control go if the user answers incorrectly? It will skip past both if statments and hit the end of the function, terminating the program.

Also, you didn't even call the hackeralert() function. You probably meant to call it after the second if statement.
Last edited on
Topic archived. No new replies allowed.