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.