Mar 22, 2013 at 2:39pm UTC
The program works, other than if I place the cursor below the last line in my merch file, the program outputs a line of garbage. The only solution I could find is to leave the cursor on the last line. Anyone know how to fix this?
[code]
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct vRecord
{
string venue, item;
float price;
};
void swap(vRecord[], int i, int j);
void sort(vRecord[], int size);
int main()
{
vRecord A[250];
ifstream fin;
fin.open("merch.txt");
ofstream fout;
fout.open("venueReport.txt");
int i=0;
while(!fin.eof())
{
fin>>A[i].venue>>A[i].item>>A[i].price;
i++;
}
sort(A, i);
int recordCount=i;
float venueTotal=0;
string currentVenue;
float venueAvg=0;
int count=1;
fout<<"Detail records for "<<A[0].venue<<endl;
venueTotal=A[0].price;
currentVenue=A[0].venue;
venueAvg=venueTotal/(count);
fout<<A[0].item<<" "<<A[0].price<<endl;
for(int x=1; x<recordCount; x++)
{
count++;
if(A[x-1].venue!=A[x].venue)
{
fout<<"\t"<<currentVenue<<" spent $"<<venueTotal<<endl;
fout<<"\t"<<currentVenue<<" spent an average of $"<<venueAvg<<endl;
fout<<endl;
venueTotal=0;
count=1;
fout<<"Detail records for "<<A[x].venue<<endl;
}
venueTotal=venueTotal + A[x].price;
venueAvg=venueTotal/(count);
currentVenue=A[x].venue;
fout<<A[x].item<<" "<<A[x].price<<endl;
}
fout<<"\t"<<currentVenue<<" spent $"<<venueTotal<<endl;
fout<<"\t"<<currentVenue<<" spent an average of $"<<venueAvg<<endl;
fout<<endl;
fin.close();
fout.close();
return 0;
}
void swap(vRecord A[], int i, int j)
{
vRecord temp;
temp=A[i];
A[i]=A[j];
A[j]=temp;
return;
}
void sort(vRecord A[], int size)
{
for(int p=1; p<size; p++)
{
for(int c=0; c<size-p; c++)
{
if(A[c].venue>A[c+1].venue) swap(A, c, c+1);
}
}
return;
}
[code]