Page 1 of 2 1 2 LastLast
Results 1 to 20 of 33

Thread: more code troubles c++     submit to reddit submit to twitter

  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    more code troubles c++

    trying to get this code to work

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    
    class Account {
        private:
        float balance;
    	float deposit;
    	float withdraw;
    	
        public:
        Account(float balance, float deposit, float withdraw);
        void depositFunds(float balance);
        void withdrawFunds(float balance);
        float getBalance(float balance);
    };
    
    /*Account::Account(float balance, float deposit, float withdraw) {
    	balance = 2000;
    	deposit = 0;
    	withdraw = 0;
    }*/
    
    void depositFunds(float balance) {
    	float numb = 0;
    	cout << "How much would you like to deposit: " << endl;
    	cin >> numb;
    	float dtotal = balance + numb;
    	cout << "You have deposited "<<dtotal<<" dollars"<< endl;
        system ("PAUSE");
    }
    
    void withdrawFunds(float balance) {
    	float numb = 0;
    	cout << "How much would you like to withdraw today: " << endl;
    	cin >> numb;
    	float wtotal = balance - numb;
    	cout << "You have withdrawn "<<wtotal<<" dollars" << endl;
        system ("PAUSE");
    }
    
    float getBalance(float balance) {
    	balance = balance;
    	return balance;
    }
    
    /*class Transaction {
    	private:
    		vector <string> transactionRecord;
    	public:
    		void addNewTransaction(string);
    };*/
    
    void main() {
        int menu;
        int trigger = 0;
    	float balance = 2000;
    	Account myAccount = (balance);
    
     	while(trigger==0){
    		cout << "1. Get a balance inquiry." << endl;
            cout << "2. Make a deposit." << endl;
    		cout << "3. Make a withdrawal." << endl;
    		cout << "4. Recent Transactions." << endl;
    		cout << "5. Exit." << endl;
    		cout << "what would you like to do?:";
    		cin >> menu;
            switch (menu){
                case 1:
    				//Current balance
    				cout << myAccount.getBalance(balance);
                    break;
                case 2://does the deposit
    				cout << myAccount.depositFunds(balance);
    				break;
                case 3://does the withdrawal
    				cout << myAccount.withdrawFunds(balance);
    				break;
    			case 4://Recent Transactions
    				//lalalal
    				break;
                default:// exit out the loop
    				cout << "Thank you for using our serivice"<<endl;
    				cout << "Goodbye" << endl;
                    system ("PAUSE");
    				trigger=1;
            }
            system ("cls");
        }
    }

    I'm geting 3 errors now.

    1) Line 61: Can not convert float to account in function main()

    Code:
    Account myAccount = (balance);

    2) Line 77: Operator<< not implemented in type ostream for arguments of type void in function main()

    Code:
    cout << myAccount.depositFunds(balance);
    3) Line 80: Operator << not implemented in type ostream for augments of type void in function main ()

    Code:
    cout << myAccount.withdrawFunds(balance);
    Can anyone help me out in getting these 3 errors worked out

  2. #2
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    got rid of error 2-3 by deleting the cout statment. but still having trouble with error 1

  3. #3
    E. Body
    Join Date
    Jun 2005
    Posts
    2,226
    BG Level
    7
    FFXI Server
    Caitsith

    Quote Originally Posted by Skie View Post
    trying to get this code to work

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    
    class Account {
        private:
        float balance;
    	float deposit;
    	float withdraw;
    	
        public:
        Account(float balance, float deposit, float withdraw);
        void depositFunds(float balance);
        void withdrawFunds(float balance);
        float getBalance(float balance);
    };
    
    /*Account::Account(float balance, float deposit, float withdraw) {
    	balance = 2000;
    	deposit = 0;
    	withdraw = 0;
    }*/
    
    void depositFunds(float balance) {
    	float numb = 0;
    	cout << "How much would you like to deposit: " << endl;
    	cin >> numb;
    	float dtotal = balance + numb;
    	cout << "You have deposited "<<dtotal<<" dollars"<< endl;
        system ("PAUSE");
    }
    
    void withdrawFunds(float balance) {
    	float numb = 0;
    	cout << "How much would you like to withdraw today: " << endl;
    	cin >> numb;
    	float wtotal = balance - numb;
    	cout << "You have withdrawn "<<wtotal<<" dollars" << endl;
        system ("PAUSE");
    }
    
    float getBalance(float balance) {
    	balance = balance;
    	return balance;
    }
    
    /*class Transaction {
    	private:
    		vector <string> transactionRecord;
    	public:
    		void addNewTransaction(string);
    };*/
    
    void main() {
        int menu;
        int trigger = 0;
    	float balance = 2000;
    	Account myAccount = (balance);
    
     	while(trigger==0){
    		cout << "1. Get a balance inquiry." << endl;
            cout << "2. Make a deposit." << endl;
    		cout << "3. Make a withdrawal." << endl;
    		cout << "4. Recent Transactions." << endl;
    		cout << "5. Exit." << endl;
    		cout << "what would you like to do?:";
    		cin >> menu;
            switch (menu){
                case 1:
    				//Current balance
    				cout << myAccount.getBalance(balance);
                    break;
                case 2://does the deposit
    				cout << myAccount.depositFunds(balance);
    				break;
                case 3://does the withdrawal
    				cout << myAccount.withdrawFunds(balance);
    				break;
    			case 4://Recent Transactions
    				//lalalal
    				break;
                default:// exit out the loop
    				cout << "Thank you for using our serivice"<<endl;
    				cout << "Goodbye" << endl;
                    system ("PAUSE");
    				trigger=1;
            }
            system ("cls");
        }
    }

    I'm geting 3 errors now.

    1) Line 61: Can not convert float to account in function main()

    Code:
    Account myAccount = (balance);

    2) Line 77: Operator<< not implemented in type ostream for arguments of type void in function main()

    Code:
    cout << myAccount.depositFunds(balance);
    3) Line 80: Operator << not implemented in type ostream for augments of type void in function main ()

    Code:
    cout << myAccount.withdrawFunds(balance);
    Can anyone help me out in getting these 3 errors worked out
    based upon your code, you're assigning a float to an account, not assigning the component of an account that is a float to a float.

    Account myAccount;
    myAccount.balance = balance;

    The above would assign the float balance, to myAccount.balance. Also, you might be better served simply using...

    Account myAccount;
    myAccount.balance = 2000;

    ...if your assigning a variable that is used just for that.

    edit; also, to output fields from a class merely reference that field in a cout:

    cout << "Balance: " << myAccount.balance << endl;
    cout << "Deposit: " << myAccount.desposit << endl;

  4. #4
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    I did that and it worked thanks! but now it fliped out saying:

    Error: Unresolved external 'Account::getBalance(float)' referenced from (c:\USERS\WHITNEY\DOWNLOADS\DESKTOP\ACCOUNT3.OBJ


    Error: Unresolved external 'Account::depositFunds(float)' referenced from (c:\USERS\WHITNEY\DOWNLOADS\DESKTOP\ACCOUNT3.OBJ


    Error: Unresolved external 'Account::withdrawFunds(float)' referenced from (c:\USERS\WHITNEY\DOWNLOADS\DESKTOP\ACCOUNT3.OBJ

  5. #5
    E. Body
    Join Date
    Jun 2005
    Posts
    2,226
    BG Level
    7
    FFXI Server
    Caitsith

    imho, i don't like how you've named a class member after multiple parameters in your functions. it's rather confusing when reading through code if you reuse the same variable multiple times. you can use balance as a parameter for your class functions, but i'd use something like acctBalance for the class member.

    for example:

    Code:
    class Circle: public Shape {
    public:
        void area();
        void volume();
        void perimeter();
        
        Circle(int r) {
              radius = r;
        }
        
         ~Circle(){}
        
    protected:
        int radius;
    };
    I don't know if the multiple uses of balance are causing that error. if you made the adjustments I said in my last post, your main should be ok from what I can tell (in class browsing /BG/ so i haven't looked into it too much. I would recommend using a different name for your class member. For future reference, unresolved external is usually an error with linking your program, which means you've got a static data member name issue, or the like.



    EDIT: doh, i missed the obvious, lol. You need to make your functions part of your class.

    void Account::despositFunds(...);

    for example

  6. #6
    CoP Dynamis
    Join Date
    Jun 2006
    Posts
    255
    BG Level
    4
    FFXIV Character
    Kellen Amarian
    FFXIV Server
    Hyperion
    FFXI Server
    Gilgamesh

    Missing the "Account::" when you define your class methods right?

    Account::depositFunds
    Account::withdrawFunds
    Account::getBalance

  7. #7
    E. Body
    Join Date
    Jun 2005
    Posts
    2,226
    BG Level
    7
    FFXI Server
    Caitsith

    Quote Originally Posted by Kellen View Post
    Missing the "Account::" when you define your class methods right?

    Account::depositFunds
    Account::withdrawFunds
    Account::getBalance
    yea, i edit'd my post like 4 seconds after i finished it, lol. I just read through the code as it was posting. Stupid Operating Systems lecture is putting me to sleep. D: damned semaphores, yawn.

  8. #8
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Kellen's solution fixed it thanks!

  9. #9
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Everything works near perfect, i need to set the balance into a array so its static and not defalting to 2000 everything but i'll worry about that later. Now i need to make a new class that records everything (use vector?) what would be the best way to do this? Was going to do like:

    class Transaction {
    private:
    vector <string> transactionRecord;
    public:
    void addNewTransaction(string transactionRecord);
    };

    void addNewTransaction(string transactionRecord) {
    for (int i=0; i<transactionRecord.size(); i++) {
    cout<<transactionRecord.at(0)<<"\n";
    }

  10. #10
    E. Body
    Join Date
    Jun 2005
    Posts
    2,226
    BG Level
    7
    FFXI Server
    Caitsith

    a vector should be fine since it should automatically scale. the only problem would be running out of space on the heap, but i doubt that would be an issue for most applications of your code. also, transactionRecord.at(0) in that look accesses the same record every time in that for loop. I would say make an int transNumbers, and increase it from 0 -> some max, so in your for loop it can know which transaction it needs.

  11. #11
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    your variable naming conventions are soooo weird holy shit.

    Code:
    class Transaction {
    private:
    vector <string> transactionRecord;
    public:
    void addNewTransaction(string transactionRecord);
    };
    
    void addNewTransaction(string transactionRecord) {
    for (int i=0; i<transactionRecord.size(); i++) {
    cout<<transactionRecord.at(0)<<"\n";
    }
    naming everything transactionRecord, especially since you name a string and a vector that way, is going to yield unreadable code. I realize this is probably from the hip pseudocode you haven't tried to compile yet, but get in the habit, even in pseudocode, of making things readable.

    Your addNewTransaction fuction isn't doing what you want it to (which presumably is to add a value to the end of your transactions vector. It's always going to be trying to print out the values in your vector instead of adding anything to it. You probably want (once you've written the constructor) something more like

    Code:
    void addNewTransaction(string transaction) {
    	transactionRecord.push_back(transaction);
    }
    You probably haven't been introduced to iterators yet so I guess if you want a printTransactions() function the indexed for loop you have now is appropriate.

  12. #12
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Octavious View Post
    a vector should be fine since it should automatically scale. the only problem would be running out of space on the heap, but i doubt that would be an issue for most applications of your code. also, transactionRecord.at(0) in that look accesses the same record every time in that for loop. I would say make an int transNumbers, and increase it from 0 -> some max, so in your for loop it can know which transaction it needs.

    I'm wanting to print every transaction that has occured sence the program was opened. i was thinking maybe have that every time the user inputed something other than 4-5 it would add one to the count then just do a for loop to output based on the counter

  13. #13
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    Quote Originally Posted by Skie View Post
    I'm wanting to print every transaction that has occured sence the program was opened. i was thinking maybe have that every time the user inputed something other than 4-5 it would add one to the count then just do a for loop to output based on the counter
    I really don't understand this part of the sentence. vectors also know how long they are, so there's no reason to track a separate counter.

    It also may be overkill to define a whole other class to manage transactions when all you are really doing is wrapping function names around basic vector usage. Unless the assignment demands a new class you can just define a variable in main() and use it to store transactions as they are used.

  14. #14
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    What im wanting to do is make a vector class. When the user chooses to deposit/withdraw money it pushes that into the vector. When the user chooses option 4, it prints and everything the user did.

    Is this right? or am i doing it wrong

    Code:
    class Transaction {
    	private:
    		vector <string> transactionRecord;
    	public:
    		void addNewTransaction(string transactionRecord);
    };
    
    void Transaction::addNewTransaction(string transactionRecord) {
    	for (int i=0; i<transactionRecord.size(); i++) {
    	cout<<transactionRecord.at(i)<<"\n";
    }

    Assignment demands that i do a totaly new class

  15. #15
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    I'm not clear on the encapsulation requirements of this assignment, but from the looks of things you should be pushing new transactions to your recording class every time a transaction (withdraw/deposit) hapens, and just loop through it when someone asks for it. There's no reason at all to keep a counter, vector.size() tells you how big it is.

  16. #16
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    ok i wont use a counter. in my class (stated below) im geting a error saying

    comparing signed and unsigned values in function Transaction::addNewTransaction

    what does that mean?

  17. #17
    Fake Numbers
    Join Date
    Jul 2008
    Posts
    90
    BG Level
    2

    That's probably a warning and not an error. transactionRecord.size() is an unsigned integer, and you're comparing it to a signed integer (i). The compiler is warning you that this may or may not be what you want. You can safely ignore the message, or make i an unsigned int, or cast transactionRecord.size() to an int.

  18. #18
    blax n gunz
    Join Date
    May 2005
    Posts
    11,141
    BG Level
    9

    Okay I finally read your code. It doesn't do anything you really want it to do.

    I imagine that your teacher hasn't reinforced the idea of setters/getters yet, but

    Code:
    float getBalance(float balance) {
    	balance = balance;
    	return balance;
    }
    Is going to mess you up bigtime, since you're only supposed to be returning data here, not modifying it and then returning it. At it's core your Account class should really do most of the work, and it's up to you to determine where it's best to keep a record of the transactions.

    Code:
    class Account {
    	private:
    		float balance;
    		vector <string>session;
    
    	public:
    		Account (float startingBalance);
    		void depositFunds(float amount);
    		void withdrawFunds(float amount);
    		float getBalance();
    
    
    		void printTransactions();
    		void clearTransactions();
    }
    When I learned c++ I was taught specifically to write all the declarations for my classes first so I wouldn't get confused about the function signatures/prototypes later. This more or less models a basic savings account. the get() function takes no arguments and returns a value. The withdraw()/deposit() functions take arguments and return nothing (error-reporting notwithstanding).

    the printTransactions()/clearTransactions() assume that within withdraw() and deposit() you are adding items to the vector named session, and it is the job of print() to iterate through that vector and print to cout, and clear() to zero it out for whatever reason you wish.

    IF your assignment does not require that much encapsulation then your main() function will have to keep track of the transaction records itself.

  19. #19
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    ugh your right, was a warringing the error is:

    expression syntax in function Transaction::addNewTransaction

  20. #20
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    He wants the vector in its own class >< i think it would be much easer to just add it to the account class but its what he wants.

Page 1 of 2 1 2 LastLast

Similar Threads

  1. more java code
    By Skie in forum Tech
    Replies: 0
    Last Post: 2010-09-24, 20:14