简体   繁体   中英

(c++)Input needs to be a single character, but it accepts multiple characters and checks the first letter

I'm having a problem with a program I've built. It should take input from the user and check whether it's 'P' or 'M'. The problem is that I only want it to work if you enter 'P' or 'M', as it is now it accepts as 'M' anything you type as long as it starts with an 'M' (eg. if you type "morse" it will accept it as 'M'). I'm not a programmer and don't have much knowledge of c++, I just made it for fun. An example of how it is:

int main(){
  std::cout << "Enter 'M' or 'P'\n";
  char slction;
Inputrror:
  std::cin >> slction;
  switch (slction) {
  case 'M':
    goto Morse;
    break;
  case 'm':
    goto Morse;
    break;
  case 'P':
    goto Text;
    break;
  case 'p':
    goto Text;
    break;
  default:
    std::cout << "Please only enter 'M' or 'P'\n;
    goto Inputrror;
    break;
  }
Morse:
  std::cout << "Morse\n;"
  return 1;
Text:
  std::cout << "Text\n;"
  return 1;
}

EDIT: I tried to read the input as a string like it was suggested and it now works properly. The correct version:

int main() {
  std::cout << "Enter 'M' or 'P'\n";
  std::string slction;

Inputrror:
  std::cin >> slction;
  if (slction == "M" || slction == 'm') {
    goto Morse;
  }
  else if (slction == "P" || slction == 'p') {
    goto Text;
  }
  else {
    std::cout << "Please only enter 'P' or 'M'\n";
    goto Inputrror;
  }

Morse:
  std::cout << "Morse\n";
  return 1;

Text:
  std::cout << "Text\n";
  return 1;

}

One comment before I answer:

Instead of

case 'M':
  goto Morse;
  break;
case 'm':
  goto Morse;
  break;

you could use

case 'M':
case 'm':
  goto Morse;
  break;

break stops the block so as long as you don't use it you can nest one after another. You can even do stuff like:

case 'M':
  cout << "CAPITALIZED";
case 'm':
  goto Morse;
  break;

Now, to your question: you are reading a char , meaning it will only take the first letter you input. Use a string instead if you want to be able to read words too:

string slction;
cin >> slction;

PD: remember to change the case 'M' and other options' quotes to double quotes (for strings)

PD2: you can't use switch with strings, so you will have to use if/else blocks

With what was said in the first answer, additionally you could use #include <cctype> toupper() function to remove extra cases. As well as validate your input with if statements.

example validation function:

char isValid(char &selection){

    std::cin >> selection;
    selection = toupper(selection); // ctype.h for toupper changes all to uppercase characters

    //checks to see if more than 1 character is inputed
    if (std::cin.get() != '\n'){
            std::cin.ignore(256, '\n'); //ignores 256 chars until newline('\n')
            std::cin.clear(); // clears the input
            selection = '\0'; // sets selection to null
    }
    return selection;
}

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM