Menu Help!

Here is a code I created and I need some help with the errors in it! Thanks:code


#include <iostream>
#include <string>

using namespace std;

void menu (int& a)
{
cout << " 1 - Start Game.\n";
cout << " 2 - Online Multiplayer.\n";
cout << " 3 - Clans.\n";
cout << " 4 - Achievements.\n";
cout << " 5 - Downloadable Content.\n";
cout << " 6 - Settings.\n";
cout << " 7 - Exit.\n";
cout << " Enter your choice and press return: ";
cin >> a;
}

void decision(int& a)
{
int choice;
switch (a)
{

case 1: cout << "Heres the game!.\n";
cout << "To Go Back Press 0.\n";
cin >> a;
break;

case 2: cout << "Working on Multiplayer!\n";
cout << "To Go Back Press 0.\n";
// rest of code here
break;


case 3: std::string clanname;
std::getline( std::cin, clanname ); // <- gets one line of text from the user, puts it in "clanname" variable

std::cout << "your clan name is " << clanname;
break;

case 4: cout << "Noob: Play this game online/offline for an hour!\n";
cout << "Grenade Professional: Get two kills with one grenade!\n";
cout << "Pro Shooter: Get 100 head shots!\n";
cout << "Lets Play Online: Play one match online!\n";
cout << "Offline Professional: Beat offlie!\n";
cout << "Online Modes: Play one round on every mode online!\n";
cout << "Sniper Maddness: Play on sniper maddness 10 times!\n";
cout << "Battle: Play on battle 10 times!\n";
cout << "Team Battle: Play on team battle 10 times!\n";
cout << "Versus: Play on versus 10 times!\n";
cout << "To Go Back Press 0.\n";
// rest of code here
break;

case 5: cout << "Every thing is 99 cents"\n";
cout << "Skin\n";
cout << "Gamer Point\n";
break;


case 6: cout << "Profile\n";
cout << "Gamer Points\n";
cout << "Skins\n";
cout << "Options\n";
break;

case 7; cout << "Exit\n";
break;

break;
default: cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
break;

}

int main()
{
int choice;
bool gameOn = true;

menu(choice);
switch (choice)
{

case 1: cout << "Heres the game!.\n";
cout << "To Go Back Press 0.\n";
break;

case 2: cout << "Working on Multiplayer!\n";
"To Go Back Press 0.\n";
// rest of code here
break;


case 3: std::string clanname;
std::getline( std::cin, clanname ); // <- gets one line of text from the user, puts it in "clanname" variable

std::cout << "your clan name is " << clanname;
break;

break;

case 4: cout << "Noob: Play this game online/offline for an hour!\n";
cout << "Grenade Professional: Get two kills with one grenade!.\n";
cout << "Pro Shooter: Get 100 head shots!\n";
cout << "Lets Play Online: Play one match online!\n";
cout << "Offline Professional: Beat offlie!\n";
cout << "Online Modes: Play one round on every mode online!\n";
cout << "Sniper Maddness: Play on sniper maddness 10 times!\n";
cout << "Battle: Play on battle 10 times!\n";
cout << "Team Battle: Play on team battle 10 times!\n";
cout << "Versus: Play on versus 10 times!\n";
cout << "To Go Back Press 0.\n";
// rest of code here
break;

case 5: cout << "Every thing is 99 cents"\n";
cout << "Skin\n";
cout << "Gamer Points
cout << "To Go Back Press 0.\n";
break;

case 6: cout << "Profile\n";
cout << "Gamer Points\n";
cout << "Skins\n";
cout << "Options\n";
cout << "To Go Back Press 0.\n";
break;

case 7: "Exit\n";
break;

gameOn = false;
break;
default: cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break;
}

system ("pause");
return 0;
}
Last edited on
Firstly, please use code tags to make your code easier to read.

Secondly, could you please sit still and clear your mind of all thoughts for the next 30 minutes, so that it's easier for us to read your mind to find out what these mysterious errors are?
Please someone help everytime I try to fix it I run the program again and another error pops up! If you can help at all please do. and if someone doesnt mind can you tell me how to put code tags on a code if I post one? Thanks so much!
To use code tags you put your code in between the tags:

[code]Put your code in here[/code]

Secondly, as MikeyBoy [snidely] suggested, it helps if you tell us what and where the errors actually are. The compiler will tell you specifically which line it's on and will give you a message that is a clue as to what's wrong. You should post that stuff with your code so we can clearly see what the problem is.

The first of your errors is here:

1
2
3
4
5
6
7
8
9
case 3: std::string clanname;
std::getline( std::cin, clanname ); // <- gets one line of text from the user, puts it in "clanname" variable

std::cout << "your clan name is " << clanname;
break;

break;

case 4:


This is a weird quirk with switch statements.... when you declare a variable in a case statement, it has a scope which expands to the entire switch, but the ctor gets skipped in case labels after it, which cases an error.

If you don't understand what that means it's fine... basically you have 2 options:
1) move the 'clanname' variable declaration outside of the switch
or
2) put the case 3 code inside braces to limit the scope of the clanname variable:
1
2
3
4
case 3:
{ // <- braces to limit scope
    //...
}break;





You have several other errors related to missing or misplaced quotation marks. I'll let you find those on your own. They're not difficult -- the compiler will tell you the line the problem is on... so just look closely at that line and maybe 1 or 2 lines above it and try to spot the problem.
Here is the exact line of 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <string>
 
using namespace std;

void menu (int& a)
{
cout << " 1 - Start Game.\n";
cout << " 2 - Online Multiplayer.\n";
cout << " 3 - Clans.\n";
cout << " 4 - Achievements.\n";
cout << " 5 - Downloadable Content.\n";
cout << " 6 - Settings.\n";
cout << " 7 - Exit.\n";
cout << " Enter your choice and press return: ";
cin >> a;
}

void decision(int& a)
{
int choice;
switch (a)
{
 
case 1: cout << "Heres the game!.\n";
cout << "To Go Back Press 0.\n";
cin >> a;
break;
 
case 2: cout << "Working on Multiplayer!\n";
               cout << "To Go Back Press 0.\n";
// rest of code here
break;
   

case 3: std::string clanname;
std::getline( std::cin, clanname ); // <- gets one line of text from the user, puts it in "clanname" variable

std::cout << "your clan name is " << clanname;
break;
 
case 4: cout << "Noob: Play this game online/offline for an hour!\n";
cout << "Grenade Professional: Get two kills with one grenade!\n";
cout << "Pro Shooter: Get 100 head shots!\n";
cout << "Lets Play Online: Play one match online!\n";
cout << "Offline Professional: Beat offlie!\n";
cout << "Online Modes: Play one round on every mode online!\n";
cout << "Sniper Maddness: Play on sniper maddness 10 times!\n";
cout << "Battle: Play on battle 10 times!\n";
cout << "Team Battle: Play on team battle 10 times!\n";
cout << "Versus: Play on versus 10 times!\n";
cout << "To Go Back Press 0.\n";
// rest of code here
break;
 
case 5: cout << "Every thing is 99 cents"\n";
            cout << "Skin\n";
               cout << "Gamer Point\n";
   break;
  
 
       case 6: cout << "Profile\n";
               cout << "Gamer Points\n";
               cout << "Skins\n";
               cout << "Options\n";
               break;
 
   case 7; cout << "Exit\n";
   break;
 
break;
default: cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
break;
 
}

int main()
{
int choice;
bool gameOn = true;

menu(choice);
switch (choice)
{
 
case 1: cout << "Heres the game!.\n";
cout << "To Go Back Press 0.\n";
break;
 
case 2: cout << "Working on Multiplayer!\n";
	"To Go Back Press 0.\n";
// rest of code here
break;
   

case 3: std::string clanname;
std::getline( std::cin, clanname ); // <- gets one line of text from the user, puts it in "clanname" variable

std::cout << "your clan name is " << clanname;
break;
 
   break;
 
case 4: cout << "Noob: Play this game online/offline for an hour!\n";
cout << "Grenade Professional: Get two kills with one grenade!.\n";
cout << "Pro Shooter: Get 100 head shots!\n";
cout << "Lets Play Online: Play one match online!\n";
cout << "Offline Professional: Beat offlie!\n";
cout << "Online Modes: Play one round on every mode online!\n";
cout << "Sniper Maddness: Play on sniper maddness 10 times!\n";
cout << "Battle: Play on battle 10 times!\n";
cout << "Team Battle: Play on team battle 10 times!\n";
cout << "Versus: Play on versus 10 times!\n";
cout << "To Go Back Press 0.\n";
// rest of code here
break;
 
case 5: cout << "Every thing is 99 cents"\n";
        cout << "Skin\n";
            cout << "Gamer Points
               cout << "To Go Back Press 0.\n";
               break;
  
   case 6: cout << "Profile\n";
               cout << "Gamer Points\n";
               cout << "Skins\n";
               cout << "Options\n";
               cout << "To Go Back Press 0.\n";
               break;
 
   case 7: "Exit\n";
   break;
   
   gameOn = false;
break;
default: cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break;
}

system ("pause");
return 0;
} 



and here are the errors:



1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(42): error C2360: initialization of 'clanname' is skipped by 'case' label
1> c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(36) : see declaration of 'clanname'
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(56): error C2360: initialization of 'clanname' is skipped by 'case' label
1> c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(36) : see declaration of 'clanname'
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(56): error C2017: illegal escape sequence
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(56): error C2001: newline in constant
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(56): error C2146: syntax error : missing ';' before identifier 'n'
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(56): error C2065: 'n' : undeclared identifier
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(56): error C2143: syntax error : missing ';' before 'string'
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(57): error C2146: syntax error : missing ';' before identifier 'cout'
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(62): error C2360: initialization of 'clanname' is skipped by 'case' label
1> c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(36) : see declaration of 'clanname'
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(68): error C2143: syntax error : missing ':' before ';'
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(68): error C2360: initialization of 'clanname' is skipped by 'case' label
1> c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(36) : see declaration of 'clanname'
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(72): error C2361: initialization of 'clanname' is skipped by 'default' label
1> c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(36) : see declaration of 'clanname'
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(79): error C2601: 'main' : local function definitions are illegal
1> c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(20): this line contains a '{' which has not yet been matched
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(119): error C2017: illegal escape sequence
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(119): error C2001: newline in constant
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(121): error C2001: newline in constant
1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(146): fatal error C1075: end of file found before the left brace '{' at 'c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(20)' was matched
you are already using namespace std.

stop re-putting std::
Secondly, as MikeyBoy [snidely] suggested [...]

Well, I was going for humourously, but I guess it didn't come off.

But seriously, I don't get why you would come to a site looking for help, and then withold the information that would be crucial in obtaining that help. It just seems like such a no-brainer...

In any case - apart from the problem that Disch has already explained, the one on line 56 is because you have put some extra quote marks in the middle of a string:

cout << "Every thing is 99 cents"\n";

On line 68, you're using a semicolon where you should be using a colon in the case label.

The error on line 79 is because you have too few closing braces at the end of your decision function. You need one to close the switch/case block, and another to close your function.

At 119, there's a repeat of the same error as line 56.

At 121, you've missed the quotes from the end of your string.

Seriously - compiler error messages are your friends. Read them carefully, and look at the lines of code they refer to, because they will help you fix the problems.

well, mostly all of it is done except even after I tryed yours @Disch this error keeps poping up on a TON of line.

1>c:\users\tracy\documents\visual studio 2010\projects\hello world\hello world\main.cpp(42): error C2360: initialization of 'clanname' is skipped by 'case' label. that is not the only time it pops up it pops up like 15 other times. What does this mean?
closed account (Dy7SLyTq)
it means that your missing the initialization of your object because its in a case that isnt being accessed if im not mistaken
My first reply already explains how to solve that issue:

I wrote:

This is a weird quirk with switch statements.... when you declare a variable in a case statement, it has a scope which expands to the entire switch, but the ctor gets skipped in case labels after it, which cases an error.

If you don't understand what that means it's fine... basically you have 2 options:
1) move the 'clanname' variable declaration outside of the switch
or
2) put the case 3 code inside braces to limit the scope of the clanname variable:

1
2
3
4
case 3:
{ // <- braces to limit scope
    //...
}break;

@Disch it finaly worked, there is one mess up and I tryed to fix it..... it says (157) fatal error c1075: end of file found before the left brace '{' at (155) was matched..... I tryed switching the braces up but that didnt seem to work.
closed account (Dy7SLyTq)
your missing a brace
For every opening brace { there must be a closing brace }

It might be hard to see, but in my example I snuck a closing brace in right before the break;
Look at line 155 of your code. Presumably there is an open-brace there. Do you have a matching close-brace somewhere? Do you have a matching close-brace for each and every open-brace in your code?
Topic archived. No new replies allowed.