output coming out incorrectly

closed account (o1w5fSEw)
Hello i need a little help with my programming hw. I got it running just the output is coming out incorrect. everything is correct except the first line of my output. This is what i get...

Account:
-858993460              -858993460      -107374176.00
Account:
6788    Awaz    789     1643.11
Account:
5511    Boris   555     1107.22
Account:
4567    Eng     345     848.79
Account:
3453    Nguyen  456     1293.12
Account:
8764    Ruida   234     6185.23
Account:
3123    Sen     222     1034.45
Account:
3456    Westin  765     9705.21
Account:
1010    admin   999     200.00
Account:
1111    test    111     4757.78
Press any key to continue . . .



I need to input this file and have the program sort it in alphabetical order:
1010 admin 999 200.00
1111 test 111 4757.78
3123 Sen 222 1034.45
4567 Eng 345 848.79
3453 Nguyen 456 1293.12
6788 Awaz 789 1643.11
5511 Boris 555 1107.22
3456 Westin 765 9705.21
8764 Ruida 234 6185.23

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
#include <iostream>
#include <string>
#include <fstream>
#include<iomanip>
#include<cstdlib>
using namespace std;
struct Account  // a new data type to represent an ATM bank account
{
    string name;
    int ID, PIN;
    float balance;
};
void Display(Account a);
void Swap (Account a);   // swap 2 ints
void Sort (Account a[], int size); // sort an array of int
int main ()
{	
cout<<fixed<<showpoint;
cout<<setprecision(2);
ifstream infile;
Account accts[25];
int n=0,j;
infile.open("accounts.txt");
if (!infile)
cout<<"File Not Found"<<endl;
while(!infile.eof()){
infile>>accts[n+1].ID>>accts[n+1].name>>accts[n+1].PIN>>accts[n+1].balance;
n++;
}
Sort(accts,n+1);
for(j=0;j<n+1;j++){
Display(accts[j]);}
system("pause");
return 0;
}
void Display(Account a)
{
cout<<"Account:\n"<<a.ID<<'\t'<<a.name<<'\t'<<a.PIN<<'\t'<<a.balance<<endl;
}

void Swap (Account &a, Account &b)
{
Account Temp = a; 
a = b;
b = Temp;
}
//Sort() Order the values in the array
void Sort (Account a[], int size)
{
int nm, n;
	
for (n=0; n<size; n++) // repeat; each cell but last.
{
for (nm=0; nm<size-1; nm++)// find smallest from k on...
{
if (a[nm].name > a[nm+1].name) // if smaller, save position
{
Swap (a[nm], a[nm+1]); // swap these cells
}
}
}
}
Last edited on
Would you mind editing your post and putting the source inside code tags? It will make it a lot more legible and folks here will be more likely to look at it.
closed account (o1w5fSEw)
sorry new to the forums i fixed it.
closed account (D80DSL3A)
Your array index usage is a bit off. On line 27 the 1st element filled is accts[1]. This should start at accts[0]. This is why you have garbage values for accts[0], it was never assigned values!

Replace all n+1 with n on lines 27, 30 and 31 (you are also Sorting and Displaying 1 too many accts). I think that will fix it.
closed account (o1w5fSEw)
it work!! Thanks Alot!!!
Topic archived. No new replies allowed.