Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 31

Thread: Why am i getting a invalid use of Class?

  1. #21
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    Oh i see. hmmm look like mi really affi guh read da c++ how to program book deh sigh.. to many pages.

    So i edited the program to still use the menu but not to ask the user to enter the opening balance

    Code:
    #include <iostream>
    
    using namespace std;
    
    /*
     * 
     */
    
    int choice;
    int choice2;
    
    class Account {
        
    private:
        int balance;
        
    public:
        Account(){
            
            this->balance = 0;
        }
            
        Account(int a);
        int creditAccount(int);
        int debitAccount(int);
        void getBalance();
    };
    
    void menu();
    void submenu();
    
    int main(int argc, char** argv) {
        
        int amount;
        
        Account user1(1000);
        Account user2(500);
        
        do {
            
            menu();
    
            cout << "Choice: ";
            
            cin >> choice;
            
            switch (choice) {
                
                case 1:
                    
                    submenu();
                    cout << "Choice: ";
                    cin >> choice2;
                    
                    switch (choice2){
                        
                        case 1:
                            
                            user1.getBalance();
                            break;
                            
                        case 2:
                            
                            user2.getBalance();
                            break;
                            
                        case 9:
                            break;
                    }
                    break;
                    
                case 2:
                    
                    submenu();
                    cout << "Choice: ";
                    cin >> choice2;
                    
                    switch (choice2){
                        
                        case 1:
                            
                            cout << "Please Enter Lodgement amount: ";
                            cin >> amount;
                            user1.creditAccount(amount);
                            break;
                            
                        case 2:
                            
                            cout << "Please Enter Lodgement amount: ";
                            cin >> amount;
                            user2.creditAccount(amount);
                            break;
                            
                        case 9:
                            break;
                    }
                    break;
                    
                case 3:
                    submenu();
                    cout << "Choice: ";
                    cin >> choice2;
                    
                    switch (choice2){
                        
                        case 1:
                            
                            cout << "Please Enter Withdrawal amount: ";
                            cin >> amount;
                            user1.debitAccount(amount);
                            break;
                            
                        case 2:
                            
                            cout << "Please Enter Withdrawal amount: ";
                            cin >> amount;
                            user2.debitAccount(amount);
                            break;
                            
                        case 9:
                            break;
                    }
                    break;               
                    
                case 9:
                    
                    cout << "Thanks for Processing! Have a good Day!" << endl;
                    break;
                    
                default:
                    
                    cout << "Invalid Option Selected. Please Try again!" << endl;
                    break;
                    cout << "Choice: ";
                    cin >> choice;
                
            }
                    
            
        }while (choice != 9);
    
        return 0;
    }
    
    void menu(){
        
        cout << "###############################################################" << endl << 
                "##                  Account Transaction Menu                 ##" << endl <<
                "##                                                           ##" << endl <<
                "##                                                           ##" << endl <<
                "##   Please select the number for the option needed:         ##" << endl <<
                "##                                                           ##" << endl <<
               // "##   1. Enter Account Opening Balance                        ##" << endl <<
                "##   1. Check Account Balance                                ##" << endl <<
                "##   2. Make Account Lodgement                               ##" << endl <<
                "##   3. Make Withdrawal from Account                         ##" << endl <<
                "##   9. Exit                                                 ##" << endl <<
                "##                                                           ##" << endl <<
                "###############################################################" << endl;
    }
    
    void submenu(){
        
        cout << "###############################################################" << endl << 
                "##                  Account Transaction Menu                 ##" << endl <<
                "##                                                           ##" << endl <<
                "##                                                           ##" << endl <<
                "##   Please select the number for the option needed:         ##" << endl <<
                "##                                                           ##" << endl <<
                "##   1. User1                                                ##" << endl <<
                "##   2. User2                                                ##" << endl <<
                "##   9. Back to Main                                         ##" << endl <<
                "##                                                           ##" << endl <<
                "###############################################################" << endl;
    }
    
    Account::Account(int a){
        
        if (a >= 0){
            
            this->balance = a;
        }
        else {
            
            cout << "Invalid Amount Entered" << endl;
            this->balance = 0;
        }
    }
    
    int Account::creditAccount(int a){
        
        this->balance += a;
    }
    
    int Account::debitAccount(int a){
        
        if((this->balance - a) >= 0)
            {
                this->balance -= a;
            }else{
                cout << "Error: insufficient funds." << endl;;
            }
    }
    
    void Account::getBalance(){
        
        cout << "The current available balance is: " << this->balance << endl << endl;
        cin.get();
    }
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  2. #22
    Join Date
    Jun 2008
    Posts
    74
    Rep Power
    0

    Default

    Is this a term project or just a class assignment?

  3. #23
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    Its a class assignment. Plan to revisit this later on and see if i can get the initialization done after user enters amounts
    Last edited by lovepython; Oct 21, 2012 at 05:57 PM.
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  4. #24
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    Quote Originally Posted by owen View Post
    I have to agree with Arch_Angel on this point. Learning programming concepts and playing follow the leader on the internet are 2 different things. Global functions are perfectly fine and are a core feature of all important programming languages.
    its not follow the leader, its common sense. when you use a lot of globals you lose sight of what the code is or was about.
    lovepython had about three global functions related to creating a menu. i say that calls for a class
    1337

  5. #25
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    The global functions was for a menu and submenu. How would i have implemented the 2 different menus in a class?
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  6. #26
    Join Date
    Jun 2008
    Posts
    74
    Rep Power
    0

    Default

    Quote Originally Posted by wiel View Post
    its not follow the leader, its common sense. when you use a lot of globals you lose sight of what the code is or was about.
    lovepython had about three global functions related to creating a menu. i say that calls for a class
    Gee, I wonder how do hundreds (maybe thousands) of programmers keep track of what their code is or what it is about in programs that span millions of lines of code and some across multiple languages without using classes and/or namespaces? Example: The linux kernel is written in C and assembly.

    The global functions was for a menu and submenu. How would i have implemented the 2 different menus in a class?
    Do not use classes just for the sake of using classes.

  7. #27
    Join Date
    Jul 2002
    Posts
    1,395
    Rep Power
    0

    Default

    Quote Originally Posted by jayrulez View Post
    @lovepython yes, it is that easy. It meets all the requirements specified.

    @other guys, using global functions in c++ is completely fine and there is no justified recommendation against using them where they make sense.
    My 1 argument against global functions (due to my lacking knowledge of programming and C++) is what my lecturer says [or something like]:
    "OO is the winning feature of C++; global functions and variables break encapsulation and OOP."
    With global functions and variables, you have to declare everything before main() while with OO classes fall anywhere. And there is also some conflict with POD (Plain Old Data), that I don't remember.

  8. #28
    Join Date
    Apr 2003
    Posts
    13,269
    Rep Power
    34

    Default

    We can start a new thread debating the pro's and con's of global functions, but lets keep this thread about helping the OP with his code.
    His global functions work for what he is trying to accomplish, so he doesn't really need to change it to classes or namespaces at this point.
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2008

  9. #29
    Join Date
    Sep 2005
    Posts
    2,394
    Rep Power
    0

    Default

    I submitted the working code but will be tackling the lecturer about how i can accomplish getting the figures from the user and then passing it to the contructor so the other functions can access it
    To find what you seek in the road of life, the best proverb of all is that which says: "Leave no stone unturned." Edward Bulwer Lytton

  10. #30
    Join Date
    Jun 2008
    Posts
    74
    Rep Power
    0

    Default

    Quote Originally Posted by lovepython View Post
    I submitted the working code but will be tackling the lecturer about how i can accomplish getting the figures from the user and then passing it to the contructor so the other functions can access it
    I modified the previous example to accept the opening balance from the user.

    Can i get a simplified explanation of what this-> does?
    "this" is a keyword which represents a pointer to the instance of the containing class.
    Last edited by jayrulez; Oct 22, 2012 at 01:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •