[英]C++: Trouble with ending a loop based on a condition?
該代碼用於運行三次循環,該循環將收集的卵數取走並輸出數十個或更多的卵,直到用戶輸入負數為止。 然后,它打印出收集(輸入)的雞蛋的平均數量,並輸出幾十個及更多的雞蛋總數。
我們分配給使用的輸入是:43、31,-1、24、8、14,-999,-5。
一切正常,直到我們輸入-5。 我們的老師不希望打印幾十個附加項目的平均值或總數(您會在輸出中看到我的意思)。
源代碼如下:
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
int eggNum;
int eggDozens;
int eggExtra;
int eggTotal;
int loopCount;
int forCount;
float eggAvg;
int totalDozens;
int totalExtra;
for(forCount = 1; forCount <= 3; forCount=forCount + 1)
{
cout << left << "TEST #" << forCount << ":" << endl;
cout << "Welcome to Aunt Ellen\'s eggs to dozens converter!";
cout << endl << endl;
cout << "\tEnter the number of eggs gathered: ";
cin >> eggNum;
eggTotal = 0;
loopCount = 0;
while(eggNum >= 0)
{
eggDozens = eggNum / 12;
eggExtra = eggNum % 12;
if(eggDozens != 0)
{
if(eggExtra != 0)
{
cout << "\tYou have " << eggDozens << " dozen ";
cout << eggExtra << " eggs.";
cout << endl << endl;
}
else
{
cout << "\tYou have " << eggDozens << " dozen eggs.";
cout << endl << endl;
}
}
else
{
cout << "\tYou have " << eggExtra << " eggs.";
cout << endl << endl;
}
loopCount = loopCount + 1;
eggTotal = eggTotal + eggNum;
cout << "\tEnter the number of eggs gathered: ";
cin >> eggNum;
}
cout << endl << "TOTALS:" << endl;
eggAvg = eggTotal / float(loopCount);
cout << "\tOn average " << eggAvg << " eggs have been";
cout << " gathered.";
totalDozens = eggTotal / 12;
totalExtra = eggTotal % 12;
cout << endl << "\tA total of " << totalDozens << " dozen ";
cout << totalExtra << " and eggs have been gathered!" << endl;
cout << endl << endl;
}
return 0;
}
並輸出:
TEST #1:
Welcome to Aunt Ellen's eggs to dozens converter!
Enter the number of eggs gathered: 43
You have 3 dozen 7 eggs.
Enter the number of eggs gathered: 31
You have 2 dozen 7 eggs.
Enter the number of eggs gathered: -1
TOTALS:
On average 37 eggs have been gathered.
A total of 6 dozen 2 and eggs have been gathered!
TEST #2:
Welcome to Aunt Ellen's eggs to dozens converter!
Enter the number of eggs gathered: 24
You have 2 dozen eggs.
Enter the number of eggs gathered: 8
You have 8 eggs.
Enter the number of eggs gathered: 14
You have 1 dozen 2 eggs.
Enter the number of eggs gathered: -999
TOTALS:
On average 15.3333 eggs have been gathered.
A total of 3 dozen 10 and eggs have been gathered!
TEST #3:
Welcome to Aunt Ellen's eggs to dozens converter!
Enter the number of eggs gathered: -5
TOTALS:
On average -1.#IND eggs have been gathered.
A total of 0 dozen 0 and eggs have been gathered!
我不想要最后一個“ TOTALS”和以下各行。 我希望程序在輸入-5后終止。
最簡單的事情是在進入while循環之前執行此操作:
cin >> eggNum;
if (eggNum < 0)
break ;
那將退出for
循環,並return 0;
如果需要,可以在調用break
之前向調用者添加一些有關輸入負數的注釋。
您提到您只想省略TOTALS
的最后一塊。
您可以簡單地添加一個特殊情況以在這種情況下盡早離開外循環。
就在此塊之前,但在 while
循環的右括號之后 。
cout << endl << "TOTALS:" << endl;
eggAvg = eggTotal / float(loopCount);
插入此:
if (forCount == 3) break;
如果您只想避免在平均值小於0
打印,則應在同一位置插入。
if (eggAvg < 0) continue;
這將跳過for循環的其余迭代。
我認為對您的問題的一個簡單答案是僅在打印總代碼周圍放置一個if語句。 像這樣:
if(eggNum > -5){ //won't print for negative 5
cout << endl << "TOTALS:" << endl;
eggAvg = eggTotal / float(loopCount);
cout << "\tOn average " << eggAvg << " eggs have been";
cout << " gathered.";
totalDozens = eggTotal / 12;
totalExtra = eggTotal % 12;
cout << endl << "\tA total of " << totalDozens << " dozen ";
cout << totalExtra << " and eggs have been gathered!" << endl;
cout << endl << endl;
}
我希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.