The code is supposed to display this but my code either displays the framework without any values or it just loops infinity when I try to enter values in a .txt file.
This is for my algorithm class. It'll only display the framework or loop infinitely. I've attempted the getline functions but I'm not sure where to put them into my code because it usually causes several errors.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void headerModule();
void detailsModule();
int main ()
{
headerModule();
detailsModule();
return 0;
}
void headerModule()
{
cout<<"Brewster's Used Cars, Inc."<<endl;
cout<<"Sales Report"<<endl;
cout<<endl;
cout<<"Salesperson ID Sales Amount"<<endl;
cout<<"====================================================="<<endl;
}
void detailsModule()
{
int salespersonID, currentID;
float sales;
float totalSales = 0;
float allSales = 0;
ifstream salesFile;
salesFile.open("sales.txt");
salesFile>>salespersonID>>sales;
currentID = salespersonID;
while(!salesFile.eof())
{
if(salespersonID!=currentID)
{
cout<<"Total sales for this salesperson:"<<"$"
<<totalSales<<endl;
cout<<endl;
currentID=salespersonID;
totalSales=0;
}
cout<<salespersonID<<""<<"$"<<sales<<endl;
totalSales = totalSales+sales;
allSales = allSales+sales;
salesFile>>salespersonID>>sales;
}
cout<<"Total sales for this salesperson: "<<"$"<<totalSales<<endl;
cout<<"Total of all sales: "<<"$"<<allSales<<endl;
salesFile.close();
}
This is what it should look like:
Brewster's Used Cars, Inc.
Sales Report
Salesperson ID Sale Amount
======================================
100 $10,000.00
100 $12,000.00
100 $5,000.00
Total sales for this salesperson: $27,000.00
101 $14,000.00
101 $18,000.00
101 $12,000.00
Total sales for this salesperson: $44,500.00
102 $13,000.00
102 $14,000.00
102 $20,000.00
Total sales for this salesperson: $48,000.00
Total of all sales: $119,500.00
I either just get this:
Brewster's Used Cars, Inc.
Sales Report
Salesperson ID Sale Amount
======================================
Total sales for this salesperson: $0
Total of all sales: $0
Or it loops indefinitely when I try to enter values in the .txt file.