Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 31

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

  1. #11
    Join Date
    Jun 2008
    Posts
    74
    Rep Power
    0

    Default

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Account
    {
    private:
        int balance;
    
    public:
        Account() {
            this->balance = 0;
        }
    
        Account(int balance) {
            if(balance >= 0)
            {
                this->balance = balance;
            }else{
                this->balance = 0;
                cout << "Error: invalid balance." << endl;
            }
        }
    
        int getBalance() { return this->balance; }
    
        void debit(int amount) {
            if((this->balance - amount) >= 0)
            {
                this->balance -= amount;
            }else{
                cout << "Error: insufficient funds." << endl;;
            }
        }
        void credit(int amount) { this->balance += amount; }
    };
    
    int main()
    {
        int amount, oBal;
    	
    	cout << "Enter the opening balance for account 1: ";
    	cin >> oBal;
    
    	Account account(oBal);
    
    	cout << "TEST 1" << endl << "===============================================================================" << endl;
    
    	cout << "Account balance: " << account.getBalance() << endl;
    
    	cout << "Enter amount to withdraw: ";
    	cin >> amount;
    
    	account.debit(amount);
    
    	cout << "Account balance: " << account.getBalance() << endl;
    
    	cout << "Enter the opening balance for account 2: ";
    	cin >> oBal;
    	
        Account account2(oBal);
    
        cout << endl << endl;
    
        cout << "TEST 2" << endl << "===============================================================================" << endl;
    
        cout << "Account balance: " << account2.getBalance() << endl;
    
        cout << "Enter amount to deposit: ";
        cin >> amount;
    
        account2.credit(amount);
    
        cout << "Account balance: " << account2.getBalance() << endl;
    
        return 0;
    }
    Last edited by jayrulez; Oct 22, 2012 at 01:00 PM.

  2. #12
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    yes those menu functions could be in a menu class. are you saying that because you are using a loop, the value of balance in the user1 object gets deleted when you leave a case? that probably happens because the braces {} you put in a case added a scope to each case

    edit: not sure how you are going to ask the user for a value and then set it with a constructor then. maybe a brighter person can answer
    Last edited by wiel; Oct 20, 2012 at 08:05 PM.
    1337

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

    Default

    I was actually wondering if i may have read into the question too much and said that the user must enter it but instead the initial amount should be fed at the creation of the object. Thanks for the input on the menu will try to adopt that. Yes its seems once you brace the case it only works for that case or the switch itself.

    @jayrulez you tell me that i be reading into the whole question? Is it that simple a solution?
    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. #14
    Join Date
    Apr 2003
    Posts
    13,164
    Rep Power
    29

    Default

    Quote Originally Posted by wiel View Post
    oh and dont use global functions in c++, bad practice
    Since he is doing a project for school, he has to go along with what the lecturer is teaching him. While it may be bad practice outside of the classroom, he will have to use global functions in his project since that is how the lecturer is teaching it.

    My lecturer used global functions as well. He can stop using it if he continues to program after his course is done.
    "The best software is the one that fits your needs." - A_A

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

  5. #15
    Join Date
    Feb 2003
    Posts
    3,063
    Rep Power
    0

    Default

    Quote Originally Posted by Arch_Angel View Post
    Since he is doing a project for school, he has to go along with what the lecturer is teaching him. While it may be bad practice outside of the classroom, he will have to use global functions in his project since that is how the lecturer is teaching it.

    My lecturer used global functions as well. He can stop using it if he continues to program after his course is done.
    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.

  6. #16
    Join Date
    Jul 2002
    Posts
    1,388
    Rep Power
    0

    Default

    Quote Originally Posted by Arch_Angel View Post
    Since he is doing a project for school, he has to go along with what the lecturer is teaching him. While it may be bad practice outside of the classroom, he will have to use global functions in his project since that is how the lecturer is teaching it.

    My lecturer used global functions as well. He can stop using it if he continues to program after his course is done.
    Our lecturer taught us namespaces. Further reading advise using named and unnamed namespaces as "function containers" [my words]. Since it's C++, he would be using what he learnt right?
    Sharing is caring? Then what if I care too much? A precarious situation, indeed...
    http://ip-check.org/ip-check.png

  7. #17
    Join Date
    Jun 2008
    Posts
    74
    Rep Power
    0

    Default

    @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.

  8. #18
    Join Date
    Apr 2003
    Posts
    13,164
    Rep Power
    29

    Default

    Quote Originally Posted by carey View Post
    Our lecturer taught us namespaces. Further reading advise using named and unnamed namespaces as "function containers" [my words]. Since it's C++, he would be using what he learnt right?
    I learned global functions and wasn't taught namespaces in C++ at school. I learned namespaces when doing C#.
    And yes, if he was taught namespaces, then he should utilise what he has been taught.

    But nothing stopping him from showing initiative and reading up on namespaces and implementing it in his project. I was given bonus marks for that. (showing initiative)
    "The best software is the one that fits your needs." - A_A

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

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

    Default

    Yea i have been taught namespace and global functions. From what i have learnt for namespace std, it males life a bit easier than to be calling the std for every instance it is used. If it was loaded then every standard library function would have to be preceeded by std eg. std::cout.

    I always try to learn things that aren't really taught in the class. I did alot of that when i did web applications last year. Some things in css that i learnt amazed the class even the teacher giving me the best looking and functional site of the 2 groups

    Am gonna redo the program in a bit and let u guys know what i come up with.

    Can i get a simplified explanation of what this-> does?
    Last edited by lovepython; Oct 21, 2012 at 03:49 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

  10. #20
    Join Date
    Apr 2003
    Posts
    13,164
    Rep Power
    29

    Default

    Quote Originally Posted by lovepython View Post
    Yea i have been taught namespace and global functions. From what i have learnt for namespace std, it males life a bit easier than to be calling the std for every instance it is used. If it was loaded then every standard library function would have to be preceeded by std eg. std::cout.
    No man, not talking the using namespace std;.
    See here: http://www.cplusplus.com/doc/tutorial/namespaces/
    "The best software is the one that fits your needs." - A_A

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

Posting Permissions

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