//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
int total; // 全部的組合
int current; // 使用者現在輸入的數值
int counter; // 迴圈計數器
int main(int argc, char* argv[])
{
total = 0;
counter = 0;
while (counter < 5){
std::cout << "Number? ";
std::cin >> current;
total += current;
++counter;
}
std::cout << "The grand total is " << total << '\n';
std::system("pause");
return 0;
}
//---------------------------------------------------------------------------
這個程式可用 for 敘述重寫,結果如範例 8-2。
題目來源:C++風格與藝術 第二版 Steve Oualline 著 黃吉霈 編譯
留言列表