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

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.

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.

switch(qValue)
{
case q1:
   code;
   code;
   break;
case q2:
   ...
case qError:
   ...
default:
   ...
}

<< 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

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

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?

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.

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:

//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;
    }

<< moderator edit: added [code][/code] tags >>

:)

Thanks so much,
Scott

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...

This is a short example of what I was saying.

#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;
}

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.

#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;
}

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

I'm working on it....post will be soon....

Scott

While many say GOTO is bad I happen to believe its very handy. 20 is quite a lot though. A few goto's is ok - but 20 may be getting too much. Try replacing the with if/else.

Hey, [edit] lol wow just realized this is over 5 year old thread :P [/edit]

i was looking for an example of the goto command and came across this post.

firstly the way I interpret the switch is as a neat way to write a long chain of if else ifs. so..

int i;

cin>>i;

//a chain of if else if

if(i==0){
   cout<<"i is 0\n";
}else if(i==1){
   cout<<"i is 1\n";
}else if(i==2){
   cout<<"i is 2\n";
else{
   cout<<"i is not 0 1 or 2\n";
}

//now as a case

switch(i){

case(0): cout<<"i is 0\n"; 
         break;
case(1): cout<<"i is 1\n";
         break;
case(2): cout<<"i is 1\n";
         break;
default: cout<<"i is not 0 1 or 2\n";

in your situation i would say functions are the way to go, and Dave is showing you something you want to strive towards, Abstraction, which is nice. :)

now functions work a little different and not only for math, math is easy way to show how a function works, but a function needs a couple things to set it up..

here is a great reference for C++ stuff
http://www.cplusplus.com/doc/tutorial/functions/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.