I've gotten about 90% of this program done, but I can't seem to figure out how to do one last operation in it that is needed.

What I'm having a hard time figuring out, is how to make a total for each person in the file? I'm sure it's deceptively simple and I'm completely overthinking the solution to it, hence why I can't quite seem to figure it out.

Here's the input file, it's just a simple space delineated input.

Sam Smith 345.67 329.10 398.00
Roy Rodriguez 429.05 399.83 351.07
Charlie Cox 349.99 388.22 392.13
Debbie Delta 400.00 350.00 375.00
Yvonne Yang 388.88 402.19 378.90

And the code I've gotten thus far:

#include <iostream>
#include <fstream>
#include <cstdlib>

int main()
{
    char fName[25] = {" "};
    char lName[25] = {" "};
    double moOneAmt;
    double moTwoAmt;
    double moThreeAmt;
    double moOneTotal = 0;
    double moTwoTotal = 0;
    double moThreeTotal = 0;
    std::ifstream salesIn;
    salesIn.open("sales",std::ios::in);
    if(!salesIn)
    {
                std::cerr << "Sales file not opened...\n"
                          << std::endl;
                exit(-1);
    }
    salesIn    >> fName
               >> lName
               >> moOneAmt
               >> moTwoAmt
               >> moThreeAmt;

    while (!salesIn.eof())
    {
          moOneTotal += moOneAmt;
          moTwoTotal += moTwoAmt;
          moThreeTotal += moThreeAmt;
          std::cout << fName << "\t"
                    << lName << "\t"
                    << moOneAmt << "\t"
                    << moTwoAmt << "\t"
                    << moThreeAmt << std::endl;
          salesIn >> fName
                  >> lName
                  >> moOneAmt
                  >> moTwoAmt
                  >> moThreeAmt;
    }
    salesIn.close();
    std::cout << "The first months total is $" << moOneTotal << std::endl;
    std::cout << "The second months total is $" << moTwoTotal << std::endl;
    std::cout << "The third months total is $" << moThreeTotal << std::endl;
    return 0;
}

And the programs output:

Sam Smith 345.67 329.1 398
Roy Rodriguez 429.05 399.83 351.07
Charlie Cox 349.99 388.22 392.13
Debbie Delta 400 350 375
Yvonne Yang 388.88 402.19 378.9
The first months total is $1913.59
The second months total is $1869.34
The third months total is $1895.1

Recommended Answers

All 2 Replies

Yes, I think it is deceptively simple and you are overthinking it. Wouldn't the total for each person be this?

moOneAmt + moTwoAmt + moThreeAmt

Thus couldn't you just add this line to your other cout lines (see red)?

std::cout << fName << "\t"
                    << lName << "\t"
                    << moOneAmt << "\t"
                    << moTwoAmt << "\t"
                    << moThreeAmt << '\t'
                    << moOneAmt + moTwoAmt + moThreeAmt
                    <<std::endl;
commented: Concise and clear +29

Ugh now i feel like a fool, thanks much, lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.