[英]c++ How do i end a while loop that add numbers without using a number as a trigger to end the loop
我正在嘗試編寫一個程序,該程序可以連續加數字,直到用戶輸入單詞或其他東西停止程序為止。 目前,我使用-9999,但是如果用戶要添加-9999怎么辦? 我是新手,請有人幫忙。
float Addition()
{
Sum = 0;
cout << "Please enter number you wish to add:" << endl;
cin >> num;
while(num != -9999)
{
Sum += num;
cout << "Sum is:" << Sum << endl;
cout << "Please enter number or enter -9999 to exit" << endl;
cin >> num;
if(!cin.good())
{
throw exception();
}
}
}
通常的方法是將循環更改為:
while (cin >> num)
這將繼續提示輸入,直到顯示文件狀態為止,具體取決於您的操作系統,通過鍵入Ctrl-D或Ctrl-Z發出信號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.