in case statements compiler says that my object is indeclared identifier.why?

Discussion in 'C++' started by sam, Sep 27, 2011.

  1. sam

    sam New Member

    Joined:
    Jun 4, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. gpk kishore

    gpk kishore New Member

    Joined:
    Jun 30, 2011
    Messages:
    82
    Likes Received:
    0
    Trophy Points:
    0
    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
    :)
     
  3. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    make sure you use Code Blocks for code snippets in posts
     
  4. sam

    sam New Member

    Joined:
    Jun 4, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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!:)
     
  5. sam

    sam New Member

    Joined:
    Jun 4, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    ok,sorry,i didnt know that....
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice