expected `,' or `;' before "else"

Feb 20, 2010 at 6:05am
//if and else
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystg;
int x;
cout << "Press 1 to talk about names. \n" << "press 2 to go talk about video games \n" << "Press 3 to go talk about school \n";
cin >> x;
}
if (x == 1) {
cout << "Welcome to the name talk. What is your name? \n ";
getline (cin, mystg);
cout << "Hello" << mystg << "Nice to meet you, my name is Alex. \n";
}
else if (x == 2) {
cout << "Welcome to the game talk, type in your favorite game please. \n";
getline (cin, mystg);
cout << mystg << " is a good game, but i think CoD is better. \n";
}
else if (x == 3) {
cout << "Welcome to the school chat, what is your favorite subject? \n";
getline (cin, mystg);
cout << "Nice, " << mystg << " thats a cool subject, my favorite is science. \n";
}
else {
cout << "Wrong number, type in either 1, 2, or 3.";
}
{
system ("pause");
return 0;
}








This is my first try on the if and else things and i thought i did good. but i keep getting that error in the title and it says i have the error right before the if or else if stuff. please help me and btw i just copied and pasted this from my dev-c++ so it looks different and more organized on the program, also i have just started coding 3 days ago and i am learning off of this site. Please help.
Feb 20, 2010 at 6:50am
Use code tags if you want your code formatted like it was:
http://www.cplusplus.com/articles/firedraco1/

Anyway, you have some issues with your { and }s...look at 12 for example. You are closing main() with that line, so everything after that is illegal.
Feb 20, 2010 at 6:52am
Well like i said i just started coding and thats what they told me to do for every code.
Feb 20, 2010 at 6:57am
Umm if you can like tell me what to add and stuff that would be appreciative because i really dont know what like tags are and stuff because if you go to this website i have learned through what it is on http://www.cplusplus.com/doc/tutorial/control/
Feb 20, 2010 at 7:08am
I have read to the control structures and thats when i tried my coding because i was confident but i guess i dont really know what i did wrong
Feb 20, 2010 at 7:25am
I would re-read the basic program structure tutorial. Then go and compare your program to that and see why you are having issues.
Feb 20, 2010 at 8:21am
Well they actually didnt give a full example. they only showed the part where it was like when you would use the if and else if and stuff like that
Feb 20, 2010 at 8:23am
if (x == 1) {
cout << "x is 1";
}
else if (x == 2) {
cout << "x is 2";
}
else {
cout << "value of x unknown";
}

thats what they used for an example
Feb 20, 2010 at 5:22pm
remove } before your first if statement and add } at the end of your program
Last edited on Feb 20, 2010 at 5:24pm
Topic archived. No new replies allowed.