Class

Write a program to define a structure named CustomerRecord that contains name, account_number and balance. Use array to store 5 records of the structure. Create 2 functions: one for reading and another for printing the content of the structure in the array.


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
#include <iostream>

using namespace std;

class CustomerRecord
{
    public:
    CustomerRecord();
    CustomerRecord(string pname,int pacc_no,int pbalance);
    void setname(string a);
    void setaccno(int a);
    void setbalance(int a);
    string get_name()const;
    int get_acc_no()const;
    int get_balance()const;
    private:
    string name;
    int acc_no;
    int balance;
};

CustomerRecord::CustomerRecord()
{

}

CustomerRecord::CustomerRecord(string pname,int pacc_no, int pbalance)
{
    pname=name;
    pacc_no=acc_no;
    pbalance=balance;
}

void CustomerRecord::setname(string a)
{
    name=a;
}

void CustomerRecord::setaccno(int a)
{
    acc_no=a;
}

void CustomerRecord::setbalance(int a)
{
    balance=a;
}

string CustomerRecord::get_name()const
{
    return name;
}

int CustomerRecord::get_acc_no()const
{
    return acc_no;
}

int CustomerRecord::get_balance()const
{
    return balance;
}

/*void print()
{
    for(int i=0;i<5;i++)
    {
        cout<<person[i].get_name();
        cout<<person[i].get_acc_no();
        cout<<person[i].get_balance();
    }

}*/

int main()
{
 CustomerRecord person[5];

 person[0].setname("mario");
 person[1].setname("Sonic");
 person[2].setname("mari");
 person[3].setname("Super");
 person[4].setname("Zombie");

 person[0].setaccno(1234567);
 person[1].setaccno(90877564);
 person[2].setaccno(9767555);
 person[3].setaccno(2345566);
 person[4].setaccno(362853);

 person[0].setbalance(123);
 person[1].setbalance(12323);
 person[2].setbalance(8976);
 person[3].setbalance(87872);
 person[4].setbalance(5321);

 for(int i=0;i<5;i++)
    {
        cout<<person[i].get_name()<<"  ";
        cout<<person[i].get_acc_no()<<" ";
        cout<<person[i].get_balance()<<"  "<<endl;
    }

}


This is my code for the question.Can someone explain to me the difference of read and print function?if i make the print as a function, i cant compile it because there is an error saying that person was not declared in this scope.Can help?Thanks:)
person was declared as local variable in main function, so in print function it is not it's scope.
This is my help guy..try your best ;)
closed account (D80DSL3A)
Easy fix = move line 77 to line63. This will make person[5] visible to your print function.
You can rewrite print function like this:
1
2
3
4
5
6
7
8
9
10
11
void print(CustomerRecord records[],int number_of_records)
{
    for(int i=0;i<number_of_records;i++)
    {
        cout<<records[i].get_name()<<"  ";
        cout<<records[i].get_acc_no()<<"  ";
        cout<<records[i].get_balance()<<"  ";
        cout <<endl;
    }

}


then in main you can write
1
2
3
// ...
 print(person,5);
Last edited on
Easy fix = move line 77 to line63. This will make person[5] visible to your print function.


But then that makes it global. Globals are evil.
closed account (D80DSL3A)
@Disch: I know it's not best to do that. That's why I said easy fix, not best fix. I figured that if the scope issue was that confusing, then passing an array to a function would be too confusing.
I see that NULL has provided this solution though. Hopefully yts can generalize from it for his read function when he writes it.
Topic archived. No new replies allowed.