首页 > 解决方案 > C++ 只接受正整数

问题描述

我目前正在处理一项编程任务,并且无法检查用户放置的输入。该程序是您仅输入两个正数的地方,但是当我输入一个字符(例如“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;
}

标签: c++

解决方案


if (firstNum==x || secondNum==x)
    cout<< "\nError: Invalid number: Numbers must be positive integer.\n";

这个测试是错误的 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;
}

推荐阅读