How to get rid of random 0s

Last time, I promise

I got this program:
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#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(){
    cout.setf(ios::fixed, ios::floatfield);
    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++;


    }
    printcust(cust,N);
    sortname(cust,N);
    printcust(cust,N);
    sortsales(cust,N);
    printcust(cust,N);

    return 0;
}

void sortname(custo name[],int nelts){
string temp;
string temp2;
string temp3;
double temp4;
int temp5;
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;
            temp2 = name[pass].first;
            name[pass].first = name[cand].first;
            name[cand].first = temp2;
            temp3 = name[pass].state;
            name[pass].state = name[cand].state;
            name[cand].state = temp3;
            temp4 = name[pass].SHistory[1];
            name[pass].SHistory[1] = name[cand].SHistory[1];
            name[cand].SHistory[1] = temp4;
            temp4 = name[pass].SHistory[0];
            name[pass].SHistory[0] = name[cand].SHistory[0];
            name[cand].SHistory[0] = temp4;
            temp4 = name[pass].SHistory[2];
            name[pass].SHistory[2] = name[cand].SHistory[2];
            name[cand].SHistory[2] = temp4;
            temp5 = name[pass].UPerchased;
            name[pass].UPerchased = name[cand].UPerchased;
            name[cand].UPerchased = temp5;


        }
return;
}

void sortsales(custo name[],int nelts){
string temp;
string temp2;
string temp3;
double temp4;
int temp5;
for(int pass = 0; pass<nelts-1;pass++)
    for(int cand = 1;cand<nelts; cand++ )
        if(name[pass].UPerchased>name[cand].UPerchased){
             temp = name[pass].last;
            name[pass].last = name[cand].last;
            name[cand].last = temp;
            temp2 = name[pass].first;
            name[pass].first = name[cand].first;
            name[cand].first = temp2;
            temp3 = name[pass].state;
            name[pass].state = name[cand].state;
            name[cand].state = temp3;
            temp4 = name[pass].SHistory[1];
            name[pass].SHistory[1] = name[cand].SHistory[1];
            name[cand].SHistory[1] = temp4;
            temp4 = name[pass].SHistory[0];
            name[pass].SHistory[0] = name[cand].SHistory[0];
            name[cand].SHistory[0] = temp4;
            temp4 = name[pass].SHistory[2];
            name[pass].SHistory[2] = name[cand].SHistory[2];
            name[cand].SHistory[2] = temp4;
            temp5 = name[pass].UPerchased;
            name[pass].UPerchased = name[cand].UPerchased;
            name[cand].UPerchased = temp5;
        }
return;
}

void printcust(custo cust[],int nelts){
    cout.setf(ios::fixed, ios::floatfield);
    for(int a = 0; a<nelts; a++)
      cout << cust[a].first << " " << setw(5) << setprecision(0)
                << cust[a].last << setw(5) << setprecision(0)
                << cust[a].state << setw(5) << setprecision(0)
                << cust[a].SHistory[0] << setw(5) << setprecision(0)
                << cust[a].SHistory[1] << setw(5) << setprecision(0)
                << cust[a].SHistory[2] << setw(5) << setprecision(0)
                << cust[a].UPerchased << endl;
return;
}


and its working well but the output is:
Last login: Wed Dec 10 19:54:49 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
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
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Barack Obama DC 100 50 300 2000
Eric Holder DC 50 80 200 1000
Kirsten Gillibrand NY 50 100 200 1000
Bill DeBlasio NY 250 100 80 5000
Chris Chrystie CT 100 200 300 5000
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Bill DeBlasio NY 250 100 80 5000
Chris Chrystie CT 100 200 300 5000
Barack Obama DC 100 50 300 2000
Kirsten Gillibrand NY 50 100 200 1000
Eric Holder DC 50 80 200 1000
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

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

I have no idea where those 0s come from nor how to get rid of them. I'm also having trouble putting the output in table format but thats just a minor issue.
for(int a = 0; a<nelts; a++)

Check the value of nelts vs. the number of records you've actually read into the array.
Thanks, the zeros are gone
Topic archived. No new replies allowed.