Jump to content

Ok, I'm using a switch but when running it, it shows all of the options, regardless of what's chosen.

 

Code:

 

 do{

    cout << "1. Show information on all letters." << endl;

    cout << "2. Show information on one letter." << endl;

    cout << "3. Quit the program." << endl;

    cout << "Enter your choice here: " << endl;

    cin >> num;

    switch(num){

    case 1: printInfoAllLetters(ar);

    case 2:

      cout << "Which letter are you interested in?: ";

      cin >> letter;

      printInfoOneLetter(letter,ar);

    case 3: cout << "Thank you for using the system. Good bye." << endl;

    default: cout <<"Invalid choice. Please try again." << endl;

    }

  }while(num <1 || num>3);

 

  return 0;

}

 

Output:

 

2. Show information on one letter.

3. Quit the program.

Enter your choice here: 

1

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

B 0  - 

Which letter are you interested in?: S

S 0  - 

Thank you for using the system. Good bye.

Invalid choice. Please try again.

[-username-]$ ./a.out

1. Show information on all letters.

2. Show information on one letter.

3. Quit the program.

Enter your choice here: 

2

Which letter are you interested in?: s

s 8  - xxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Thank you for using the system. Good bye.

Invalid choice. Please try again.

[-username-]$ ./a.out

1. Show information on all letters.

2. Show information on one letter.

3. Quit the program.

Enter your choice here: 

3

Thank you for using the system. Good bye.

Invalid choice. Please try again.

 

 

 

So that's what happening. Any help?

My arsenal: i7-9700k Gaming Rig, an iPhone, and Stupidity.

Link to comment
https://linustechtips.com/topic/365675-urgent-need-help-with-a-switch-c/
Share on other sites

Link to post
Share on other sites

Add break after each case.

case 1: printInfoAllLetters(ar); break;    case 2:      cout << "Which letter are you interested in?: ";      cin >> letter;      printInfoOneLetter(letter,ar);    break;    case 3: cout << "Thank you for using the system. Good bye." << endl; break;    default: cout <<"Invalid choice. Please try again." << endl;

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

 

Add break after each case.

case 1: printInfoAllLetters(ar); break;    case 2:      cout << "Which letter are you interested in?: ";      cin >> letter;      printInfoOneLetter(letter,ar);    break;    case 3: cout << "Thank you for using the system. Good bye." << endl; break;    default: cout <<"Invalid choice. Please try again." << endl;

I'll try this. Thanks!

My arsenal: i7-9700k Gaming Rig, an iPhone, and Stupidity.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×