Structs

This program is supposed to read from the input file into an array of structs, compute one additional value per record, sorts the array based on this calculated field, print portions of the sorted record to both the screen and an output file. This is the code so far...I need help defining the struct and writing the function to compute all the totals...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// use proper includes
using namespace std;

struct record
{
// define fields of struct
};

void swap(record A[], int i, int j);
void sort_based_on_total(record A[], int size);

int main()
{
record data[3000];  // array of records (structs)

ifstream indata;
indata.open("sales.txt");

ofstream outdata;
outdata.open("ordered_by_total.txt");

int i=0;
indata >> data[i].name >> data[i].item >> data[i].style >> data[i].price >> data[i].quantity;
while(!indata.eof())
{
i++;
indata >> data[i].name >> data[i].item >> data[i].style >> data[i].price >> data[i].quantity;
}

for(int j=0; j < _____; j++)  // determine condition
{ 
// compute all totals
}

// call sort_based_on_total

for(int j=0; j < _____; j++)  // determine condition
{
	cout << data[j].name << "\t" << data[j].total << endl;	// print to screen
	outdata << data[j].name << "\t" << data[j].total << endl;	// print to file
}
 
indata.close();
outdata.close();
return 0;
}

void swap(record A[], int i, int j){
// write code to define swap
}

void sort_based_on_total(record A[], int size){
// write code to define sort_based_on_total
}





use this data to test code


Dwight Note Imagine 62.50 1
Dwight Letterhead Elegant 96.00 4
Jim Note Imagine 62.50 1
Jim Letterhead Excelsior 68.50 1
Karen Note Imagine 62.50 1
Michael Letterhead Elegant 96.00 1
Michael Note Impression 88.50 1
Michael Letterhead Elegant 96.00 1
Phyllis Letterhead Excalibur 40.90 1
Phyllis Note Inspire 74.90 1
Phyllis Letterhead Exquisite 89.90 1
Phyllis Note Inspire 74.90 1
Ryan Note Imagine 62.50 1
Ryan Letterhead Excelsior 68.50 1
Ryan Letterhead Elegant 96.00 1
Ryan Letterhead Excalibur 40.90 2
Ryan Note Imagine 62.50 2
Stanley Note Impression 88.50 1
Stanley Letterhead Excelsior 68.50 1
Stanley Letterhead Elegant 96.00 1
Your comments really tell you what you need to do.

You've identified the members of the struct in line 23 except for total.
Line 30: How many records did you read?
Line 32: Isn't the total simply price * quantity?
Line 36: You need to call sort_based_on_total
Line 49: Store A[i] in a temp, Store A[j] in A[i], Store temp in A[j]
Line 53: Iterate through the array, test if A[i] > A[i+1] then swap the two elements. Pay attenention that you don't reference past the last element of the array.
Topic archived. No new replies allowed.