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?
		} // 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. :)

7 Years
Discussion Span
Last Post by Ancient Dragon

Since you don't know how many salesmen or how many sales there will be you are probably better off using a list or a vector (if you know about these containers) because they enlarge as needed, rather than declare an array of salesmen that is "likely big enough"; though if you don't know about lists and/or vectors and aren't supposed to use things you haven't heard about yet, then you're stuck with do the hard work yourself of enlarging the array if you didn't guess big enough initially. Likewise, you can make your task a little easier if you know about user defined types called struct or class, but if you don't know about them then using parallel arrays as you have structured it is okay.

As you read in the information from the file, before you add the information to the array, see if the name read in is already in the array. If so, increment the sales, if not, add the current information to the arrays at the new index. To do this read the information into two holding variables instead of directly into the arrays.

And I know you didn't ask, but don't use eof() to control the performance of the loop. Use the first read to name within the () after while.

while (infile >> tempName) 		
   infile >> ordernum >> state >> tempAmount;

Once you've read in the entire file, then total all the amounts in the sales array to get the grand total.

Edited by Lerner: n/a


Sorting becomes a lot easier if you use a structure to hold each person's information instead of individual arrays. Something like this:

struct person
    char name[MAX+1];
    vector<float> sales;

Note that amount needs to be either an int or float, but never char. Assumes you are allowed to use std::vector. If not then make it a standard array float sales[MAX];

Edited by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.