Jump to content

Help to avoid using a Goto

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
norbie

norbie

    Newbie

  • Members
  • PipPip
  • 12 posts
Hello,

I'm making a program here, and it's all functioning but I have had to use a Goto in order to achieve what I wanted. It's for my college homework and I may get marked down on it, so I'd like to avoid using it if possible.

Here is my code at the moment:

          cout << "\nPlease enter the altitude you will be flying at\n";

          cout<<"\nChoose 1 for     0 ft (Sea Level)\n";

          cout<<"Choose 2 for  5000 ft\n";

          cout<<"Choose 3 for 10000 ft\n";

          cout<<"Choose 4 for 15000 ft\n";

          cout<<"Choose 5 for 20000 ft\n";

          cout<<"Choose 6 for 25000 ft\n";

          cout<<"Choose 7 for 30000 ft\n";

          cout<<"Choose 8 for 35000 ft\n";

          cout<<"Choose 9 for 40000 ft\n";

          cout<<"\n";

          cin>>i;

    

          switch(i)

                   {

                   case 1:(Alt=0);break;

                   case 2:(Alt=5000);break;

                   case 3:(Alt=10000);break;

                   case 4:(Alt=15000);break;

                   case 5:(Alt=20000);break;

                   case 6:(Alt=25000);break;

                   case 7:(Alt=30000);break;

                   case 8:(Alt=35000);break;

                   case 9:(Alt=40000);break;

                   default:cout<<"ERROR - Please choose a number from the list\n";

                   

                   

                   * INSERT SOMETHING HERE THAT SENDS PEOPLE UP TO THE TOP *

                   

                   

                   }

The idea is that if someone does not enter one of the numbers displayed for the switch/case statement, they will be taken back up the top again so they can retry it.

A goto works fine, but as I said I would like to get around this.
Any of you helpful people got a suggestion? Please post a code example if possible rather than just saying "do a loop" as I'm not too good on this lol.

Thankyou and Merry Xmas!

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I think a loop would work best. I tested this on Linux using G++.

          int i;
          int Alt=-1;
       
      while (Alt==-1) {
          cout << "\nPlease enter the altitude you will be flying at\n";
          cout<<"\nChoose 1 for     0 ft (Sea Level)\n";
          cout<<"Choose 2 for  5000 ft\n";
          cout<<"Choose 3 for 10000 ft\n";
          cout<<"Choose 4 for 15000 ft\n";
          cout<<"Choose 5 for 20000 ft\n";
          cout<<"Choose 6 for 25000 ft\n";
          cout<<"Choose 7 for 30000 ft\n";
          cout<<"Choose 8 for 35000 ft\n";
          cout<<"Choose 9 for 40000 ft\n";
          cout<<"\n";
          cin>>i;
    
          switch(i)
                   {
                   case 1:(Alt=0);break;
                   case 2:(Alt=5000);break;
                   case 3:(Alt=10000);break;
                   case 4:(Alt=15000);break;
                   case 5:(Alt=20000);break;
                   case 6:(Alt=25000);break;
                   case 7:(Alt=30000);break;
                   case 8:(Alt=35000);break;
                   case 9:(Alt=40000);break;
                   default:cout<<"ERROR - Please choose a number from the list\n";

                   }
        }

Attached Files



#3
norbie

norbie

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi Jordan,

Thanks for your reply.

I tried that but it wouldn't accept the code. Perhaps it's because the whole of that code is already running in a while loop and you cant "nest" one inside another?

Here is the whole module this code is from:

// Module to input the altitude to be flown at

int GetAlt ()

{

    int Alt, i, Choice;

    

    // While statement for validation

    // The user can re-enter altitude if they have selected the wrong choice

    while (Choice > 1) {


          cout << "\nPlease enter the altitude you will be flying at\n";

          cout<<"\nChoose 1 for     0 ft (Sea Level)\n";

          cout<<"Choose 2 for  5000 ft\n";

          cout<<"Choose 3 for 10000 ft\n";

          cout<<"Choose 4 for 15000 ft\n";

          cout<<"Choose 5 for 20000 ft\n";

          cout<<"Choose 6 for 25000 ft\n";

          cout<<"Choose 7 for 30000 ft\n";

          cout<<"Choose 8 for 35000 ft\n";

          cout<<"Choose 9 for 40000 ft\n";

          cout<<"\n";

          cin>>i;

    

          switch(i)

                   {

                   case 1:(Alt=0);break;

                   case 2:(Alt=5000);break;

                   case 3:(Alt=10000);break;

                   case 4:(Alt=15000);break;

                   case 5:(Alt=20000);break;

                   case 6:(Alt=25000);break;

                   case 7:(Alt=30000);break;

                   case 8:(Alt=35000);break;

                   case 9:(Alt=40000);break;

                   default:cout<<"ERROR - Please choose a number from the list\n";

                   }

                   

          cout << "You have selected " << Alt << " ft\n";

          cout << "\nIf that is correct press 1, if you wish to enter Altitude again press 2\n";

          cin >> Choice;

    }

    // End of while statement

          

    return Alt;

}

So I have one while statement there already, which is used at the end to print out the user's selection and check that it is correct (Verification).

I would still like to add some validation there so if they enter "33" in the select/case part it gives that error I have in there, and sends them to the top again so they can re-enter a valid number.

#4
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Change your code to look like this:

int GetAlt ()
{
    int Alt=-1;
    int i, Choice;
    
    // While statement for validation
    // The user can re-enter altitude if they have selected the wrong choice
    while (Choice > 1 && Alt==-1) {

Should work fine although I didn't test it.