Of classes and output

Sorry about asking a third question but for some reason, this program's output doesn't work too well.

This is the file:
Barack Obama DC 100 50 300 2000
Bill DeBlasio NY 250 100 80 5000
Kirsten Gillibrand NY 50 100 200 1000
Chris Chrystie CT 100 200 300 5000
Eric Holder DC 50 80 200 1000

This is the code:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <fstream>
#define N 10

using namespace std;

  class custo{
        public:
            string first;
            string last;
            int UPerchased;
            string state;
            double SHistory[3];
    };

void printcust(custo[],int);
void sortsales(custo [],int);
void sortname(custo[],int);

int main(){
    ifstream file;
    file.open("/Users/Soul/Documents/School/CISC/Assignment10/file.txt");

    custo cust[N];
    int a = 0;
    while(file){
        file >> cust[a].first;
        file >> cust[a].last;
        file >> cust[a].state;
        file >> cust[a].SHistory[0];
        file >> cust[a].SHistory[1];
        file >> cust[a].SHistory[2];
        file >> cust[a].UPerchased;
        a++;
        cout << cust[a].first << " "
                << cust[a].last << " "
                << cust[a].state << " "
                << cust[a].SHistory[0] << " "
                << cust[a].SHistory[1] << " "
                << cust[a].SHistory[2] << " "
                << cust[a].UPerchased << endl;


    }

    return 0;
}

void sortname(custo name[],int nelts){
string temp;
for(int pass = 0; pass<nelts-1;pass++)
    for(int cand = 1;cand<nelts; cand++ )
        if(name[pass].last>name[cand].last){
            temp = name[pass].last;
            name[pass].last = name[cand].last;
            name[cand].last = temp;
        }
return;
}

void sortsales(custo name[],int nelts){
double temp;
for(int pass = 0; pass<nelts-1;pass++)
    for(int cand = 1;cand<nelts; cand++ )
        if((name[pass].SHistory[1]+name[pass].SHistory[2]+name[pass].SHistory[0])>(name[cand].SHistory[1]+name[cand].SHistory[2]+name[cand].SHistory[0])){
            temp = name[pass].SHistory[0];
            name[pass].SHistory[0] = name[cand].SHistory[0];
            name[cand].SHistory[0] = temp;
        }
return;
}

void printcust(custo cust[],int nelts){
return;
}


And this is the output:
Last login: Wed Dec 10 18:51:27 on ttys000
/Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Soul/Documents/School/CISC/Assignment10/bin/Debug/Assignment10
Christians-MacBook-Pro:~ Soul$ /Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Soul/Documents/School/CISC/Assignment10/bin/Debug/Assignment10
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Process returned 0 (0x0) execution time : 0.005 s
Press ENTER to continue.


The program is supposed to print the file, using classes of course, and the output won't work.
This works for me - putting the input variables into the while condition.

Also, the increment needs to go after the cout statements, otherwise you're trying to output the next set that hasn't been read in yet.

1
2
3
4
5
6
7
8
9
10
11
while(file >> cust[a].first >> cust[a].last >> cust[a].state >> cust[a].SHistory[0] >> cust[a].SHistory[1] >> cust[a].SHistory[2] >> cust[a].UPerchased){
       
        cout << cust[a].first << " "
                << cust[a].last << " "
                << cust[a].state << " "
                << cust[a].SHistory[0] << " "
                << cust[a].SHistory[1] << " "
                << cust[a].SHistory[2] << " "
                << cust[a].UPerchased << endl;
        a++;
    }
Topic archived. No new replies allowed.