简体   繁体   English

构造do while循环以处理无法使用C ++的帐号

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

I'm trying to use a do while loop to evaluate account numbers. 我正在尝试使用do while循环来评估帐号。 A valid account number has to have 5 digits and start with the letters R or B, not case sensitive. 有效的帐号必须有5位数字,并且以字母R或B开头,不区分大小写。

Valid account number examples: r90000 B10101 R88888 b77777 有效的帐号示例:r90000 B10101 R88888 b77777

invalid account number examples: y90000 r888822 无效的帐号示例:y90000 r888822

This is the loop I've made, and I can't figure out what's wrong with my parameters that's causing it to repeat over and over again, never excepting an account number. 这是我做过的循环,我无法弄清楚我的参数出了什么问题,导致它一遍又一遍地重复,从来没有一个帐号。

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') );

Any ideas? 有任何想法吗?

Your conditions for repeating the loop are any of: 您的重复循环条件是任何的:

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

But for every character, at least two of (3), (4), and (5) are true, so you'll always repeat. 但是对于每个字符,(3),(4)和(5)中至少有两个是正确的,因此您将始终重复。

You need to loop while all of the accountType checks fail OR any of the accountNum checks fail. 您需要在所有 accountType检查失败或任何accountNum检查失败时进行循环。 That is, any of: 即,以下任何一项:

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

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

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