更新:我发现它
#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;
}
所以我有两个程序
其中一个几乎完成了所有工作,但文件验证无效。
然而在另一个我使它工作得非常好,但它与最后一个while循环不兼容。
我尝试过将它们组合在一起,但似乎只有一个或另一个可以工作。
我的意思是,我将文件验证从较短的代码切换到较长的代码,我的销售和费用的最大值搞砸了,我不明白为什么。
是因为我没有正确使用文件的打开和关闭?
也是效果最好的那个,setprecision(2)给了我一些疯狂的结果,几乎没有数字。
例如当我编辑它时:cout&lt;&lt; &#34;费用的平均值为:$&#34; &LT;&LT; (Esum / ElineCount)&lt;&lt; ENDL;
到此:cout&lt;&lt; &#34;费用的平均值为:$&#34; &LT;&LT; setprecision(2)&lt;&lt; (Esum / ElineCount)&lt;&lt; ENDL;
它只是给我一个抽象的结果。
好的,我最想知道为什么:
//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();
与此不相容:
//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;
}
另一个:
#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;
}
这是我的输入文件的样子:
6/20/2015 890.85
所有这些^格式都在销售和费用中重复。
答案 0 :(得分:0)
基本输入结构。我删除了费用部分,因为逻辑相似。我还留下了一些关于如何计算你需要输出的结果的注释,但是如果你仍然无法使它输出,输出真的应该是一个问题。
一次只写一位代码。一旦它工作,你可以相信它为下一部分提供良好的数据,然后继续。如果第2阶段建立在错误的第1阶段,那么你可能会调试错误的代码。
这也有助于您将问题简明扼要。他们更有可能以这种方式回答。
#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;
}