简体   繁体   English

c++ 任务在 while 循环后不会执行

[英]c++ task won't execute after while loop

C++ newbie here. C++ 新手在这里。 I'm not sure how to describe this but the task outside of the while-loop won't execute immediately.我不确定如何描述这一点,但while-loop之外的任务不会立即执行。 I need to enter the input value again to get it done.我需要再次输入输入值才能完成。 Here is my code:这是我的代码:

#include <iostream>
using namespace std;

int main()
{

    int fourDigitInt, firstDigit, secondDigit, thirdDigit, fourthDigit, i = 0;

    cout << "Enter a 4-digit integer  :  ";
 
    cin >> fourDigitInt;


    while (fourDigitInt > 9999 || !(cin >> fourDigitInt))
    {

        i++;
        if (i >= 3)
        {
            cout << "You do not seem to understand the program instruction.\nPlease try again later." << endl;
            return -1;
        }
        else
        {

            cout << "Error: Please make sure you are entering a 4-digit integer" << endl;
            cin.clear();
            cin.ignore(4, '\n');
            cout << "Enter a 4-digit integer  :  ";
        }
    }

    firstDigit = fourDigitInt / 1000 % 10;

    secondDigit = fourDigitInt / 100 % 10;

    thirdDigit = fourDigitInt / 10 % 10;

    fourthDigit = fourDigitInt / 1 % 10;

    cout << "1st digit  :  " << firstDigit << endl;
    cout << "2nd digit  :  " << secondDigit << endl;
    cout << "3rd digit  :  " << thirdDigit << endl;
    cout << "4th digit  :  " << fourthDigit << endl;
}

Here are some problems I encountered:以下是我遇到的一些问题:

1)If I enter a string first, it doesn't have any problem. 1)如果我先输入一个string ,它没有任何问题。 2)But if I enter any number less than 9999, it won't execute the calculation unless I enter it again. 2)但是如果我输入任何小于9999的数字,除非我再次输入,否则它不会执行计算。 3)If I enter a 5-digit number the endl won't work. 3)如果我输入一个 5 位数字, endl将不起作用。 It will just display Enter a 4-digit integer: Error: Please make sure you are entering a 4-digit integer which suppose to be a different line.它只会显示Enter a 4-digit integer: Error: Please make sure you are entering a 4-digit integer ,这应该是不同的行。

Where exactly did I do wrong?我到底哪里做错了? Thank you in advance.先感谢您。

The main issue here is in the while loop condition.这里的主要问题是while循环条件。 It should just check for the value of the fourDigitInt variable as that is what is important.它应该只检查fourDigitInt变量的值,因为这很重要。

Looking closer, you will also be able to notice the fact that the if case inside of the loop would check for the second itteration instead of the third.仔细观察,您还可以注意到循环内的if case 将检查第二次迭代而不是第三次的事实。 I fixed that as well by moving the i++ inside of the else block.我也通过在else块中移动i++来解决这个问题。

while (fourDigitInt > 9999 || fourDigitInt < 1000){ 
    if (i >= 3){ 
        cout << "You do not seem to understand the program (ironic coming for OC) instruction.\nPlease try again later." << endl;
        return -1;
    }
    else {
        i++;
        cout << "Error: Please make sure you are entering a 4-digit integer" << endl;
        cout << "Enter a 4-digit integer  :  ";
        cin >> fourDigitInt;
    }
}

Any other issues with your code that may occur are not related to this question.您的代码可能出现的任何其他问题与此问题无关。

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

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