Stupid problem with variables

Hey Guys!!!

I was writting this code and now there is a very simple problem which is eating my brain cause i dont know what the hell is wrong with the code. when you compile and run the code choose option number 1, then enter your data, and exit the program. the output will be stored in a file called info.txt. the problem is that account holder name will not be stored and exactly was has been entered for account type will be stored as account holder name too and i really dont know why.

thanks in advance.

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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  #include<iostream>
#include<cstdlib>
#include<fstream>
#include<cctype>
#include<cstdio>
#include<cstring>

using namespace std;

class Bank
{
public:
    void new_account();
    void deposit_amount();
    void withdraw_amount();
    void balance();
    void close_account();
    void modify_account();

    int acc_number_f();
    char acc_holder_name_f();
    char acc_type_f();
    int inital_amount_f();
    int total_amount_f();

private:
    int acc_number;
    char acc_holder_name[50];
    char acc_type;
    int inital_amount;
    int total_amount;

};
int main_menu();
/*----------------------------------------------------------
//--------------------MAIN FUNCTION()-----------------------
-----------------------------------------------------------*/
int main()
{
int chosen_number;
char ans;
start_over:
chosen_number=main_menu();
Bank cus_obj;
if(chosen_number=='1')
{
    system("cls");
    cus_obj.new_account();
}
else if(chosen_number=='4')
{
    system("cls");
    cus_obj.balance();
}
else
{
    return 0;
}
cout<<"do you want to continue? ";
cin>>ans;
if (ans=='y')
{
    system("cls");
    goto start_over;
}
else
{
system("cls");
cout<<"Thanks for Banking with us!!!";
}

return 0;
}
//-----------------MAIN MENU()-------------------------
int main_menu()
{
    char opt;
    cout<<"-------HPRO. Banking system---------\n";
    cout<<"1.   New account"<<endl;
    cout<<"2.   Deposit amount"<<endl;
    cout<<"3.   Withdraw amount"<<endl;
    cout<<"4.   Balance Inquery"<<endl;
    cout<<"5.   All Acount Hoedr List"<<endl;
    cout<<"6.   Close an account"<<endl;
    cout<<"7.   Modify and account"<<endl;
    cout<<"8.   Exit"<<endl;
    cout<<"Choose a number between 1 and 8: ";
    cin>>opt;
    if (opt>='1' && opt<='8')
    {
        return opt;
    }
    else
    {
        system("cls");
        cout<<"!!!!!!!!!!!!Wrong selection, Please choose again\n";
        main_menu();
    }
}
//-----------------------------------------------------------
void Bank::new_account()
{
    cout<<"***Creating new Account***\n";
    cout<<"Enter the account number: ";
    cin>>acc_number;
    cout<<"Enter the account holder's name: ";
    cin.ignore();
    gets(acc_holder_name);
    cout<<"Enter the account type(s/c): ";
    cin>>acc_type;
    cout<<"Initial amount: ";
    cin>>inital_amount;


    ofstream myfile;
    myfile.open("info.txt", ios::out | ios::app);
    if(myfile.is_open())
    {
        myfile<<acc_number_f()<<"\t"<<acc_holder_name_f()<<"\t"<<acc_type_f()<<"\t"<<inital_amount_f()<<endl;
        myfile.close();
    }
    else
    {
        cout<<"Error opening file, contact the administrator\n";
    }
}
/*----------------------------------------------------------
                BALANCE() FUNCTION
-----------------------------------------------------------*/
void Bank::balance()
{
    char input;
    ifstream myfile;
    myfile.open("info.txt", ios::in);
    while(!myfile.eof())
    {
        myfile.get(input);
        cout<<input;
    }
    myfile.close();

}
int Bank::acc_number_f()
{
    return acc_number;
}
char Bank::acc_holder_name_f()
{
    return acc_holder_name[50];
}
char Bank::acc_type_f()
{
    return acc_type;
}
int Bank::inital_amount_f()
{
    return inital_amount;
}
int Bank::total_amount_f()
{
    return total_amount;
}
The problem is this:
1
2
3
4
5
char Bank::acc_holder_name_f() // you return only a character
{
    return acc_holder_name[50]; // 50 is out of bounds
        // 49 is the last possible index because it starts with 0
}


what you want is this:
1
2
3
4
const char *Bank::acc_holder_name_f()
{
    return acc_holder_name; // This returns the string
}
Coder777 thanks for your response, i did what you said but the program gets error and does not get compiled.
I know playing "guess-the-error" is a lot of fun, but some of us are kinda busy. Perhaps you could, y'know, actually tell us what the error message is, rather than making us guess?

EDIT: Did you remember to change the method declaration in your class definition?
Last edited on
closed account (zb0S216C)
You can return an array by reference:

1
2
3
4
char ( &GetCharArray( ) )[50]
{
    return( SomeArray );
}

Wazzak
Topic archived. No new replies allowed.