c++ - 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;
}
解决方案
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;
}
推荐阅读
- scala - yyyy-MM-dd HH24:mm:ss 时间戳使用 Spark SQL
- cron - crontab:简单的作业没有运行
- .net - 无法在 docker 映像中运行 PlayWright
- c# - 应用服务 - 从字节 [] 创建 X509 证书时磁盘空间不足
- google-data-studio - Google Data Studio URL 参数 iframe embed - 在新标签页中打开时丢弃的 URL 参数
- windows-10 - USB A 到 A 连接 - 笔记本电脑可以看到 Surface 平板电脑,但 Surface 看不到笔记本电脑
- reactjs - 为什么 localhost 不再工作了?(Nextjs)
- huawei-mobile-services - 华为AppGallery中一款适用于移动设备和电视的应用程序
- python - 在 pyqt5 QTableWidget 中应用样式:已选择
- visual-studio-code - 重置 Microsoft 密码后无法连接到 Azure ML