C++只接受正整数

C++只接受正整数,c++,C++,我目前正在进行一项编程作业,在检查用户输入时遇到问题。该程序只输入两个正数,但当我输入一个字符(例如“a”)作为第一个数字时,程序会接受它并输出它,就像我输入了零一样。它应该输出无效的数字:数字必须是正整数。有人能告诉我我做错了什么吗?谢谢大家! //Program where user enters two positive numbers //and program will display various things. #include <iostream> using n

我目前正在进行一项编程作业,在检查用户输入时遇到问题。该程序只输入两个正数,但当我输入一个字符(例如“a”)作为第一个数字时,程序会接受它并输出它,就像我输入了零一样。它应该输出无效的数字:数字必须是正整数。有人能告诉我我做错了什么吗?谢谢大家!

//Program where user enters two positive numbers
//and program will display various things.

#include <iostream>
using namespace std;

int main()
{
//Displays information of what program will do
cout<< "Practice with iterations\n\n"
    << "The function of this program is, given 2 positive numbers, the"
    << " program";
cout<< "\nwill display the following\n\n";

cout<< "\t1. All even numbers between firstNum and secondNum.\n"
    << "\t2. All odd numbers between firstNum and secondNum.\n"
    << "\t3. Sum of all even numbers between firstNum and secondNum.\n"
    << "\t4. Sum of all odd numbers between firstNum and secondNum.\n"
    << "\t5. All prime numbers between firstNum and secondNum.\n"
    << "\t6. Factorial of the secondNum.\n"
    << "\t7. The numbers and their squares between firstNum and "
    << "secondNum."<< endl;

//Declare first and second number variables
int firstNum;
int secondNum;
bool flag= true;    //Set to true
char x;             //Use to see if value entered is letter

//Ask user to input values
cout<< "\n\nEnter the first number:\t\t";
cin>> firstNum;

if (cin.fail())
{
    cin.clear();
    cin.ignore(256,'\n');
    flag= 0;
}

cout<< "Enter the second number:\t";
cin>> secondNum;

if (cin.fail())
{
    cin.clear();
    cin.ignore(256,'\n');
    flag= 0;
}

//If user puts wrong input
if (firstNum>secondNum)
    cout<< "\nError: First number must be < second number.\n";
else if (firstNum<0 || secondNum<0)
    cout<< "\nError: Invalid number: Number must be positive.\n";
else if (firstNum==x || secondNum==x)
    cout<< "\nError: Invalid number: Numbers must be positive integer.\n";
else
{
    cout<< "\nYou entered: "<< firstNum<< " and "<< secondNum;
}

return 0;
}
这个测试是错误的,x没有初始化,它没有真正的意义。另外,你已经使用了这个标志来测试失败输入的情况,我假设你的代码应该是这样的

#include <iostream>
using namespace std;

int main()
{
    //Displays information of what program will do
    cout << "Practice with iterations\n\n"
        << "The function of this program is, given 2 positive numbers, the"
        << " program";
    cout << "\nwill display the following\n\n";

    cout << "\t1. All even numbers between firstNum and secondNum.\n"
        << "\t2. All odd numbers between firstNum and secondNum.\n"
        << "\t3. Sum of all even numbers between firstNum and secondNum.\n"
        << "\t4. Sum of all odd numbers between firstNum and secondNum.\n"
        << "\t5. All prime numbers between firstNum and secondNum.\n"
        << "\t6. Factorial of the secondNum.\n"
        << "\t7. The numbers and their squares between firstNum and "
        << "secondNum." << endl;

    //Declare first and second number variables
    int firstNum;
    int secondNum;
    bool flag = true;    //Set to true

                        //Ask user to input values
    cout << "\n\nEnter the first number:\t\t";
    cin >> firstNum;

    if (cin.fail())
    {
        cin.clear();
        cin.ignore(256, '\n');
        flag = 0;
    }

    cout << "Enter the second number:\t";
    cin >> secondNum;

    if (cin.fail())
    {
        cout << "lol" << endl;
        cin.clear();
        cin.ignore(256, '\n');
        flag = 0;
    }

    if (flag) {
        if (firstNum > secondNum)
            cout << "\nError: First number must be < second number.\n";
        else if (firstNum < 0 || secondNum < 0)
            cout << "\nError: Invalid number: Number must be positive.\n";

        else
        {
            cout << "\nYou entered: " << firstNum << " and " << secondNum;
        }
    }
    else cout << "Error input" << endl;
    return 0;
}

如果在读取输入时出错,那么您已经正确地解释了第一步-清除输入流并清除cin的错误状态。您缺少的是在变量中读取有效的内容。为此,您需要一个循环

while (true )
{
   //Ask user to input values
   cout<< "\n\nEnter the first number:\t\t";

   // Try to read the input. If it is successful, break out of the loop.
   if ( cin>> firstNum )
   {
      break;
   }

   // Clear the error flag. Clear the input stream. Try again.
   cin.clear();
   cin.ignore(256,'\n');
}
对第二个数字执行相同的操作

while (true )
{
   //Ask user to input values
   cout<< "\n\nEnter the first number:\t\t";

   // Try to read the input. If it is successful, break out of the loop.
   if ( cin>> firstNum )
   {
      break;
   }

   // Clear the error flag. Clear the input stream. Try again.
   cin.clear();
   cin.ignore(256,'\n');
}