Hopefully this title makes some sense, i wasn't really sure what to put.
Hi i need some help with this assignment. Unfortunately, I'm not sure what I've done is anywhere close to what it needs to be. I need to read a list of customer data from a text file and determine the individual sales per person and complete total. I then need to display the data in alphabetical order. This is what I have so far.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define MAX 100
int main()
{
int cnt = 0, k, ordernum, i;
char temp[31];
char state[3];
char name[MAX][101], amount[MAX][101];
double total = 0;
fstream infile("customer.txt");
if (infile.fail())
{
cerr << "Failed to open data file\n";
return 1;
}
// Read phone records from the file into arrays.
while (! infile.eof())
{
infile >> name[cnt] >> ordernum >> state >> amount[cnt];
//total = amount[cnt] + total; Pointer Addition error?
++cnt;
} // while
infile.close(); // close the data file
// Sort the records in alphabetical order by names.
for (i=0; i < cnt-1; ++i)
for (k=i+1; k<cnt; ++k)
if (strcmp(name[i], name[k]) > 0)
{
strcpy_s(temp, name[i]);
strcpy_s(name[i], name[k]);
strcpy_s(name[k], temp);
strcpy_s(temp, amount[i]);
strcpy_s(amount[i], amount[k]);
strcpy_s(amount[k], temp);
}
//Total individual sales total???
// Display the sorted records.
for (k=0; k < cnt; ++k)
{
cout << name[k] << " " << amount[k] << endl;
}
//cout << "\n" << total ;
return 0;
} // main
What I have so far displays the data in alphabetical order, but i cannot figure out how to total it. Additionally it needs to work on a unknown number of lines. The only way i could figure out how to make it work currently was set a file line max of 100.
Heres a sample of the data file im using.
Customer name - order number (ignored) - State (ignored) - Sale $
Sally 19205 MD 1017.00
George 18523 VT 982.00
Sally 16709 MD 856.00
Bill 15841 TX 1523.00
Any help would be greatly appreciated. :)