4
cout << "Options:\n1: Change Name\n2: Change Password\n3: Change Address\n4: Withraw\n5: Deposit\nL: Log out\n>";
while (user_input2 != '1' && user_input2 != '2' && user_input2 != '3' && user_input2 != '4' && user_input2 != '5' && user_input2 != 'L')
{
    cout << "Invalid input";
}

So how do I just shortened the while conditions?

I tried doing:

cout << "Options:\n1: Change Name\n2: Change Password\n3: Change Address\n4: Withraw\n5: Deposit\nL: Log out\n>";
while (user_input2 != '1','2','3','4','5','L')
{
    cout << "Invalid input";
}

but it doesn't work.

edit1: "I added more hints to what I wanted to do"

MJ DLS
  • 87
  • 7
  • 1
    The comma operator may compile but it will not do what you want. – drescherjm Nov 29 '20 at 23:44
  • 1
    I think you made a mistake in your first block code. By using "or" `||`, the condition will always be satisfied with any value `user_input` can take. I think you meant to use the "and" `&&` operator. – Patrick Nov 29 '20 at 23:45
  • This seems like a dupe https://stackoverflow.com/questions/61447353 though it uses `if` instead of `while`. – cigien Nov 29 '20 at 23:49
  • I just found an older duplicate but that one is better. [https://stackoverflow.com/questions/15181579/c-most-efficient-way-to-compare-a-variable-to-multiple-values](https://stackoverflow.com/questions/15181579/c-most-efficient-way-to-compare-a-variable-to-multiple-values) – drescherjm Nov 29 '20 at 23:51
  • @drescherjm That's perfect, thanks. – cigien Nov 29 '20 at 23:53
  • 1
    Are the values that you're comparing against always going to be chars? And will they be contiguous? – cigien Nov 30 '20 at 00:10
  • Yes they're chars – MJ DLS Nov 30 '20 at 02:06
  • Ok, and will they be contiguous, i.e. do you care about cases like `user_input2 != '1','3','5', '8'`? – cigien Nov 30 '20 at 02:25
  • Actually I don't care if the numbers are in order or not, these chars are just options plus the L which will be the quitting option to the while loop, I'm just wondering which is a way where the "user_input2 !=" condition to be implemented to all the chars I want without typing too much. Thank you in advance. :) – MJ DLS Nov 30 '20 at 05:28

3 Answers3

9

You can use the < and > operators for the range of '1' to '5', but you'll have to handle the check for 'L' explicitly:

while (user_input2 != 'L' && (user_input2 < '1' || user_input2 > '5'))
{
    cout << "Invalid input";
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    Maybe a short explanation about character encodings to help a beginner programmer? – Patrick Nov 29 '20 at 23:40
  • 3
    @Patrick — character encodings aren’t really involved here. In C++ and C the digits `[‘0’ .. ‘9’]` are required to be contiguous and increasing. So that test is valid for every character encoding that the compiler supports. – Pete Becker Nov 30 '20 at 01:56
  • it didn't work, the char inputs have an L in it. so the options are ('1','2','3','4','5','L') – MJ DLS Nov 30 '20 at 05:19
  • @MJDelosSantos A little more effort. The condition proposed here takes care of cases where `user_input2` is a digit between 1 and 5. If you have an extra valid character you can use this: `input < '1' || input > '5' || input == 'L'`. – Patrick Nov 30 '20 at 06:40
3

You also can write:

while (user_input2 != '1' && user_input2 != '2' && user_input2 != '3' && user_input2 != 
'4' && user_input2 != '5')
{
    cout << "Invalid input"
}
deepv2
  • 51
  • 10
  • This while conditions are just too long, I just want to shortened it to save my fingers and time typing it. – MJ DLS Nov 30 '20 at 02:07
2

How about a function where you provide all accepted characters as string?

Demo:

#include <iostream>

bool checkValid(char cChk, std::string_view allowed)
{
  for (char c : allowed) if (cChk == c) return true;
  return false;
}

int main()
{
  char user_input2;
  std::cin >> user_input2;
  if (!checkValid(user_input2, "12345L")) {
    std::cout << "Invalid input\n";
  }
}

Live Demo on coliru

Btw. there is a standard C library function (adopted in C++ standard) which could be used as well:

std::strchr()

It returns a pointer to found character or nullptr and could be used similar like checkValid() in the above sample:

#include <cstring>
#include <iostream>

int main()
{
  char user_input2;
  std::cin >> user_input2;
  if (!std::strchr("12345L", user_input2)) {
    std::cout << "Invalid input\n";
  }
}

Live Demo on coliru


Thinking twice (about OPs possible intention), I started to ask why the check is needed at all. Assuming that the valid input has to be processed somehow (I would use a switch for this), invalid input just could be covered as well.

Demo:

#include <iostream>

int main()
{
  for (char user_input2; std::cin >> user_input2;) {
    switch (user_input2) {
      case '1': std::cout << "Change Name\n"; break;
      case '2': std::cout << "Change Password\n"; break;
      case '3': std::cout << "Change Address\n"; break;
      case '4': std::cout << "Withraw\n"; break;
      case '5': std::cout << "Deposit\n"; break;
      case 'l': case 'L': std::cout << "Log out\n"; return 0;
      default: std::cerr << "Invalid input!\n";
    }
  }
}

Input:

12AL

Output:

Change Name
Change Password
Invalid input!
Log out

Live Demo on coliru

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56