我有一個做while循環創建,它幾乎正是我想要的,但其中的一部分重複比我想要的更多。下面是代碼:do-while循環的一部分重複的次數超過了它應該的次數。我該如何解決?
#include <iostream>
#include <iomanip>
using namespace std;
int main() // none of the above is included in the pdf version of this file but I put it in anyway.
{
int count = -1, number = 1, product = 1;
do
{
++count;
product = product * number;
cout << "Enter an integer number to be included in the product "
<< endl << "or enter 0 to end the input: "; // I put endl in this weird position just because I found it more readable lined up like this that is all.
cin >> number;
}
while (number != 0);
if (count > 0)
{
cout << endl << "The product is " << product << "." << endl;
}
}
的問題是,「輸入一個整數被包含在產品或輸入0結束輸入I輸入號碼後重復它仍然給了我正確的答案。但是當我不想要它的時候,cout仍然會重複,我試着修復了很多不同的方法(注意:我對編程非常陌生),但是每一個都會讓程序不再起作用,或者不能在第一次修復它
該評論對象是我的老師,非常抱歉,如果這不是實際的代碼,這是我第一次在這裏發表任何東西
適用於我 - 您使用什麼輸入?你看到了什麼輸出?請注意,如果輸入任何無效(即不是整數),您的程序將永久循環。 – 2014-10-31 04:20:53
爲什麼整個「輸入0結束輸入」的東西,如果你不想重複? – 2014-10-31 05:56:38