944,131 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4395
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 3rd, 2005
0

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

Expand Post »
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:


C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cscotty is offline Offline
12 posts
since Jun 2005
Jun 3rd, 2005
0

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

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 3rd, 2005
0

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

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.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EricaJanine is offline Offline
22 posts
since Jun 2005
Jun 3rd, 2005
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cscotty is offline Offline
12 posts
since Jun 2005
Jun 3rd, 2005
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EricaJanine is offline Offline
22 posts
since Jun 2005
Jun 3rd, 2005
0

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

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 3rd, 2005
0

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

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:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cscotty is offline Offline
12 posts
since Jun 2005
Jun 3rd, 2005
0

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

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cscotty is offline Offline
12 posts
since Jun 2005
Jun 3rd, 2005
0

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

This is a short example of what I was saying.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 6th, 2005
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cscotty is offline Offline
12 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: const
Next Thread in C++ Forum Timeline: Urgent help needed..plz someone help...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC