-2

I've got some problems with this code:

void selectOption(){
    int choice; cin >> choice;
    string name, surname;
    switch(choice){
        case 1:
            showStudents();
            break;
        case 2:
            cout <<"Name: "; cin >> name;
            cout <<"Surname: "; cin >> surname;
            addStudent(name,surname);
            break;
        case 3:
            break;
        default:
            cout <<"DOES NOT SUPPORT" << endl;
            break;
    }
}

int main(){
    while(true){
        selectOption();
    }
}

The problem is, whenever I enter a string, the program goes into the default case instead of letting me enter a choice.

The second problem is, the program only works with 1, 2, and 3. If I write 5, nothing happens (it does not even go into the default case).

pere
  • 11
  • 3

3 Answers3

-1

You need to check that the value entered is an integer number before trying to do anything with it otherwise you may get some very nasty undefined behavior. There are many easy ways to do that, my personal preference is to use std::all_of from algorithm to check that every character in the input is a digit. This of course requires that you take the input as a std::string, like this

#include <iostream>
#include <string>
#include <algorithm>

void showStudents() {
  // Show students  
}
void addStudent(std::string name, std::string surname) {
  // Add student
}

void selectOption(){
  int choice = 0;
  std::string in = ""; 
  std::cout << "Choice: " << std::flush;
  std::cin >> in;
  if (std::all_of(in.begin(), in.end(), ::isdigit)) {
    choice = std::stoi(in);
  }
  std::string name, surname;
  switch(choice){
    case 1:
      showStudents();
      break;
    case 2:
      std::cout <<"Name: "; 
      std::cin >> name;
      std::cout <<"Surname: "; 
      std::cin >> surname;
      addStudent(name,surname);
      break;
    case 3:
      break;
    default:
      std::cout << "DOES NOT SUPPORT" << std::endl;
      return;
  }
}

int main() {
  while(true) {
    selectOption();
  }
}

Which will produce the behavior you are expecting - input any string and it will go to the default case, input a 1, 2, or 3 and it will execute that case. Example output:

Choice: string
DOES NOT SUPPORT
Choice: 1
Choice: 2
Name: Name
Surname: Surname
Choice: 3
Choice: 4
DOES NOT SUPPORT
Choice: 5
DOES NOT SUPPORT
Choice: t
DOES NOT SUPPORT
Choice:
William Miller
  • 8,060
  • 3
  • 13
  • 39
-1

This appears to work as is. What's the issue?

#include <iostream>
#include <string>

using namespace std;


void selectOption()
{
    int choice;
    string name, surname;

    cin >> choice;
    switch(choice)
    {
        case 1:
            cout << "showStudents()" << endl;
            break;
        case 2:
            cout <<"Name: "; cin >> name;
            cout <<"Surname: "; cin >> surname;
            cout << "addStudent(" << name << "," << surname << ")" << endl;
            break;
        case 3:
            break;
        default:
            cout <<"DOES NOT SUPPORT" << endl;
            break;
    }
}

int main(){
    while(true){
        selectOption();
    }
}

With output:

> 1 showStudents() 
> 2 
> Name: Rufus 
> Surname: VonWoodson
> addStudent(Rufus,VonWoodson) 
> 3 
> 4 
> DOES NOT SUPPORT 
> 5 
> DOES NOT SUPPORT 
> 6
> DOES NOT SUPPORT 
> ^C 
> Command terminated
> 
> Interrupt: Press ENTER or type command to continue
VoNWooDSoN
  • 900
  • 7
  • 12
  • 1
    The issue is when a `string` is entered instead of an `int` for `choice` – William Miller Dec 27 '18 at 22:17
  • I don't think I deserved the downvote though. I asked a question in response to the second issue " If I write 5, nothing happens" There's no clear part of the OP's statement that he enters a string instead of an int for that. – VoNWooDSoN Jan 02 '19 at 17:00
  • That part of the OP's question is likely the result of undefined behavior arising from using `choice` uninitialized - which is likely why it wasn't reproduced by your execution. When you ask what the issue is I was only trying to clarify that there is more undefined behavior when `cin` expects an `int` and gets a `string`. Perhaps my comment would have been more constructive had I stated explicitly that the issue was undefined behavior, but the downvote is appropriate because you provided an answer which ignored and preserved both instances of undefined behavior in the question. – William Miller Jan 02 '19 at 17:52
-2

In switch you want to test the condition for integer, if you will input string for the variable choice it will go to the default case(hope I understood the question)

tyMind
  • 33
  • 5
  • Okay, but it should break out of this case, and go once again into `selectOption` function and let me choice another case. `cin>>choice` is standing before `switch`. – pere Dec 27 '18 at 19:08
  • I don't understand, why selectOption function in main takes arguments if in the definition there are no parameters inside parentheses – tyMind Dec 27 '18 at 19:13
  • sorry for that, I have change some things before save and left that one 'file' in argument. Now its my exact problem. – pere Dec 27 '18 at 19:16
  • @pere Provide a [mcve] that reproduces your problem as mentioned please! – πάντα ῥεῖ Dec 27 '18 at 19:19