program skipping cin

Does anyone know why a program might skip straight past an input and close? I have a program that has input and output, but once it finishes with its output it closes without waiting for input. There are no if statements or other conditions that I know of that might cause the program to close prematurely.
That is probably because there is still the carriage return left from the user pressing enter in the last cin statement, so the next cin sees that and counts it as input.

You can solve this by clearing the buffer either with:

cin.sync()

cin.ignore(numeric_limits<streamsize>::max(), '\n');
Neither of those did anything
In that case I would suggest posting the code to see what else could be happening.
The problem occurs between the first and second cin. It asks the players name, performs the intro function, outputs the different choices and then quits just before the second input.

#include "stdafx.h"
#include "iostream"

using namespace std;

int waterbottles =1;
int energybars =3;
int kills =0;
int destination;
int anything;
int house1;
int house2;
int house3;
int knifechoice;
int player;
bool tireiron =true;
bool pocketknife = true;
bool dead = false;
bool success =false;

void intro();
void choicedest();
void ending();
void deathscene();


int main()
{

intro();
choicedest();

if (dead ==true)
{
deathscene();
}

else if (success=true)
{
ending();
}
return 0;
}

void intro()
{
cout<<"enter your name"<<endl;
cin>>player;
cout<<"After the plague reached the city, there was pandamonium, "<<endl;
cout<< "you only managed to escape the city because you were knocked out "<<endl;
cout<< "in the stampede""to escape the building. You awoke to hear screams "<<endl;
cout<< "from far off. The mob" "of dead that was waiting in the lobby for your"<<endl;
cout<< "fleeing co-workers had dispersed while you slept. You were ludacrisly" <<endl;
cout<< "fortunate that your car hadn't been stolen or destroyed in the chaos."<<endl;
cout<< "Unfortunately, twenty miles out of the city, you begin to" <<endl;
cout<< "run out of gas. You realize that you must move on foot." <<endl;
cout<< "After packing all of the food and tools you can scrounge out of the" <<endl;
cout<< "car,three energy bars, a pocketknife, a water bottle, and a" <<endl;
cout<< "tire iron, you head out." <<endl;
}

void choicedest()
{
cout<<"After a few minutes of walking, you realize that you do not know where" <<endl;
cout<<"you are trying to go. You sit down and think about your next move."<<endl;
cout<<"Eventually, you come up with three choices in destinations."<<endl;
cout<<"Where will you go?"<<endl;
cout<<"1.) Home, you have some nice gear there, and you'll be much harder" <<endl;
cout<<"pressed to survive without it."<<endl;
cout<<""<<endl;
cout<<"2.) The police station, It's probably the safest place to be right now."<<endl;
cout<<"After all, there is a heavily armed police department guarding it."<<endl;
cout<<""<<endl;
cout<<"3.) The country, fewer people means fewer zombies, it would be easier" <<endl;
cout<<"to survive out there.."<<endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n') >> destination;

switch (destination)
{
case 0:
cout<<"I don't think that's a good idea."<<endl;

case 1:
cout<<"Good idea, the police station is probably full of panicky idiots, and it will probably be hard to find gear in the country."<<endl;


case 2:
cout<<"Good idea, your house has probably already been overrun by zombies or looted, and it will probably be hard to find gear in the country."<<endl;


case 3:
cout<<"Good idea, your house has probably already overrun by zombies or looted, and the police station is probably full of panicky idiots."<<endl;

}
}
void gohome()
{
cout<<"You begin walking down the road in the same direction you were headed"<<endl;
cout<<"before. By force of habit, this was the direction you left the city in."<<endl;
cout<<"It is not long before you hear an unsettling noise, the slow shuffle of"<<endl;
cout<<"awkward footsteps behind you. You slowly turn around to see a horrifying"<<endl;
cout<<"sight, a grey skinned man stumbing towards you. A nauseating stench"<<endl;
cout<<"fills your nostrils, and he begins to make a noise that can only be"<<endl;
cout<<" described as growling. What will you do?"<<endl;
cout<<""<<endl;
cout<<"1.) Smash its head in with you're tire iron!"<<endl;
cout<<""<<endl;
cout<<"2.)Stab it in the brain with your knife!"<<endl;
cout<<""<<endl;
cout<<"3.)Run away!"<<endl;
cin>>house1;
switch (house1)
{
case 1:
cout<<"You swing the tire iron with all your might, and the zombie crashes to"<<endl;
cout<<"the ground, a grisly dent in the side of its head."<<endl;
kills+=1;

case 2:
cout<<"You drive the knife deep into the zombie's temple and watch it fall to"<<endl;
cout<<"the ground. Do you wish to retrieve your knife from the zombie's skull?"<<endl;
kills+=1;
cin>>knifechoice;

if (knifechoice==1)
{
cout<<"You retrieve your knife."<<endl;
}

else if (knifechoice==2)
{
cout<<"That's pretty gross, I'll just leave it there."<<endl;
pocketknife=false;
}

else
{
cout<<"Yes or no please."<<endl;
}

case 3:
cout<<"You quicken your pace to escape the zombie. However, the zombie attracted"<<endl;
cout<<"others with its growls and you are surrounded and devoured."<<endl;
dead = true;
}

cout<<"You leave the wrecked zombie carcass behind you. There are a few miles left"<<endl;
cout<<"until you reach your house, and you are starting to feel tired. What will"<<endl;
cout<<"you do?"<<endl;
cout<<""<<endl;
cout<<"1.) Eat one of your energy bars to keep your strength up."<<endl;
cout<<""<<endl;
cout<<"2.) Find a place to rest."<<endl;
cout<<""<<endl;
cout<<"3.) Man up and keep going."<<endl;
cin>>house2;

switch (house2)
{
case 1:
cout<<"You eat an energy bar. You feel a little better."<<endl;
energybars-=1;
case 2:
cout<<"You crawl under an abandoned car and fall asleep quickly. You wake to"<<endl;
cout<<"the sound of shuffling feet and growling. You are quickly devoured."<<endl;
dead =true;

case 3:
cout<<"You try to keep going, but you begin to feel weak. In your weakened "<<endl;
cout<<"state, you stand no chance when a gang of zombies emerges from behind an" <<endl;
cout<<"abandoned truck. Your head is ripped off and the ensuing squabble is"<<endl;
cout<<"reminiscent of a game of oddball."<<endl;
dead =true;
}

cout<<"You are nearly home, but blocking your way is a herd of zombies milling about"<<endl;
cout<<"your neighborhood. How will you get into your house?"<<endl;
cout<<""<<endl;
cout<<"1.) Move right through the mob, killing zombies as you go."<<endl;
cout<<""<<endl;
cout<<"2.) Set off a car alarm across the street and go when they are distracted."<<endl;
cout<<""<<endl;
cout<<"3.) Give up, there are too many, its just not worth it."<<endl;
cin>>house3;

switch (house3)
{
case 1:
cout<<"You move in to the mob. You fight bravely, but are overwhelmed by the"<<endl;
cout<<"superior numbers."<<endl;
dead=true;

case 2:
cout<<"You manage to set off a car alarm across the street from the zombies."<<endl;
cout<<"Sure enough, the mob slowly begins to amble towards the car, leaving you"<<endl;
cout<<"a window of oppourtunity to reach your house."<<endl;

case 3:
cout<<"Really? After all this effort? All right, I won't force you, but this was"<<endl;
cout<<"your quest! I wish you the best of luck, surviving with this failure on your"<<endl;
cout<<"conscience. I won't be able to go with you from this point. Therefore, I will"<<endl;
cout<<"have to assume that you have died. Goodbye"<< player<<endl;
dead= true;
}

cout<<"You sprint past the mob and into your house. You have enought supplies to fortify"<<endl;
cout<<"the place and live here for months. You have survived."<<endl;

}

void deathscene()
{
cout<<"you are dead! Input anything to quit!"<<endl;
cin>>anything;
}

void ending()
{
cout<<"Thanks for playing! Input anything to quit!"<<endl;
cin>>anything;
}
I think the problems not with input, but with your functions. Try taking your main () function to the end of the code. A simple copy and paste will do. See if that works.
You need to declare all of your functions before main starts.

like so:

1
2
3
4
5
6
7
8
9
10
11
void function();

int main()
{
}

void function()
{
   // stuff
}
@Blue Shell

I don't think that is necessary - better to put all the function declarations before main.

@continuumspooky

You should know by now to always use code tags. Edit your post, select the code, press the <> button on the right under formatting. This will make your code much easier to read & you might attract more replies.

With your switch statements - you don't have break statements at the end of each case, so executions rolls through each one rather than selecting one of them. You should also always provide a default: case to help catch errors.

HTH
closed account (zb0S216C)
See here: http://www.cplusplus.com/forum/beginner/83659/#msg448819

Wazzak
http://cplusplus.com/doc/tutorial/variables/
You are trying to save a string into an integer type
Topic archived. No new replies allowed.