| | |
Completed Code but A LOT of GOTO...Replacement?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2005
Posts: 12
Reputation:
Solved Threads: 0
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:
<< 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
C++ Syntax (Toggle Plain Text)
//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; }
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
How about writing each question as a function. Have the function return 0 if they get it wrong or 1 if they get it right. Loop around each question-function until they get it right, then move to the next function-question.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Hi Scott,
You have the logic down, but goto, in an expanded form, becomes unmanageable and you end up with many lines of code that are not necessary. Why don't you try to set a variable (for the questions, like you have..q1, q2) and create a switch statement to cycle through them? Pass in the variable for the switch case.
<< moderator edit: added [code][/code] tags >>
Here's an expansion of the suggestion above: http://www.cprogramming.com/tutorial/lesson5.html
Hope this helps,
ej
You have the logic down, but goto, in an expanded form, becomes unmanageable and you end up with many lines of code that are not necessary. Why don't you try to set a variable (for the questions, like you have..q1, q2) and create a switch statement to cycle through them? Pass in the variable for the switch case.
C++ Syntax (Toggle Plain Text)
switch(qValue) { case q1: code; code; break; case q2: ... case qError: ... default: ... }
Here's an expansion of the suggestion above: http://www.cprogramming.com/tutorial/lesson5.html
Hope this helps,
ej
I think it should work fine, but am interested in what Dave thinks.
His function suggestion might be more useful. In a few C# programs I wrote, I have used a variation of the switch case to cycle through responses from a host system.
Question: How many questions are you going to use?
Dave, what do you think?
His function suggestion might be more useful. In a few C# programs I wrote, I have used a variation of the switch case to cycle through responses from a host system. Question: How many questions are you going to use?
Dave, what do you think?
Using a switch vs functions? It depends on the coder's comfort level. If I were to do it, I'd make all the functions with the same signature and create an array of function pointers to all of these question functions, and then loop through them.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jun 2005
Posts: 12
Reputation:
Solved Threads: 0
Thanks both of you. I will attempt to write codes for both the switch and the function (isnt switch a function) and post them. Then we can evaluate the best method to use. The syntax of switch is kinda funny. BTW I was going to do 100+ questions. I planned on writing them into 4 programs, then linking the programs. I was going to use a menu screen such as:
<< moderator edit: added [code][/code] tags >>

Thanks so much,
Scott
C++ Syntax (Toggle Plain Text)
//Define Variables int a, b, c, d, e; a = 1; b = 2; c = 3; d = 4; //Question choices cout<<"Please select your choice below:\n\n"; cout<<"1. Sample Question 1\n"; cout<<"2. Sample Question 2\n"; cout<<"3. Sample Question 3\n"; cout<<"4. Sample Question 4\n\n"; cin>>e; cout<<"\n\n"; cin.ignore(); //If answers are selected goto the specified lesson. If not, ask again. if(e==a){ system("cls"); goto q1; //links to the program. I know it is wrong but just an example } else if(e==b){ system("cls"); goto q2; //links to the program. I know it is wrong but just an example } else if(e==c){ system("cls"); goto q3; //links to the program. I know it is wrong but just an example } else if(e==d){ system("cls"); goto q4; //links to the program. I know it is wrong but just an example } else { cout<<"Incorrect selection. Please select from one of the options above\n\n"; system("pause"); system("cls"); goto outline; }

Thanks so much,
Scott
This is a short example of what I was saying. When there is repetitive code, like there is here, I prefer to go after a better solution. But hopefully it will demonstrate some basics in a direction away from gotos.
[edit]Maybe like this.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; bool q1() { static const char question[] = "What are the colors of the United States Flag?"; static const char *answer[] = { "Black, Gold, Green", "Red, White", "Blue, Black, Red, White", "Red, White, Blue", }; cout << question << "\n\n"; for ( size_t i = 0; i < sizeof answer / sizeof *answer; ++i ) { cout << i + 1 << ". " << answer[i] << '\n'; } cout << "\nanswer? "; int response; cin >> response; cin.ignore(); return response == 4; } bool q2() { static const char question[] = "What do the stars of the flag mean?"; static const char *answer[] = { "One for each State", "One for each President", "One for each Senator", "The amount of wars the USA won", }; cout << '\n' << question << "\n\n"; for ( size_t i = 0; i < sizeof answer / sizeof *answer; ++i ) { cout << i + 1 << ". " << answer[i] << '\n'; } cout << "\nanswer? "; int response; cin >> response; cin.ignore(); return response == 1; } int main() { while ( q1() == 0) { cout << "\nIncorrect, please try again.\n"; } cout << "\nCorrect!\n"; while ( q2() == 0) { cout << "\nIncorrect, please try again.\n"; } cout << "\nCorrect!\n"; return 0; }
[edit]Maybe like this.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; struct Question { const char *question; const char *answer[4]; int correct; }; static Question Quiz[] = { { // Question 1 "What are the colors of the United States Flag?", { "Black, Gold, Green", "Red, White", "Blue, Black, Red, White", "Red, White, Blue", }, 4 }, { // Question 2 "What do the stars of the flag mean?", { "One for each State", "One for each President", "One for each Senator", "The amount of wars the USA won", }, 1 }, }; bool ask(size_t j) { cout << Quiz[j].question << "\n\n"; for ( size_t i = 0; i < sizeof Quiz[j].answer / sizeof *Quiz[j].answer; ++i ) { cout << i + 1 << ". " << Quiz[j].answer[i] << '\n'; } cout << "\nanswer? "; int response; cin >> response; cin.ignore(); return response == Quiz[j].correct; } int main() { for ( size_t i = 0; i < sizeof Quiz / sizeof *Quiz; ++i ) { while ( ask(i) == 0 ) { cout << "\nIncorrect, please try again.\n\n"; } cout << "\nCorrect!\n"; } return 0; }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Other Threads in the C++ Forum
- Previous Thread: Can someone that knows arrays check my program out so far
- Next Thread: Input Output File Streams - Sorting Help With Array
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






