【发布时间】:2012-03-05 17:33:12
【问题描述】:
我只是 C++ 的初学者。我正在编写一个小而简单的程序,它在两个用户指定的整数之间打印一系列整数。
最后,如果用户返回 1,程序将重新运行 while 循环,但是当这种情况发生时,程序将不会再次打印这一系列数字(for 循环不起作用)。
这里是源代码:
int main(void)
{
int num1, num2;
int doContinue = 1;
while (doContinue == 1)
{
cout << "Please enter two integers, the first being the smallest: ";
do { //does everything in curly braces while the user inputs the numbers wrong...
cin >> num1 >> num2;
if (num1 > num2)
{
cout << "Your first number was bigger than the second.\nTry again!: ";
}
} while (num1 > num2);//... but once it's not wrong, break out of this do loop
//at this point the input has been checked, so we can proceed to print the series
for(int num1; num1 <= num2; num1++)
{
cout << num1 << " \n";
}
cout << "Would you like to compute another series of integers? 1=yes, anything else=no: ";
cin >> doContinue;
}
return 0;
}
【问题讨论】: