Completed Code but A LOT of GOTO...Replacement?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2005
Posts: 12
Reputation: cscotty is an unknown quantity at this point 
Solved Threads: 0
cscotty cscotty is offline Offline
Newbie Poster

Completed Code but A LOT of GOTO...Replacement?

 
0
  #1
Jun 3rd, 2005
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:


  1. //Below is the format and notes that will be used for all the questions.
  2. //Only one occurence of these notes will be made.
  3.  
  4.  
  5. //Multiple choice question name q1:
  6. q1:
  7.  
  8. //Define vairiables
  9. int a1, b1;
  10. b1 = 4;
  11.  
  12. cout<<"What are the colors of the United States Flag?\n\n";
  13.  
  14. //Possible Answers...User should choose the number, not the name
  15. cout<<"1. Black, Gold, Green\n";
  16. cout<<"2. Red, White\n";
  17. cout<<"3. Blue, Black, Red, White\n";
  18. cout<<"4. Red, White, Blue\n\n";
  19.  
  20. //Users Answer
  21. cin>>a1;
  22. cout<<"\n\n";
  23. //Ignore the "ENTER" key as part of the answer
  24. cin.ignore();
  25.  
  26. //IF the answer is correct, proceed to the next question. If not try again.
  27. if(b1==a1){
  28. cout<<"Correct!\n\n";
  29. system("pause");
  30. system("cls");
  31. goto q2;
  32. }
  33.  
  34. else{
  35. cout<<"Incorrect. Please try again.\n\n";
  36. system("pause");
  37. system("cls");
  38. goto q1;
  39. }
  40.  
  41. //Question #2
  42.  
  43. q2:
  44. int a2, b2;
  45. b2 = 1;
  46. cout<<"What do the stars of the flag mean?\n\n";
  47.  
  48. cout<<"1. One for each State\n";
  49. cout<<"2. One for each President\n";
  50. cout<<"3. One for each Senator\n";
  51. cout<<"4. The amount of wars the USA won\n\n";
  52.  
  53. cin>>a2;
  54. cout<<"\n\n";
  55. cin.ignore();
  56.  
  57. if(b2==a2){
  58. cout<<"Correct!\n\n";
  59. system("pause");
  60. system("cls");
  61. goto q3;
  62. }
  63.  
  64. else{
  65. cout<<"Incorrect. Please try again.\n\n";
  66. system("pause");
  67. system("cls");
  68. goto q2;
  69. }
  70.  
  71. //Question #3
  72. q3:
  73. int a3, b3;
  74. b3 = 3;
  75. cout<<"How many stars are there on the American Flag?\n\n";
  76.  
  77. cout<<"1. 45\n";
  78. cout<<"2. 46\n";
  79. cout<<"3. 50\n";
  80. cout<<"4. 51\n\n";
  81.  
  82. cin>>a3;
  83. cout<<"\n\n";
  84. cin.ignore();
  85.  
  86. if(b3==a3){
  87. cout<<"Correct!\n\n";
  88. system("pause");
  89. system("cls");
  90. goto q4;
  91. }
  92.  
  93. else{
  94. cout<<"Incorrect. Please try again.\n\n";
  95. system("pause");
  96. system("cls");
  97. goto q3;
  98. }
<< 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Completed Code but A LOT of GOTO...Replacement?

 
0
  #2
Jun 3rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 22
Reputation: EricaJanine is an unknown quantity at this point 
Solved Threads: 0
EricaJanine's Avatar
EricaJanine EricaJanine is offline Offline
Newbie Poster

Re: Completed Code but A LOT of GOTO...Replacement?

 
0
  #3
Jun 3rd, 2005
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.

  1. switch(qValue)
  2. {
  3. case q1:
  4. code;
  5. code;
  6. break;
  7. case q2:
  8. ...
  9. case qError:
  10. ...
  11. default:
  12. ...
  13. }
<< 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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: cscotty is an unknown quantity at this point 
Solved Threads: 0
cscotty cscotty is offline Offline
Newbie Poster

Re: Completed Code but A LOT of GOTO...Replacement?

 
0
  #4
Jun 3rd, 2005
Thanks a lot for your help you guys. Erica, is the way you suggested ok or is it just to get by. If it is ok, can you please use one of my questions and list it as an example. I appreciate your example but I would like to see the code.

Thanks,
SCot
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 22
Reputation: EricaJanine is an unknown quantity at this point 
Solved Threads: 0
EricaJanine's Avatar
EricaJanine EricaJanine is offline Offline
Newbie Poster

Re: Completed Code but A LOT of GOTO...Replacement?

 
0
  #5
Jun 3rd, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Completed Code but A LOT of GOTO...Replacement?

 
0
  #6
Jun 3rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: cscotty is an unknown quantity at this point 
Solved Threads: 0
cscotty cscotty is offline Offline
Newbie Poster

Re: Completed Code but A LOT of GOTO...Replacement?

 
0
  #7
Jun 3rd, 2005
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:

  1. //Define Variables
  2. int a, b, c, d, e;
  3. a = 1;
  4. b = 2;
  5. c = 3;
  6. d = 4;
  7.  
  8. //Question choices
  9. cout<<"Please select your choice below:\n\n";
  10. cout<<"1. Sample Question 1\n";
  11. cout<<"2. Sample Question 2\n";
  12. cout<<"3. Sample Question 3\n";
  13. cout<<"4. Sample Question 4\n\n";
  14.  
  15. cin>>e;
  16. cout<<"\n\n";
  17. cin.ignore();
  18.  
  19. //If answers are selected goto the specified lesson. If not, ask again.
  20. if(e==a){
  21. system("cls");
  22. goto q1; //links to the program. I know it is wrong but just an example
  23. }
  24.  
  25. else if(e==b){
  26. system("cls");
  27. goto q2; //links to the program. I know it is wrong but just an example
  28. }
  29.  
  30. else if(e==c){
  31. system("cls");
  32. goto q3; //links to the program. I know it is wrong but just an example
  33. }
  34.  
  35. else if(e==d){
  36. system("cls");
  37. goto q4; //links to the program. I know it is wrong but just an example
  38. }
  39.  
  40. else {
  41. cout<<"Incorrect selection. Please select from one of the options above\n\n";
  42. system("pause");
  43. system("cls");
  44. goto outline;
  45. }
<< moderator edit: added [code][/code] tags >>





Thanks so much,
Scott
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: cscotty is an unknown quantity at this point 
Solved Threads: 0
cscotty cscotty is offline Offline
Newbie Poster

Re: Completed Code but A LOT of GOTO...Replacement?

 
0
  #8
Jun 3rd, 2005
wow...really having some trouble....can functions only be used for mathematical solutions. All the tutorials I am reading are aiming at addition, subtraction, and so on...
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,358
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Completed Code but A LOT of GOTO...Replacement?

 
0
  #9
Jun 3rd, 2005
This is a short example of what I was saying.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool q1()
  5. {
  6. static const char question[] = "What are the colors of the United States Flag?";
  7. static const char *answer[] =
  8. {
  9. "Black, Gold, Green",
  10. "Red, White",
  11. "Blue, Black, Red, White",
  12. "Red, White, Blue",
  13. };
  14. cout << question << "\n\n";
  15. for ( size_t i = 0; i < sizeof answer / sizeof *answer; ++i )
  16. {
  17. cout << i + 1 << ". " << answer[i] << '\n';
  18. }
  19. cout << "\nanswer? ";
  20. int response;
  21. cin >> response;
  22. cin.ignore();
  23. return response == 4;
  24. }
  25.  
  26. bool q2()
  27. {
  28. static const char question[] = "What do the stars of the flag mean?";
  29. static const char *answer[] =
  30. {
  31. "One for each State",
  32. "One for each President",
  33. "One for each Senator",
  34. "The amount of wars the USA won",
  35. };
  36. cout << '\n' << question << "\n\n";
  37. for ( size_t i = 0; i < sizeof answer / sizeof *answer; ++i )
  38. {
  39. cout << i + 1 << ". " << answer[i] << '\n';
  40. }
  41. cout << "\nanswer? ";
  42. int response;
  43. cin >> response;
  44. cin.ignore();
  45. return response == 1;
  46. }
  47.  
  48. int main()
  49. {
  50. while ( q1() == 0)
  51. {
  52. cout << "\nIncorrect, please try again.\n";
  53. }
  54. cout << "\nCorrect!\n";
  55. while ( q2() == 0)
  56. {
  57. cout << "\nIncorrect, please try again.\n";
  58. }
  59. cout << "\nCorrect!\n";
  60. return 0;
  61. }
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.
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Question
  5. {
  6. const char *question;
  7. const char *answer[4];
  8. int correct;
  9. };
  10.  
  11. static Question Quiz[] =
  12. {
  13. { // Question 1
  14. "What are the colors of the United States Flag?",
  15. {
  16. "Black, Gold, Green",
  17. "Red, White",
  18. "Blue, Black, Red, White",
  19. "Red, White, Blue",
  20. },
  21. 4
  22. },
  23. { // Question 2
  24. "What do the stars of the flag mean?",
  25. {
  26. "One for each State",
  27. "One for each President",
  28. "One for each Senator",
  29. "The amount of wars the USA won",
  30. },
  31. 1
  32. },
  33. };
  34.  
  35. bool ask(size_t j)
  36. {
  37. cout << Quiz[j].question << "\n\n";
  38. for ( size_t i = 0; i < sizeof Quiz[j].answer / sizeof *Quiz[j].answer; ++i )
  39. {
  40. cout << i + 1 << ". " << Quiz[j].answer[i] << '\n';
  41. }
  42. cout << "\nanswer? ";
  43. int response;
  44. cin >> response;
  45. cin.ignore();
  46. return response == Quiz[j].correct;
  47. }
  48.  
  49. int main()
  50. {
  51. for ( size_t i = 0; i < sizeof Quiz / sizeof *Quiz; ++i )
  52. {
  53. while ( ask(i) == 0 )
  54. {
  55. cout << "\nIncorrect, please try again.\n\n";
  56. }
  57. cout << "\nCorrect!\n";
  58. }
  59. return 0;
  60. }
"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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 12
Reputation: cscotty is an unknown quantity at this point 
Solved Threads: 0
cscotty cscotty is offline Offline
Newbie Poster

Re: Completed Code but A LOT of GOTO...Replacement?

 
0
  #10
Jun 6th, 2005
Thanks again for all your help. I think I'm going to try the switch though, until I am comfortable using Functions. Any chance you can show me with a switch? (Just one question will be fine)

Thanks,
Scott
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC