Using a Return
May 12, 2013 at 2:52pm UTC
I won't post the entire code because it ranges about 200 lines. Only one part of my code is giving me trouble. I made a function that returns a different number depending on the user's input. What I want to do is have an if else in int main. For example if the return of the function is 1 then this happens. Anyways, here's the code;
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
//Main menu
cout << "Fading Death" << endl;
cout << "1 = Start" << endl;
cout << "2 = Difficulty" << endl;
cout << "3 = Credits" << endl;
cin >> choice;
switch (choice)
{
/*1*/ case Start:
StartMenu ();
//Here is where I want the if else.
if (return == 1)
{
continue ;
break ;
}
else
{
break ;
}
/*2*/ case Difficulty:
//If player chooses difficulty.
DifficultyMenu ();
continue ;
break ;
/*3*/ case Credits:
//Only option is to press 4 to go back to main menu.
cout << "Created by: Daniel Murano" << endl;
cout << "4 = Back" << endl;
cin >> choice2;
if (choice2 == 4)
continue ;
default :
cout << "Incorrect input." << endl;
continue ;
}
break ;
}
return 0;
}
int StartMenu()
{
// If start is chosen
bool validChoice = false ;
while ( !validChoice )
{
cout << "1 = New Game" << endl;
cout << "2 = Resume Game" << endl;
cout << "3 = Load Game" << endl;
cout << "4 = Back" << endl;
cin >> choice2;
switch (choice2)
{
/*A*/ case NewGame:
//If new game is chosen
validChoice = true ;
cout << "Starting a new game..." << endl;
DifficultyMenu ();
break ;
//No files to load so this will simply exit the program.
/*B*/ case ResumeGame:
validChoice = true ;
cout << "Choose a file to resume..." << endl;
cout << "1: File 1" << endl;
cout << "2: File 2" << endl;
cin >> choice5;
switch (choice5)
{
case File1:
cout << "Loading..." << endl;
return 1;
break ;
case File2:
cout << "Loading..." << endl;
return 1;
break ;
}
break ;
/*C*/ case LoadGame:
validChoice = true ;
cout << "Choose a file to load..." << endl;
cout << "1: File 1" << endl;
cout << "2: File 2" << endl;
cin >> choice6;
switch (choice6)
{
case File3:
cout << "Loading..." << endl;
//These are the returns
return 1;
break ;
case File4:
cout << "Loading..." << endl;
return 1;
break ;
}
break ;
/*D*/ case 4:
validChoice = true ;
break ;
default :
cout << "Incorrect input." << endl;
}
}
}
May 12, 2013 at 3:21pm UTC
Instead of this:
1 2 3
StartMenu ();
//Here is where I want the if else.
if (return == 1)
try this:
or assign the value to a variable and then use that:
int menuvalue = StartMenu();
May 12, 2013 at 3:22pm UTC
May 12, 2013 at 4:57pm UTC
Great idea! Thanks!
Topic archived. No new replies allowed.