-1

UPDATE: I DUN DID IT

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    ifstream toRead;
    ifstream toRead2;
    string fileName;
    string fileName2;
    string date;
    string Edate;
    string largestDate;
    string ElargestDate;
    double Samount = 0.00;
    double Eamount = 0.00;
    double average = 0.00;
    double Eaverage = 0.00;
    double sum = 0.00;
    double Esum = 0.00;
    double largest = 0.00;
    double Elargest = 0.00;
    double lineCount = 0.00;
    double ElineCount = 0.00;
    double netIncome = 0.00;


    cout << "Please enter the path to your sales file: ";
    getline(cin, fileName);
    toRead.open(fileName.c_str());

    while(toRead.fail())
    {
        cout << "Failed. Please enter the path to your sales file again: ";
        getline(cin, fileName);
        toRead.open(fileName.c_str());
    }


    cout << "Please enter the path to your expenses file: ";
    getline(cin, fileName2);
    toRead2.open(fileName2.c_str());

    while(toRead2.fail())
    {
        cout << "Failed. Please enter the path to your expenses file again: ";
        getline(cin, fileName2);
        toRead2.open(fileName2.c_str());
    }


//-----------------------------------------------------------------------------------


//SALES LARGEST & AVERAGE
    cout << "---------------SALES-----------------"<< endl;
    while(!toRead.eof())
    {
        lineCount++;
        toRead >> date;
        toRead >> Samount;
        if(Samount > largest)
        {
            largest = Samount;
            largestDate = date;
        }
        sum += Samount;
        //  lineCount++;
        cout << "Sale date and amount: "<< date << " $" << Samount << endl;

    }



//EXPENSES LARGEST & AVERAGE
    cout << "\n--------------EXPENSES----------------"<< endl;
    while(!toRead2.eof())
    {
        ElineCount++;
        toRead2 >> Edate;
        toRead2 >> Eamount;
        if(Eamount > Elargest)
        {
            Elargest = Eamount;
            ElargestDate = Edate;
        }
        Esum += Eamount;
        cout << "Expenses date and amount: "<< Edate << " $" << Eamount << endl;

    }


//---------------------------------------------------------------------------------------------


    cout << "\nThe sum of all sales: $"<<setprecision(2)<<fixed<< sum << endl;
    cout << "Largest sale amount and date: "<< largestDate << " $" << setprecision(2)<<fixed<<largest << endl;
    cout << "The average of the sales is: $" << setprecision(2)<<fixed<<(sum/lineCount) << endl;


    cout << "\nThe sum of all expenses: $"<<setprecision(2)<<fixed<< Esum << endl;
    cout << "Largest expense amount and date: "<< ElargestDate << " $" <<setprecision(2)<<fixed<< Elargest << endl;
    cout << "The average of the expenses is: $" << setprecision(2)<<fixed<<(Esum/ElineCount) << endl;


    netIncome = sum - Esum;

    if (netIncome > 0)
    {
        cout << "\nYou are in the BLACK. Net income is: $" << setprecision(2)<<fixed<<netIncome << endl;
    }
    else cout << "\nYou are in the RED. Net income is: $" << "(" <<setprecision(2)<<fixed<< (netIncome * -1) << ")" << endl;

    toRead.close();
    toRead2.close();
    return 0;
}

So i have two programs

one of them has almost everything done but the file validation is just not working.

yet in the other one i made it works perfectly fine yet it is incompatible with the last while loop.

ive tried combining them but it seems like only one or the other will work.

what i mean by this is that i switch the file validation from the shorter code to the longer code and my largest value for sales and expenses gets messed up and i dont understand why.

is it because i dont have proper use of opening and closing of the files?

also with the one that works the most, setprecision(2) gives me some insane result thats barely a number.

for example when i edit this: cout << "The average of the expenses is: $" << (Esum/ElineCount) << endl;

to this: cout << "The average of the expenses is: $" << setprecision(2) << (Esum/ElineCount) << endl;

it just gives me an abstract result.

OK SO MOST OF ALL I WANT TO KNOW WHY THIS:

//SALES FILE ENTRTY
    cout << "Please enter sales file name\n";
    getline(cin, fileName);
    while(toRead.fail())
    {
        cout << "File does not exist\n Please try again\n";
        cout << "Please enter file name\n";
        getline(cin, fileName);

    }



toRead.open(fileName.c_str());
    toRead >> date;
    toRead >> Samount;
    largest = Samount;
    lineCount++;
toRead.close();

IS INCOMPATIBLE WITH THIS:

//SALES LARGEST & AVERAGE
cout << "---------------SALES-----------------"<< endl;
toRead.open(fileName.c_str());
    while(!toRead.eof())
    {
        lineCount++;
        toRead >> date;
        toRead >> Samount;
        if(Samount > largest)
        {
            largest = Samount;
            largestDate = date;
        }
    sum += Samount;
  //  lineCount++;
        cout << "Sale date and amount: "<< date << " $" << Samount << endl;

    }
toRead.close();

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    ifstream toRead;
    ifstream toRead2;
    string fileName;
    string fileName2;
    string date;
    string Edate;
    string largestDate;
    string ElargestDate;
    double Samount;
    double Eamount;
    double average = 0.00;
    double Eaverage = 0.00;
    double sum = 0.00;
    double Esum = 0.00;
    double largest;
    double Elargest;
    double lineCount = 0.00;
    double ElineCount = 0.00;
    double netIncome = 0.00;

//SALES FILE ENTRTY
    cout << "Please enter sales file name\n";
    getline(cin, fileName);
    while(toRead.fail())
    {
        cout << "File does not exist\n Please try again\n";
        cout << "Please enter file name\n";
        getline(cin, fileName);

    }



toRead.open(fileName.c_str());
    toRead >> date;
    toRead >> Samount;
    largest = Samount;
    lineCount++;
toRead.close();

//---------------------------------------------------------------------------------------------








//EXPENSES FILE ENTRY
    cout << "Please enter expenses file name\n";
    cin >> fileName2;
    while(toRead2.fail())
    {
        cout << "File does not exist\n Please try again\n";
        cout << "Please enter file name\n";
        cin >> fileName2;

    }



toRead2.open(fileName2.c_str());
    toRead2 >> Edate;
    toRead2 >> Eamount;
    Elargest = Eamount;
    lineCount++;
toRead2.close();




//-----------------------------------------------------------------------------------




//SALES LARGEST & AVERAGE
cout << "---------------SALES-----------------"<< endl;
toRead.open(fileName.c_str());
    while(!toRead.eof())
    {
        lineCount++;
        toRead >> date;
        toRead >> Samount;
        if(Samount > largest)
        {
            largest = Samount;
            largestDate = date;
        }
    sum += Samount;
  //  lineCount++;
        cout << "Sale date and amount: "<< date << " $" << Samount << endl;

    }
toRead.close();



//EXPENSES LARGEST & AVERAGE
cout << "\n--------------EXPENSES----------------"<< endl;
toRead2.open(fileName2.c_str());
    while(!toRead2.eof())
    {
        ElineCount++;
        toRead2 >> Edate;
        toRead2 >> Eamount;
        if(Eamount > Elargest)
        {
            Elargest = Eamount;
            ElargestDate = Edate;
        }
    Esum += Eamount;
        cout << "Expenses date and amount: "<< Edate << " $" << Eamount << endl;

    }
toRead2.close();

//---------------------------------------------------------------------------------------------


    cout << "\nThe sum of all sales: $"<< sum << endl;
    cout << "Largest sale amount and date: "<< largestDate << " $" << largest << endl;
    cout << "The average of the sales is: $" << (sum/lineCount) << endl;


    cout << "\nThe sum of all expenses: $"<< Esum << endl;
    cout << "Largest expense amount and date: "<< ElargestDate << " $" << Elargest << endl;
    cout << "The average of the expenses is: $" << (Esum/ElineCount) << endl;


    netIncome = sum - Esum;

    if (netIncome > 0)
    {
        cout << "\nYou are in the BLACK. Net income is: $" << netIncome << endl;
    }
else cout << "\nYou are in the RED. Net income is: $" << "(" << (netIncome * -1) << ")" << endl;

toRead.close();
toRead2.close();
    return 0;
}

the other one:

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


using namespace std;

int main()
{
ifstream toRead;
ifstream toRead2;
string file;
string file2;



double nextSale, largestSale, saleAvg, saleAmount;
int saleLnCount = 0;
string saleDate, largestSaleDate;



cout << "Please enter the path to your sales file: ";
getline(cin, file);
toRead.open(file.c_str());

while(toRead.fail())
{
 cout << "Failed. Please enter the path to your sales file again: ";
getline(cin, file);
toRead.open(file.c_str());
}
cout << "Please enter the path to your expenses file: ";
getline(cin, file2);
toRead2.open(file2.c_str());

while(toRead2.fail())
{
    cout << "Failed. Please enter the path to your  file again: ";
    getline(cin, file2);
    toRead2.open(file2.c_str());
}


while(!toRead.fail() && !toRead.eof()){



cout << "---------SALES---------\n";

    while(toRead>>saleDate)
    {

    toRead >> saleAmount;

      cout << "Date and sale and amount: " << saleDate<< " $"<< saleAmount << endl;


    }



}




while(!toRead2.fail() && !toRead2.eof()){


string expenseDate;
double expenseAmount;

cout << "---------EXPENSES---------\n";

    while(toRead2>>expenseDate)
    {

    toRead2 >> expenseAmount;

      cout << "Date and expense and amount: " << expenseDate<< " $"<< expenseAmount << endl;


    }

}

toRead >> saleDate;
toRead >> saleAmount;

largestSale = saleAmount;
saleLnCount++;


while(!toRead.eof())
{
    saleLnCount++;
    toRead >> saleDate;
    toRead >> saleAmount;
    if(saleAmount > largestSale)
    {
        largestSale = saleAmount;
        largestSaleDate = saleDate;
    }

}
cout << "\nLargest sale date: " << largestSaleDate << " " << largestSale << endl;
cout << largestSaleDate << endl;


return 0;
}

this is what my input files look like:

6/20/2015 890.85

its all in this^ format repeated in both the sales and expenses.

jeeboe
  • 31
  • 5
  • Tons of code and no clear question. You have failed at SO. Try again. – David Jun 25 '15 at 22:35
  • i edited it. to see if it made my question more clear. – jeeboe Jun 25 '15 at 22:53
  • Tonnes of scorn and minimal explanation. More than one failure here. Jeeboe, take what you have and chop it down to a function that demonstrates your problem and a main that calls the function. Post that along with a clear question and the hatred-o-meter will go way down. – user4581301 Jun 25 '15 at 22:53
  • keep in mind this is the second program ive ever written in my life – jeeboe Jun 25 '15 at 22:53
  • 1
    Comment 1: Give variables good names. Instead of toRead use something descriptive like salesFile. – user4581301 Jun 25 '15 at 22:59
  • Comment 2: `while(!toRead.fail() && !toRead.eof()){` Will not do what you want it to do. See here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Jun 25 '15 at 23:01
  • You have too many problems in this code to solve in one question. You aren't getting the right out put because you aren't reading in the data correctly. Forget about the output and focus on getting the data in. – user4581301 Jun 25 '15 at 23:19
  • well alright but what is it you see that is bad in the input? – jeeboe Jun 25 '15 at 23:24
  • ok everything might be bad but at least pick out something like my input for example. – jeeboe Jun 25 '15 at 23:28

1 Answers1

0

Basic input structure. I've removed the expenses portion because the logic is similar. I've also left a few notes on how to tally up the results you need to output, but the output really should be nother question if you still can't get it to work.

Only write one bit of code at a time. Once it works and you can trust it to be providing good data to the next part, then proceed. If phase 2 is built on a faulty phase 1, odds are you'll wind up debugging the wrong piece of code.

This will also help you keep questions concise and to the point. They are more likely to be answered that way.

#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

int main()
{
    ifstream salesFile; // more obvious name. Makes reading the code easier.
    string salesFileName;

    double nextSale, largestSale, saleAvg, saleAmount;
    int saleLnCount = 0;
    string saleDate, largestSaleDate;

    // nothing wrong with this approach
    cout << "Please enter the path to your sales file: ";
    getline(cin, salesFileName);
    salesFile.open(salesFileName.c_str());

    while (!salesFile.is_open()) // this should be pretty obvious
    {
        cout << "Failed. Please enter the path to your sales file again: ";
        getline(cin, salesFileName);
        salesFile.open(salesFileName.c_str());
    }

    string line; 
    cout << "---------SALES---------\n";
    while (getline(salesFile, line)) // when you hit the end of the file or the 
                                     // file fails for some reason, the loop will exit     
                                     // this also separates the logic of reading the line 
                                     // from the parsing of the line.
                                     // also makes it really easy to get a line count.
    {
        stringstream linestr(line); // put the line into it's own stream
                                    // now it can be taken apart just like cin
        if (linestr >> saleDate >> saleAmount) // if anything goes wrong reading, 
                                               // this input fails and the if gets a false
        {
            cout << "Date and sale and amount: " << saleDate << " $" << saleAmount << endl;
            // note you aren't storing either the date or amount. 
            // Every new input overwrites the last, so summing results, etc... is impossible.
            // standard approach is to put the inputs into a vector and operating on 
            // the vector after the loop to separate gathering from analysis, 
            // but that's dumb in this case because the analysis is so simple.
            // instead, keep a record of the largest sale you've seen to this point
            // When you see a larger sale, replace the current largest
            // when the loop's done, output the current largest
            // to compute the average, sum up all inputs and count the total number of inputs
            // When the loop's done, divide the sum by the count. 

        }
        else
        {
            // handle invalid line
        }
    }

    cout << "\nLargest sale date: " << largestSaleDate << " " << largestSale << endl;
    cout << "\Average Sale: " << (salesSum / totalSales) << endl;

    return 0;
}
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • thank you i ended up finishing this and getting an A. i edited the question and put my code in it. – jeeboe Jun 28 '15 at 19:28
  • @jeeboe Excellent. But still be careful of `while(!toRead.eof())`. It doesn't work. It looks like i works if you're lucky or the data is perfectly formatted, but it fails horribly and silently if the file ends unexpectedly in the middle of a line. – user4581301 Jun 29 '15 at 15:51