-
while question
ok so why doesn't this work. i mean i can go into the different sections but when i'm back at main i can't go into, lets say new game again. ok. i know i'm a super noob but i'm eager to learn. thx a bunch.
Code:
#include <iostream>
using namespace std;
int main()
{
int x;
while (x!=3) {
cout<<"**MAIN MENU**\n\n";
cout<<"1.New game\n2.Options\n3.Exit\n";
cout<<"Choice:";
cin>>x;
cout<<"\n";
/*NEW GAME*/
if (x==1) {
int y;
while (y!=2) {
cout<<"*New game*\n\n";
cout<<"1.Map\n2.Back\n";
cout<<"Choice:";
cin>>y;
cout<<"\n";}
}
/*OPTIONS*/
if (x==2) {
int z;
while (z!=2) {
cout<<"*Options*\n\n";
cout<<"1.Difficulty\n2.Back\n";
cout<<"Choice:";
cin>>z;
cout<<"\n";}
}
}
}
-
Code:
int x;
while (x!=3) {
x is not initialized
Code:
int y;
while (y!=2) {
y is not initialized
-
THX vart that worked but why doesn't it work unless i define x, because isn't what i do whith the cin>> comand, right?
-
But you are trying to compare x != 3 before you're reading anything into the variable.
I suggest using Visual Studio, if you can. It easily catches uninitialized variables errors.
-
If you're making program that utilizes a menu selection system, use a case switch. you can google a tutorial, but it goes something like this.
Code:
switch (test) // where test is a declared int used to decide which case to execute
{
case 1 :
// code for menu 1
break;
case 2 :
// code for menu 2
break;
default :
// action that occurs when any other case happens (i.e. user presses a number that is not a part of your menu)
}