Hello all. I am a new c++ programmer and I am having a lot of fun with it. I just wrote this programming to help with some American History. I've read tutorials and have learned enough to write this program. I am dissapointed in my code however. I noticed that a lot of programmers are discouraging the use of the GOTO command and that it indicates bad programming. I am ashamed to say that I used it in my program almost 20 times. Here is a sample code:
//Below is the format and notes that will be used for all the questions.
//Only one occurence of these notes will be made.
//Multiple choice question name q1:
q1:
//Define vairiables
int a1, b1;
b1 = 4;
cout<<"What are the colors of the United States Flag?\n\n";
//Possible Answers...User should choose the number, not the name
cout<<"1. Black, Gold, Green\n";
cout<<"2. Red, White\n";
cout<<"3. Blue, Black, Red, White\n";
cout<<"4. Red, White, Blue\n\n";
//Users Answer
cin>>a1;
cout<<"\n\n";
//Ignore the "ENTER" key as part of the answer
cin.ignore();
//IF the answer is correct, proceed to the next question. If not try again.
if(b1==a1){
cout<<"Correct!\n\n";
system("pause");
system("cls");
goto q2;
}
else{
cout<<"Incorrect. Please try again.\n\n";
system("pause");
system("cls");
goto q1;
}
//Question #2
q2:
int a2, b2;
b2 = 1;
cout<<"What do the stars of the flag mean?\n\n";
cout<<"1. One for each State\n";
cout<<"2. One for each President\n";
cout<<"3. One for each Senator\n";
cout<<"4. The amount of wars the USA won\n\n";
cin>>a2;
cout<<"\n\n";
cin.ignore();
if(b2==a2){
cout<<"Correct!\n\n";
system("pause");
system("cls");
goto q3;
}
else{
cout<<"Incorrect. Please try again.\n\n";
system("pause");
system("cls");
goto q2;
}
//Question #3
q3:
int a3, b3;
b3 = 3;
cout<<"How many stars are there on the American Flag?\n\n";
cout<<"1. 45\n";
cout<<"2. 46\n";
cout<<"3. 50\n";
cout<<"4. 51\n\n";
cin>>a3;
cout<<"\n\n";
cin.ignore();
if(b3==a3){
cout<<"Correct!\n\n";
system("pause");
system("cls");
goto q4;
}
else{
cout<<"Incorrect. Please try again.\n\n";
system("pause");
system("cls");
goto q3;
}
<< moderator edit: added [code][/code] tags >>
I know its real bad but it works. I would rather do it correctly though. Can someone tell me what is a differnt method other than using GOTO?
Thanks all,
Scott