构造 do while 循环以处理C++不起作用的帐号

Constructing do while loop to handle account numbers not working C++

本文关键字:C++ 不起作用 帐号 处理 do while 循环 构造      更新时间:2023-10-16

我正在尝试使用do while循环来评估帐号。有效的帐号必须有 5 位数字,并以字母 R 或 B 开头,不区分大小写。

有效帐号示例:R90000B10101编号: R88888b77777

无效帐号示例:Y90000r888822

这是我做的循环,我无法弄清楚我的参数出了什么问题,导致它一遍又一遍地重复,除了帐号之外。

char accountType;
int accountNum;
cout << "Please enter your account number." <<endl;
cout << ">> ";
cin >> accountType >> accountNum;
cout <<endl;
do 
{
    cout << "That is not a valid account number. Please enter your account number." <<endl;
    cout << ">> ";
    cin >> accountType >> accountNum;
    cout <<endl;
}while ( (accountNum <= 9999) || (accountNum >= 100000) && (accountType != 'r') || (accountType != 'R') || (accountType != 'b') || (accountType != 'B') );

有什么想法吗?

重复循环的条件是以下任何一项

  1. accountNum <= 9999
  2. accountNum >= 100000 && accountType != 'r'
  3. accountType != 'R'
  4. accountType != 'b'
  5. accountType != 'B'

但是对于每个字符,(3)、(4) 和 (5) 中至少有两个是正确的,所以你总是会重复。

您需要在所有accountType检查失败或任何accountNum检查失败时进行循环。也就是说,以下任何一项:

  1. accountNum <= 9999
  2. accountNum >= 100000
  3. accountType != 'r' && accountType != 'R' && accountType != 'b' && accountType != 'B'