Error: expected a declaration

Can anyone help me clear up this error please?

It's popping up on line 3 in the code below:

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
#include "stdafx.h"

CheckingAccount::CheckingAccount(){
	credit=0;
	debit=0;
}
 
CheckingAccount::CheckingAccount(){
 credit=0;
 debit=0;
 }
 
double CheckingAccount::getCredit(double ba){
 double bi;
 cout << "Input amount to credit : ";
 cin >> credit;
 if(credit<=0){
 cout << "Credit must be greater than 0.0" << endl;
 return ba;
 }
 bi=ba+credit-1000;
 return bi;
 }
double CheckingAccount::getDebit(double ba){
 double bi;
 cout << "Input amount to debit : ";
 cin >> debit;
 if(debit<=0){
 cout << "Debit must be greater than 0.0" << endl;
 return ba;
 }
 if(ba<=debit){
 cout << "Balance is not enough to debit" << endl;
 return ba;
 }
 bi=ba-debit-1000;
 return bi;
 }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef stdafx.h
#define stdafx.h
 
#pragma once
 #include <fstream>
 #include <iomanip>
 #include <string>
 #include <stdio.h>
 #include <tchar.h>
 #include <iostream>
 using namespace std;
 
#include "Account.h"
 #include "CheckingAccount.h"
 #include "SavingAccount.h"

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef SavingAccount.h
#define SavingAccount.h
 
class SavingAccount : public Account{
 private:
 double interest;
 public:
 SavingAccount();
 double getCredit(double);
 double getDebit(double);
 double getInterest(){return interest;};
 };
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef CheckingAccount.h
#define CheckingAccount.h
 
class CheckingAccount : public Account{
 private:
 double credit;
 double debit;
 public:
 CheckingAccount();
 double getCredit(double);
 double getDebit(double);
 };
#endif; 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef Account.h
#define Account.h
 
class Account{ 
private: 
char name[50];
 char userID[50];
 char password[50];
 double balance;
 int type;
 public:
 Account();
 Account(char AuserID[], char Aname[], char Apassword[], double Abalance, int Atype);
 char* getName(){return name;}
 char* getUserID(){return userID;}
 char* getPassword(){return password;}
 double getBalance(){return balance;}
 int getType(){return type;}
 void setBalance(double);
 void loadDataFromFile(const char *filename);
 void saveDataToFile(const char *filename); 
int createAnAccount(); 
};
#endif; 
1
2
3
4
5
6
7
8
9
CheckingAccount::CheckingAccount(){
	credit=0;
	debit=0;
}
 
CheckingAccount::CheckingAccount(){
 credit=0;
 debit=0;
 }


Why twice?

Also you should include CheckingAccount.h (and Account.h eventually) in stdafx.h.
Last edited on
Oh sorry, I didn't mean for that to be in there twice. I have the error without the duplicate there.

I have the CheckingAccount.h and Account.h and the SavingAccount.h in the stdafx.h don't I? Are you saying I didn't declare them correctly?
I am getting a lot of these errrors in the first couple of lines of my header files:

warning C4067: unexpected tokens following preprocessor directive - expected a newline

and

error C2008: '.' : unexpected in macro definition
1
2
#ifndef stdafx.h
#define stdafx.h 
Um what? Is this even legal syntax? Periods in macro names?
Last edited on
Remove semicolons after #endif directives.
omg, thank you! duh! changing file names to stdafx_h, etc.

oops, will remove those too vlad

Thanks!!!
darn, new errors:

warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS

^^^what in the world is this?^^^

string.h(105) : see declaration of 'strcpy'

main.cpp(134): warning C4715: 'Account::createAnAccount' : not all control paths return a value

here's main:

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "stdafx_h"
 #define interes 6
 Account* a[100],b;
 int quantity=0,n;
 
Account::Account(){
 name[0]=NULL;
 userID[0]=NULL;
 password[0]=NULL;
 balance=0;
 type=0;
 }
 
Account::Account(char Aname[], char AuserID[], char Apassword[], double Abalance, int Atype){
 strcpy(name,Aname); 
strcpy(userID,AuserID);
 strcpy(password,Apassword);
 this->balance=Abalance;
 this->type=Atype;
 }
 
void Account::loadDataFromFile(const char *filename){ 
char Aname[50],AuserID[50],Apassword[50];
 double Abalance;
 int Atype;
 ifstream is;
 is.open(filename);
 if(is.fail()){ 
cout << "File not found!" << endl; 
return; 
}
 else 
while(1){
 is.getline(AuserID,50,'|');
 is.getline(Aname,50,'|');
 is.getline(Apassword,50,'|');
 is >> Abalance;
 is >> Atype;
 is.ignore();
 a[quantity++]=new Account(Aname,AuserID,Apassword,Abalance,Atype);
 n=quantity;
 if(is.gcount()==0) break;
 }
 }
 
void Account::saveDataToFile(const char *filename){
 ofstream os(filename);
 for(int i=0;i<n;i++){
 os<<a[i]->name << "|";
 fflush(stdin);
 os<<a[i]->userID << "|";
 fflush(stdin);
 os<<a[i]->password << "|";
 fflush(stdin);
 if (i==n-1){
 os<<a[i]->balance <<" ";
 fflush(stdin);
 }
 else{
 os<<a[i]->balance << " ";
 fflush(stdin);
 }
 os << a[i]->type;
 fflush(stdin);
 os << endl;
 }
 os.close();
 }
 int Account::createAnAccount(){
 char AuserID[50],Aname[50],Apassword[50];
 double Abalance;
 int c,Atype;

 /*Enter account name*/
 do{
 cout << "Enter your name : ";
 cin.getline(Aname,sizeof(Aname));
 if (strlen(Aname)<=50 && strlen(Aname)>=2) break;
 cout << "Invalid name format. Try again !"<< endl;
 fflush(stdin);
 }while(true);
 
/*Enter account ID*/
 do{
 cout << "Enter your user ID : ";
 cin.getline (AuserID,sizeof(AuserID));
 if (strlen(AuserID)<=50 && strlen(AuserID)>=2) break;
 cout << "Invalid user ID format. Try again !"<<endl;
 fflush(stdin);
 }while(true);

 /*Check userID exist*/
 int k=0;
 for(int i=0;i<n;i++)
 if(strcmp(a[i]->userID,AuserID)==0)
 k++;
 if(k!=0){
 cout << "\nAccount ID exist! Please try again with other user ID!\n" << endl;
 return 0;
 }
 
/*Enter account password*/
 do{
 cout << "Enter your password : ";
 cin.getline(Apassword,sizeof(Apassword));
 if (strlen(Apassword)<=50 && strlen(Apassword)>=6) break;
 cout << "Invalid password format. Password must have unless 6 character. Try again !"<< endl;
 fflush(stdin);
 }while(true);
 

/*Enter account balance*/
 do{ 
cout << "Enter your balance : ";
 cin >> Abalance;
 if(Abalance > 0) break;
 cout << "Balance must be positive. Try again !" << endl;
 cin.clear(); 
fflush(stdin);
 }while(true);
 
/*Enter account type*/
 cout << "Choose your account type" <<endl;
 cout << "1. Savings Account - 2. Checking Account : ";
 cin >> c;
 if(c==1) 
Atype=1; 
else 
Atype=2;
 a[quantity++]=new Account(Aname,AuserID,Apassword,Abalance,Atype);
 n=quantity;
 cout << "Successful to create your account." << endl;
 saveDataToFile("accounts.txt");
 }
 
void Account::setBalance(double ba){
 balance=ba;
 }
 
void main(){ 
static char filename[50];
 char acc[50],pass[50];
 SavingAccount sa;
 CheckingAccount ca;
 int choice,i,flag,chon;
 double bi,si;
 b.loadDataFromFile("accounts.txt");
 while(1){ 
system("cls");
 cout <<"| BANK ACCOUNT MANAGEMENT |"<<endl;
 cout <<"|-----------------------------|"<<endl;
 cout <<"| 1. Create Account |"<<endl;
 cout <<"| 2. Login |"<<endl;
 cout <<"| 0. Exit Program |"<<endl;
 cout <<"|-----------------------------|"<<endl;
 cout <<"| Your seclection <0-->2> : ";
 cin >> choice;
 cin.sync(); 
cin.clear();
 if(choice==0) break;
 switch (choice){
 case 1: 
system("cls");
 cout<<"] Account Register ["<<endl;
 cout<<"|================================|"<<endl;
 b.createAnAccount();
 break;
 case 2: 
system("cls");
 cout<<"] LOGIN ["<<endl;
 cout<<"|================================|"<<endl;
 cout << "Enter User ID : ";
 fflush(stdin);
 cin.getline(acc,50);
 cout << "Enter Password : ";
 fflush(stdin);
 cin.getline(pass,50);
 for(i=0;i<quantity;i++){
 flag=0;
 if(strcmp(acc,a[i]->getUserID())==0 && strcmp(pass,a[i]->getPassword())==0){
 cout << "Welcome to Bank Account Management !" << endl; 
flag=1;

 /*Saving Account*/
 
if(a[i]->getType()==1){
 int choose;
 double Ainterest,ba;

 do{
 system("cls");
 cout << "|Saving Account Management|" << endl;
 cout << "|=========================|" << endl;
 cout << "| 1.View Balance |" << endl;
 cout << "| 2.View Interest Amount |" << endl;
 cout << "| 3.Credit |" << endl;
 cout << "| 4.Debit |" << endl;
 cout << "| 0.Exit to main menu |" << endl;
 cout << "|Your selection (0->2): ";
 cin >> choose;
 cin.sync(); 
cin.clear();

 if(choose==0) break;
 switch (choose){
 case 1:{
 ba=a[i]->getBalance();
 Ainterest=sa.getInterest();
 Ainterest=(ba*interes)/100;
 cout << "Your current balance is " << ba << endl; 
system("pause");
 break;
 }
 case 2:{
 ba=a[i]->getBalance();
 Ainterest=sa.getInterest();
 Ainterest=(ba*interes)/100;
 cout << "Your current interest Amount is " << Ainterest << endl;
 system("pause");
 break;
 }
 case 3:{
 si=sa.getCredit(a[i]->getBalance());
 a[i]->setBalance(si);
 system("pause");
 b.saveDataToFile("accounts.txt");
 break;
 }
 case 4:{
 si=sa.getDebit(a[i]->getBalance());
 a[i]->setBalance(si);
 system("pause");
 b.saveDataToFile("accounts.txt");
 break;
 }
 }
 }while(1);
 }

 /*Cheking Account*/
 if(a[i]->getType()==2){
 do{
 system("cls");
 cout <<"| Cheking Account Management |" << endl;
 cout <<"|============================|" << endl;
 cout <<"| 1. View Balance |" << endl;
 cout <<"| 2. Credit |" << endl;
 cout <<"| 3. Debit |" << endl;
 cout <<"| 0. Exit |" << endl;
 cout <<"|Your selection (0->3) : ";
 cin >> chon;
 cin.sync(); 
cin.clear();
 if(chon==0) break;
 switch(chon){
 case 1:{
 cout << "Your current balance is " << a[i]->getBalance() << endl; 
system("pause");
 break;
 }
 
case 2:{
 bi=ca.getCredit(a[i]->getBalance());
 a[i]->setBalance(bi);
 system("pause");
 b.saveDataToFile("accounts.txt");
 break;
 }

 case 3:{
 bi=ca.getDebit(a[i]->getBalance());
 a[i]->setBalance(bi);
 system("pause");
 b.saveDataToFile("accounts.txt");
 break;
 }
 }
 }while(1);
 }break;
 }
 }
 
if(flag==0)
 cout << "Sorry. Your account doesn't exist." << endl;
 break;

 default: cout << "Your selection must (0->2). Try again ! ";
 }
 system("pause");
 }
 }

Last edited on
Please indent your code

By the way
1
2
3
4
5
#ifndef SavingAccount_h
#define SavingAccount_h
#include "Account.h" //you should include as you're using it for inheritance
 
class SavingAccount : public Account{
Relying on stdafx.h is error prone.
Last edited on
noo1 wrote:
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS

^^^what in the world is this?^^^
This, my friend, is a company known as Microsoft trying to lock programmers to their platform by encouraging them to use functions that only their platform supports. They're right, it may be unsafe, but so is everything else in C++ :p
warning C4715: 'Account::createAnAccount' : not all control paths return a value


Exactly what it says. You have promised that this function returns an int, but there are ways for your function to finish that do not return an int.
You don't have to worry about strcpy. It's just a warning, but if you want to remove the warning define _CRT_SECURE_NO_WARNINGS at the beginning of stdafx.h (after the initial #ifdef and #define).
If you want it to be safer you should use strcpy_s which comes from Microsoft's CRT, but you will probably lack portability to other OS (Easily fixable rewriting the function).
It adds another parameter which is the destination string's size.
It's used so it will not write memory that you cannot access - thus avoiding a bad memory writing.

warning C4715: 'Account::createAnAccount' : not all control paths return a value

Like moschops said, in your function, when some conditions happen, the function is not returning a value that may be requested somewhere else. This is an example:

1
2
3
4
int AddInts(int First, int Second)
{
    if(First > Second) return First + Second;
}

In this example, if First <= Second, it will never return any valid value. You should make sure all "paths" return a value.
Last edited on
@noo1

I think you should avoid all the infinite loops you have. You have obvious end conditions, so use these in the loop condition, as opposed to testing the end condition then breaking out.

You could make use of a bool variable to help end a loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
      bool Quit = false;
     char answer = 'N';

    while(!Quit) {

           //your code here

           cout << "Do you want to Quit? Y or N" << endl;
           cin >> answer;
          
           answer = std::toupper(answer);  //avoids testing answer twice
           if(answer == 'Y')  //test variable once
                 Quit = true;
    }



There are valid situations when infinite loops are OK, but this isn't one of them. Always try to have sensible loops.

HTH
Also, main returns an int - always. There have been all kinds of excuses why newbies don't do this - I think the wisest thing is to just do it properly:

1
2
3
4
int main {

return 0; //if all is well, some thing else otherwise
}


You could make more use of functions - each menu option should call a function which does that option. The functions being called should be part of the class. This will tidy up main considerably I think.

BTW, what did you think of the code I posted to you a while back? You current code only has one class - are you sure that is the best solution? The purpose of the Bank class in my code was to manage the Account class, and contained a vector of accounts.

Did you manage to figure out how to do separate header and .cpp files for classes - instead of putting everything into one giant file?

I don't know why your code is not indented properly - your IDE should do this for you.

I look forward to see how you go.
Thank you all very much.

Hi HTH. I messed around with the indenting we talked about last time and now it's worse I think. Will keep tweaking settings.

I didn't use the code you pointed me to last time because it uses vectors which are not part of this assignment. Maybe for the next assignment...

Thanks again everyone for all the help.

Hope everyone enjoyed the Thanksgiving holiday (if you're in the U.S.) :-)
Hi HTH


:+D , I wondered when that might happen - HTH means "Hope this helps" not my initials - cheers. I am not the only one that does this.

I guess it is tough to work out what all these abbreviations mean.

I didn't use the code you pointed me to last time because it uses vectors which are not part of this assignment. Maybe for the next assignment...


You could use an array of fixed size instead of the vector. The advantage of the vector is you don't need a fixed size - you can just keep pushing objects into it. Not hard to change it to an array though.
oh haaa, sorry about thet HTH. I've used it as "hope that helps" myself at times, just wasn't thinking

ps...I do try to keep up with what you explain, but I often feel like I'm jumping high so that it doesn't go over my head. :-)))


Thanks!
Topic archived. No new replies allowed.