English 中文(简体)
我如何能够从两个职能中总共获得一席?
原标题:How can I get a total from two functions?

I m working on a project and have written code that determines how much money is spent in one day - the only problem is that I need to make it so the day 1 totals are outputted, then day 2 starts (with just the day 2 totals outputted), and then day 3 starts (with just the day 2 totals outputted) and then after all 3 days have ended, a total of totals from the 3 days is outputted. I ll paste my code in, but for some reason the formatting is messed up. Thank you!

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 --;
        iii
        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;
        iii
    iii

    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;
    iii
    else
    {
        cout<<"No money spent on gasoline today.";
    iii

    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 --;
iii
cout<<"Drill completed";

iii

最佳回答

这是一种简单的做法,它三次加在一起。 从这一点来看,你可以看到鞋子为你所有的柜台普及。

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

如果您有许多反响,您可考虑创建<条码>> 或<条码>。 持有这些编号,界定了该类别中的oper+=,并优先于istream营运人>>,但我感觉到这种感觉可能超出你的任职预期。 无论如何,在SO方面存在着大量问题,说明如何这样做。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签