While Loop Refusing To Pause
Sep 5, 2012 at 9:52pm Sep 5, 2012 at 9:52pm UTC
So I have this code(I KNOW SYSTEM IS HORRIBLE PLEASE DON'T YELL AT ME FOR IT!!):
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// Popilization.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int main(){
bool running = false ;
int time = 0;
int ans = 0;
running = true ;
while (running == true ){
system("cls" );
cout<<"1. Play" <<endl;
cout<<"2. Save | Load" <<endl;
cout<<"3. Quit" <<endl;
if (ans == 1){
system("cls" );
cout<<"Play" <<endl;
cin.get();
}
if (ans == 2){
system("cls" );
cout<<"Save/Load" <<endl;
cin.get();
running = false ;
}
if (ans == 3){
system("cls" );
cout<<"Thanks For Playing!" <<endl;
cin.get();
return 0;
}
cin.get();
}
cin.get();
return 0;
}
Currently all it does it display the menu and when I try to select an option and have it display the message, it jumps right back to the beginning of the loop. I tried cin.get(); as you can see. It also refuses to let the program close when I choose option 3. Its infuriating me, please someone tell me how to properly pause and quit a loop.
Thanks
Sep 5, 2012 at 9:59pm Sep 5, 2012 at 9:59pm UTC
What you do is what you get.
You wrote
1 2
running = true ;
while (running == true ){
So your loop will iterate while running is true. And you set running to true.
Variable ans is set to zero and you did not enter any other value to this variable
int ans = 0;
So this code will be executed never because ans is equal to 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
if (ans == 1){
system("cls" );
cout<<"Play" <<endl;
cin.get();
}
if (ans == 2){
system("cls" );
cout<<"Save/Load" <<endl;
cin.get();
running = false ;
}
if (ans == 3){
system("cls" );
cout<<"Thanks For Playing!" <<endl;
cin.get();
return 0;
}
Sep 5, 2012 at 10:02pm Sep 5, 2012 at 10:02pm UTC
Oh wow... thanks for pointing that out never even thought of that... I feel like a horrible programmer now. Thanks again!
Topic archived. No new replies allowed.