简体   繁体   English

如何制作一个while循环但有更多条件C++

[英]How to make a while loop but with more conditions C++

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?那么我该如何缩短while条件呢?

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"编辑1:“我为我想做的事情添加了更多提示”

You can use the < and > operators for the range of '1' to '5' , but you'll have to handle the check for 'L' explicitly:您可以将<>运算符用于'1''5'的范围,但您必须明确处理对'L'的检查:

while (user_input2 != 'L' && (user_input2 < '1' || user_input2 > '5'))
{
    cout << "Invalid input";
}

You also can write:你也可以写:

while (user_input2 != '1' && user_input2 != '2' && user_input2 != '3' && user_input2 != 
'4' && user_input2 != '5')
{
    cout << "Invalid input"
}

How about a function where you provide all accepted characters as string? function 怎么样提供所有接受的字符作为字符串?

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 colirucoliru 现场演示

Btw.顺便提一句。 there is a standard C library function (adopted in C++ standard) which could be used as well:有一个标准的 C 库 function (在 C++ 标准中采用)也可以使用:

std::strchr()标准::strchr()

It returns a pointer to found character or nullptr and could be used similar like checkValid() in the above sample:它返回一个指向找到的字符或nullptr的指针,并且可以像上面示例中的checkValid()一样使用:

#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 colirucoliru 现场演示


Thinking twice (about OPs possible intention), I started to ask why the check is needed at all.三思而后行(关于 OP 可能的意图),我开始问为什么需要检查。 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.假设必须以某种方式处理有效输入(我会为此使用switch ),无效输入也可以被覆盖。

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: Output:

Change Name
Change Password
Invalid input!
Log out

Live Demo on colirucoliru 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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