我正在研究一个项目,并编写了代码,用于确定一天中花了多少钱 - 唯一的问题是我需要这样做才能输出第1天的总数,然后第2天开始(只输出第2天总计),然后第3天开始(仅输出第2天总计),然后在所有3天结束后,输出3天的总数。我会粘贴我的代码,但由于某种原因格式化了。谢谢!
void drillDay ()
{
int dayCounter = 1;
while (dayCounter > 0)
{
//Enlisted Soldiers, total number of Soldiers who attend
int privatesRankOneTotal = 0;
int privatesRankTwoTotal = 0;
int privatesRankThreeTotal = 0;
int specialistsCorporalsTotal = 0;
int sergeantsTotal = 0;
int staffSergeantsTotal = 0;
int pltSergeantsTotal = 0;
int masterSergeantsTotal = 0;
//Warrant Officers, total number of Warrant Officers who attend
int chiefWarrantRankOneTotal = 0;
int chiefWarrantRankTwoTotal = 0;
int chiefWarrantRankThreeTotal = 0;
//Officers, total number of officers who attend
int ltRankTwoTotal = 0;
int ltRankOneTotal = 0;
int captainsTotal = 0;
//How much each soldier is paid per day, based on rank.
double pvtOnePay = 99.40;
double pvtTwoPay = 111.42;
double pfcPay = 117.16;
double SpcCplPay = 136.42;
double sgtPay = 165.84;
double ssgPay = 229.12;
double sfcPay = 239.32;
double msgPay = 309.44;
double cwoOnePay = 233.40;
double cwoTwoPay = 274.92;
double cwoThreePay = 336.04;
double ltTwoPay = 188.56;
double ltOnePay = 294.96;
double cptPay = 335.40;
cout<<endl<<"Enter amount of enlisted soldiers:"<<endl<<endl;
cout<<"How many Privates (E-1) / (PVT) are attending drill today? "<<endl;
cin>>privatesRankOneTotal;
cout<<"How many Privates (E-2) / (PV2) are attending drill today? "<<endl;
cin>>privatesRankTwoTotal;
cout<<"How many Privates (E-3) / (PFC) are attending drill today? "<<endl;
cin>>privatesRankThreeTotal;
cout<<"How many Specialists / Corporals (E-4) / (SPC / CPL) are attending drill today? ";
cin>>specialistsCorporalsTotal;
cout<<"How many Sergeants (E-5) / (SGT) are attending drill today? "<<endl;
cin>>sergeantsTotal;
cout<<"How many Staff Sergeants (E-6) / (SSG) are attending drill today? "<<endl;
cin>>staffSergeantsTotal;
cout<<"How many Platoon Sergeants (E-7) / (SFC) are attending drill today? "<<endl;
cin>>pltSergeantsTotal;
cout<<"How many soldiers with the paygrade of (E-8) are attending drill today? "<<endl;
cin>>masterSergeantsTotal;
cout<<endl<<endl<<endl;
cout<<"Enter amount of Warrant Officers:"<<endl<<endl;
cout<<"How many Warrant Officers (W-1) / (WO1) are attending drill today? "<<endl;
cin>>chiefWarrantRankOneTotal;
cout<<"How many Warrant Officers (W-2) / (CW2) are attending drill today? "<<endl;
cin>>chiefWarrantRankTwoTotal;
cout<<"How many Warrant Officers (W-3) / (CW3) are attending drill today? "<<endl;
cin>>chiefWarrantRankThreeTotal;
cout<<endl<<endl<<endl;
cout<<"Enter amount of Officers:"<<endl<<endl;
cout<<"How many 2nd Lieutenants (O-1) / (2LT) are attending drill today? "<<endl;
cin>>ltRankOneTotal;
cout<<"How many 1st Lieutenants (O-2) / (1LT) are attending drill today? "<<endl;
cin>>ltRankTwoTotal;
cout<<"How many Officers with paygrade (O-3) / Rank (CPT) are attending drill today? "<<endl;
cin>>captainsTotal;
cout<<endl<<endl<<endl;
int totalEnlistedSoldiers = privatesRankOneTotal + privatesRankTwoTotal + privatesRankThreeTotal + specialistsCorporalsTotal + sergeantsTotal + staffSergeantsTotal + pltSergeantsTotal + masterSergeantsTotal;
cout<<"Total enlisted soldiers at drill today: "<<totalEnlistedSoldiers<<endl<<endl;
int totalWarrantOfficers = chiefWarrantRankOneTotal + chiefWarrantRankTwoTotal + chiefWarrantRankThreeTotal;
cout<<"Total Warrant Officers at drill today: "<<totalWarrantOfficers<<endl<<endl;
int totalOfficers = ltRankOneTotal + ltRankTwoTotal + captainsTotal;
cout<<"Total Officers at drill today: "<<totalOfficers<<endl<<endl;
int totalSoldiers = totalEnlistedSoldiers + totalWarrantOfficers + totalOfficers;
cout<<"Total soldiers (Enlisted, Warrant Officers, and Officers) at drill today: "<<totalSoldiers<<endl;
double privateOneTotalPay = privatesRankOneTotal*pvtOnePay;
double privateTwoTotalPay = privatesRankTwoTotal*pvtTwoPay;
double privateThreeTotalPay = privatesRankThreeTotal*pfcPay;
double spcCplTotalPay = specialistsCorporalsTotal*SpcCplPay;
double sgtTotalPay = sergeantsTotal*sgtPay;
double ssgTotalPay = staffSergeantsTotal*ssgPay;
double sfcTotalPay = pltSergeantsTotal*sfcPay;
double msgTotalPay = masterSergeantsTotal*msgPay;
double cwoOneTotalPay = chiefWarrantRankOneTotal*cwoOnePay;
double cwoTwoTotalPay = chiefWarrantRankTwoTotal*cwoTwoPay;
double cwoThreeTotalPay = chiefWarrantRankThreeTotal*cwoThreePay;
double ltOneTotalPay = ltRankTwoTotal*ltTwoPay;
double ltTwoTotalPay = ltRankOneTotal*ltOnePay;
double cptTotalPay = captainsTotal*cptPay;
double totalSoldiersDrillPay = privateOneTotalPay + privateTwoTotalPay + privateThreeTotalPay + spcCplTotalPay + sgtTotalPay + ssgTotalPay + sfcTotalPay + msgTotalPay + cwoOnePay + cwoTwoPay + cwoThreePay + ltOnePay + ltTwoPay + cptPay;
cout<<endl<<endl<<endl;
double costOfLunch = 14.99;
int soldiersProvidedLunch = 0;
int loopCounter = 1;
cout<<"How many soldiers were provided lunch today?"<<endl;
cin>>soldiersProvidedLunch;
cout<<endl;
double totalLunchCost = soldiersProvidedLunch*costOfLunch;
while (loopCounter == 1)
{
if (soldiersProvidedLunch <= totalSoldiers)
{
cout<<endl<<soldiersProvidedLunch<<" were provided lunch, the total cost of lunch today will be $"<<setiosflags(ios::fixed) << setprecision(2)<<totalLunchCost;
loopCounter --;
}
else
{
cout<<totalSoldiers<<" soldiers were at drill today, there cannot be more."<<endl<<endl;
cout<<"Enter the number of soldiers that were provided lunch today."<<endl;
cin>>soldiersProvidedLunch;
}
}
cout<<endl<<endl<<endl;
double gasPrice = 3.87;
double gallonsPumped = 0;
cout<<"How many gallons of gasoline were purchased today:"<<endl;
cin>>gallonsPumped;
double finalGasPrice = gasPrice*gallonsPumped;
cout<<endl;
if (gallonsPumped >= 1)
{
cout<<endl<<"The total cost for gasoline today was $"<<setiosflags(ios::fixed) << setprecision(2) << finalGasPrice<<"."<<endl;
}
else
{
cout<<"No money spent on gasoline today.";
}
double dayDrillCost = finalGasPrice+totalLunchCost+totalSoldiersDrillPay;
cout<<endl<<endl<<endl<<"Totals for today: "<<endl<<"**************************************"<<endl;
cout<<"Total pay for soldiers present: $"<<setiosflags(ios::fixed) << setprecision(2)<<totalSoldiersDrillPay<<endl;
cout<<"Total amount of money spent on lunch: $"<<setiosflags(ios::fixed) << setprecision(2)<<totalLunchCost<<endl;
cout<<"Total amount of money spent on gas: $"<<setiosflags(ios::fixed) << setprecision(2)<<finalGasPrice<<endl<<"**************************************"<<endl;
cout<<"Cost of drill today: $"<<setiosflags(ios::fixed) << setprecision(2)<<dayDrillCost<<endl;
dayCounter --;
}
cout<<"Drill completed";
}
答案 0 :(得分:1)
这是一个简单的循环,可以将单个计数器增加三次。从这里,你可以看到锄头为你的所有柜台推广它。
int someCounter = 0;
for (int day=0; day<3; ++day) { // loop three times
// do your cin input here
int tmp;
cin >> tmp;
someCounter += tmp; // increment running total
}
// print the total
如果你有很多计数器,你可以考虑创建一个struct
或class
来保存这些,为该类定义operator+=
,并覆盖该类的istream operator>>
,但是我感觉可能超出了你的任务预期。无论如何,关于如何做到这一点有很多问题。