hi!i am having some problems with a program i have made regarding bank account handling using classes. here's part of the code: Code: #include<iostream.h> class account { private: int balance,c,d; public: account(int x) { if(x>=0) { balance=x; } else { cout<<"invalid entry\nyour account balance has been set to 0\n"; balance=0; } } void credit() { cout<<"enter the amount you want to deposit\n"; cin>>c; balance=balance+c; cout<<"deposited.\n"; } void debit() { cout<<"enter the amount you want to draw\n"; cin>>d; balance=balance-d; cout<<"done.\n"; } void get_balance() { cout<<"your current balance is "<<balance<<endl; } }; void main() { int g,h,k,j; char a; cout<<"to create an account-account 1- press a\n"; do { cin>>a; if(a=='a') { cout<<"enter the initial balnace you want to deposit\n"; cin>>g; account one(g); } else { cout<<"invalid entry.enter 'a' again\n"; } }while(a!='a'); cout<<"to create an account-account 2- press a\n"; do { cin>>a; if(a=='a') { cout<<"enter the initial balnace you want to deposit\n"; cin>>h; account two(h); } else { cout<<"invalid entry.enter 'a' again\n"; } }while(a!='a'); cout<<"for deposit,withdrawal or balance inquiry of account 1 press 1,for account two press 2.\n"; cin>>k;///////////////////// switch(k) { case 1: cout<<"for deposit press 1\nfor withdrawl press 2\nfor balance inquiry press 3\nto go to the other account press 4\nto exit press 5\n"; do { cin>>j; switch(j) { case 1: one.credit(); break; case 2: one.debit(); break; case 3: one.get_balance(); break; case 4: goto a2; break; case 5: break; } } while(j!=4 || j!=5); } } but my compiler gives the following error: --------------------Configuration: sam - Win32 Debug-------------------- Compiling... sam.cpp C:\Documents and Settings\786\Desktop\sam.cpp(93) : error C2065: 'one' : undeclared identifier C:\Documents and Settings\786\Desktop\sam.cpp(93) : error C2228: left of '.credit' must have class/struct/union type C:\Documents and Settings\786\Desktop\sam.cpp(96) : error C2228: left of '.debit' must have class/struct/union type C:\Documents and Settings\786\Desktop\sam.cpp(99) : error C2228: left of '.get_balance' must have class/struct/union type Error executing cl.exe. sam.exe - 4 error(s), 0 warning(s) its the 3rd day i ve been working on it but i cant figure out whats wrong with it.please help me!!!!!!!!!!!!!!!!! thanks in advance! sam.
Code: #include<iostream.h> #include<conio.h> class account { private: int balance,c,d; public: account(int x) { if(x>=0) { balance=x; } else { cout<<"invalid entry\nyour account balance has been set to 0\n"; balance=0; } } void credit() { cout<<"enter the amount you want to deposit\n"; cin>>c; balance=balance+c; cout<<"deposited.\n"; } void debit() { cout<<"enter the amount you want to draw\n"; cin>>d; balance=balance-d; cout<<"done.\n"; } void get_balance() { cout<<"your current balance is "<<balance<<endl; } }; void main() { int g,h,k,j; char a; clrscr(); cout<<"to create an account-account 1- press a\n"; do { cin>>a; if(a=='a') { cout<<"enter the initial balnace you want to deposit\n"; cin>>g; account(g); } else { cout<<"invalid entry.enter 'a' again\n"; } }while(a!='a'); account one(g); cout<<"to create an account-account 2- press a\n"; do { cin>>a; if(a=='a') { cout<<"enter the initial balnace you want to deposit\n"; cin>>h; a2:; account(h); } else { cout<<"invalid entry.enter 'a' again\n"; } }while(a!='a'); cout<<"for deposit,withdrawal or balance inquiry of account 1 press 1,for account two press 2.\n"; cin>>k;///////////////////// switch(k) { case 1: cout<<"for deposit press 1\nfor withdrawl press 2\nfor balance inquiry press 3\nto go to the other account press 4\nto exit press 5\n"; do { cin>>j; switch(j) { case 1: one.credit(); break; case 2: one.debit(); break; case 3: one.get_balance(); break; case 4: goto a2; case 5: break; } } while(j!=4 || j!=5); } } See I had made modifications without errors You have not declared an object you have used 'one' to call your functions in class but you have not declare that in main and other simple changes I have done Once check it
thankyou kishore!!just before viewing your answer,i tried to make the code once more and that time (thankfully) i did initiate the objects in main before using them in cases statements.however, there were still errors.after seeing your reply i studied that code once more and found out that the problem was this that i had initiated my objects in 'if' statement of 'if-else'.naturally, that meant that if the 'else' part becomes true as per user-entered-data then my objects would not be initiated(although i did use do-while statement to make sure that the user enters the character i want him to, otherwise it would become an infinite loop). what i mean to say is-thanks for helping me out!! God bless!