我正在编写这个代码用于家庭作业(刚开始使用C++,所以请放轻松).我们刚刚开始的同时,今天和今天的循环.该程序运行正常,但如果您在程序要求输入整数时输入一个字母,它会无限循环.到底是怎么回事?(以下代码)***编辑:为了澄清,循环的部分是:"您输入的数字是负数.请输入正数以继续." 但是用户没有机会输入另一个号码.它只是继续打印这个.
#include <iostream>
using namespace std;
int main ( )
{
//define variables
int num1, num2, total;
char answer1;
do
{
//user enters a number
cout << "\nPlease enter a positive number and press Enter: \n";
cin >> num1;
//check that the given num1 value is positive
while (num1 < 0)
{
cout << "The number you entered is negative.\nPlease enter a positive number to continue.\n";
cin >> num1;
}
cout << endl;
//add the sum of 1 through …Run Code Online (Sandbox Code Playgroud)