Results 1 to 20 of 20

Thread: c++ code help     submit to reddit submit to twitter

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

    c++ code help

    I keep geting a error in line 25 saying identifier expected but i cant figure it out for the life of me, can anyone help me out. C++ code btw. Line 25 is at the end of the class. Similar google errors have said that its missing a simicolen or } but i dont see anything.


    Code:
    #ifndef Account
    #define Account
    
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    class Account {
        private:
        string ownerFirstName;
        string ownerLastName;
        float balance;
    	float deposit;
    	float withdraw;
        public:
        account();
        account(string f, string l, float b, float d, float w);
        void depositFunds(float d);
        void withdrawFunds(float w);
        float getBalance();
    };
     
    Account::Account() {
    	ownerFirstName = Jhon;
        ownerLastName = Doe;
        balance = 2000;
    }
    
    Account::Account(string f, string l, float b, float w) {
        ownerFirstName = f;
        ownerLastName = l;
        balance = b;
    	deposit = d;
    	withdraw = w;
    }
    
    void Account::depositFunds(float d) {
    	cout << "How much would you like to deposit today: ";
    	cin >> deposit;
    	int total = balance + deposit;
    	cout << "You deposited", deposit," dollars." <<endl;
    	cout << "Current Total is", total," dollars." <<endl;
    	cout << " " << endl;
    }
    
    void Account::withdrawFunds(float w) {
    	cout << "How much would you like to withdraw: ";
    	cin >> withdraw;
    	int total2 = balance - withdraw;
    	cout << "You withdrew", withdraw, " dollars." <<endl;
    	cout << " Current Total is", total2," dollars." <<endl;
    	cout << " " << endl;
    }
    
    float Account::getBalance() {
    	cout << "Current account balance is", balance," dollars." << endl;
    	cout << " " << endl;
    }
    
    void main() {
        int choice = 0;
    	while choice !=4; {
    		cout << "What would you like to do"<< endl;
    		cout << "1)  Display Current Account Balance" <<endl;
    		cout << "2)  Deposit Money" <<endl;
    		cout << "3)  Withdraw Money"<< endl;
    		cout << "4)  Exit" << endl;
    		cout << "Please make a selection: ";
    		cin choice;
    	}
        while choice !=5; {
    		switch(choice) {
    			case '1':
    				cout << Account.getBalance();
    			break;
    			case '2':
    				cout <<Account.depositFunds();
    			break;
    			case '3':
    				cout <<Account.withdrawFunds();
            default:
    				cout << "Thank you for using our serivice"<<endl;
    				cout << "Goodbye" << endl;
    		}
        }
     }
    
    #endif

  2. #2
    Vic
    Vic is offline
    CoP Dynamis
    Join Date
    May 2006
    Posts
    251
    BG Level
    4

    In the class Account, your constructor is lowercase, whereas when its defined, its uppercase.

  3. #3
    Relic Horn
    Join Date
    Dec 2007
    Posts
    3,411
    BG Level
    7
    FFXIV Character
    Purrrfect Lee
    FFXIV Server
    Hyperion
    FFXI Server
    Cerberus

    edit: i wuz rong

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

    Well i've tried it both ways and it gives me 3 errors if i upper case the constructors.

    Line 18: decleration terminated incorrectly
    Line 19: ) expected
    Line 25: idintifier expected

  5. #5
    Relic Horn
    Join Date
    Dec 2007
    Posts
    3,411
    BG Level
    7
    FFXIV Character
    Purrrfect Lee
    FFXIV Server
    Hyperion
    FFXI Server
    Cerberus

    Additionally, you need quotes around Jhon and Doe in your empty constructor.

    And your constructor with parameters has 3 floats in the definition but only 2 later on where you declare it.

    And all of your cout lines with variables in them need << instead of "text",variable,"text" "text"<<variable<<"text".

    Cleaned up to the end but having trouble in the main(), doesn't look like there is an object of class Account created so nothing can store your data, and calling the member function fails it seems.

    The deposit branch of the switch does not pass in a float either.

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

    I suck at classes >< rather just use functions. Im not really sure how to make a object of accounts to hold the information

  7. #7
    Relic Horn
    Join Date
    Dec 2007
    Posts
    3,411
    BG Level
    7
    FFXIV Character
    Purrrfect Lee
    FFXIV Server
    Hyperion
    FFXI Server
    Cerberus

    Account variablename;

    Oh, forgot to add, in your while statements, you have to ( ) around the condition.
    while (stuff) {

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

    thanks

    Thanks i've edited all that weird that none of those proced errors. I'm still getting the same 3 errors but i have to go to work now till tonight, if i add the object you were talking about do you think it will fix those 3 errors?

    Code:
    #ifndef Account
    #define Account
    
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    class Account {
        private:
        string ownerFirstName;
        string ownerLastName;
        float balance;
    	float deposit;
    	float withdraw;
        public:
        Account();
        Account(string f, string l, float b, float d, float w);
        void depositFunds(float d);
        void withdrawFunds(float w);
        float getBalance();
    };
     
    Account::Account() {
    	ownerFirstName = "Jhon";
        ownerLastName = "Doe";
        balance = 2000;
    }
    
    Account::Account(string f, string l,float d, float b, float w) {
        ownerFirstName = f;
        ownerLastName = l;
        balance = b;
    	deposit = d;
    	withdraw = w;
    }
    
    void Account::depositFunds(float d) {
    	cout << "How much would you like to deposit today: ";
    	cin >> deposit;
    	int total = balance + deposit;
    	cout << "You deposited"<< deposit<<" dollars." <<endl;
    	cout << "Current Total is"<< total<<" dollars." <<endl;
    	cout << " " << endl;
    }
    
    void Account::withdrawFunds(float w) {
    	cout << "How much would you like to withdraw: ";
    	cin >> withdraw;
    	int total2 = balance - withdraw;
    	cout << "You withdrew"<< withdraw<< " dollars." <<endl;
    	cout << " Current Total is"<< total2<<" dollars." <<endl;
    	cout << " " << endl;
    }
    
    float Account::getBalance() {
    	cout << "Current account balance is"<< balance<<" dollars." << endl;
    	cout << " " << endl;
    }
    
    void main() {
        int choice = 0;
    	while (choice !=4) {
    		cout << "What would you like to do"<< endl;
    		cout << "1)  Display Current Account Balance" <<endl;
    		cout << "2)  Deposit Money" <<endl;
    		cout << "3)  Withdraw Money"<< endl;
    		cout << "4)  Exit" << endl;
    		cout << "Please make a selection: ";
    		cin >> choice;
    	}
        while (choice !=5) {
    		switch(choice) {
    			case '1':
    				cout << Account.getBalance();
    			break;
    			case '2':
    				cout <<Account.depositFunds();
    			break;
    			case '3':
    				cout <<Account.withdrawFunds();
            default:
    				cout << "Thank you for using our serivice"<<endl;
    				cout << "Goodbye" << endl;
    		}
        }
     }
    
    #endif

  9. #9
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    No need for intros, that's an insult.

    Your main problem is your #define Account then redefining it as a class a moment later.
    Maybe I fixed a few more things to get it to run...

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    class Account {
        private:
    		string ownerFirstName;
    		string ownerLastName;
    		float balance;
    		float deposit;
    		float withdraw;
        public:
    		Account();
    		Account(string f, string l, float b, float d, float w);
    		void depositFunds();
    		void withdrawFunds();
    		void getBalance();
    };
     
    Account::Account() {
    	ownerFirstName = "Jhon";
        ownerLastName = "Doe";
        balance = 2000;
    }
    
    Account::Account(string f, string l,float d, float b, float w) {
        ownerFirstName = f;
        ownerLastName = l;
        balance = b;
    	deposit = d;
    	withdraw = w;
    }
    
    void Account::depositFunds() {
    	cout << "How much would you like to deposit today: ";
    	cin >> deposit;
    	balance+=deposit;
    	cout << "You deposited "<< deposit<<" dollars." <<endl;
    	getBalance();
    }
    
    void Account::withdrawFunds() {
    	cout << "How much would you like to withdraw: ";
    	cin >> withdraw;
    	balance-=withdraw;
    	cout << "You withdrew "<< withdraw<< " dollars." <<endl;
    	getBalance();
    }
    
    void Account::getBalance() {
    	cout << "Current account balance is "<< balance<<" dollars." << endl;
    	cout << " " << endl;
    }
    
    void main() 
    {
    	Account MyAccount;
        int choice = 0;
    	do
    	{
    		cout << "What would you like to do"<< endl;
    		cout << "1)  Display Current Account Balance" <<endl;
    		cout << "2)  Deposit Money" <<endl;
    		cout << "3)  Withdraw Money"<< endl;
    		cout << "4)  Exit" << endl;
    		cout << "Please make a selection: ";
    		cin >> choice;
    		switch(choice) {
    			case 1:
    				MyAccount.getBalance();
    				break;
    			case 2:
    				MyAccount.depositFunds();
    				break;
    			case 3:
    				MyAccount.withdrawFunds();
    				break;
    			case 4:
    				cout << "Thank you for using our serivice"<<endl;
    				cout << "Goodbye" << endl;	
    				break;
    			default:
    				cout << "Invalid selection"<<endl;
    				break;
    		}
        }while(choice!=4);
     }

  10. #10
    E. Body
    Join Date
    Sep 2007
    Posts
    2,019
    BG Level
    7
    FFXI Server
    Fenrir

    The identifier expected error is almost definitely because you left out the quotes.

    Always remember:

    "awesome" = a string, char *, or char[]

    'a' = a char

    awesome = a variable.

    So if you said:

    Code:
    iam = awesome;
    it would set the value of "iam" to the value of "awesome", but I didn't define "awesome" so it would error.

    But this:

    Code:
    awesome = "awesome";
    iam = awesome;
    and

    Code:
    iam = "awesome"
    would both work

    Of course this is pseudo code, but you should get the point.

  11. #11
    Relic Horn
    Join Date
    Dec 2007
    Posts
    3,411
    BG Level
    7
    FFXIV Character
    Purrrfect Lee
    FFXIV Server
    Hyperion
    FFXI Server
    Cerberus

    I like snickysnacks' rendition, only issue is the parameter list flip flops b and d in the constructor definition/declaration. That would be an interesting thing to figure out at runtime lol.

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

    trying to go off example codes and thats how the example did it with their class. D is suposta represent deposit, b balance

  13. #13
    Murder machine with a motor in her nose
    Join Date
    Apr 2007
    Posts
    368
    BG Level
    4
    FFXI Server
    Carbuncle

    he means that in your constructor you have it one way but in the actual function it's reversed. Would be very confusing if you were using the constructor to make the function call since the actual function would be putting things in the wrong order when it was called:

    Account(string f, string l, float b, float d, float w);
    Account::Account(string f, string l,float d, float b, float w) {

  14. #14
    E. Body
    Join Date
    Sep 2007
    Posts
    2,019
    BG Level
    7
    FFXI Server
    Fenrir

    Quote Originally Posted by Skie View Post
    trying to go off example codes and thats how the example did it with their class. D is suposta represent deposit, b balance
    Skie-

    One important thing to note and something I've noticed about most good programmers...

    Proper spelling and capitalization is almost ALWAYS a sign of a good programmer.

    Code:
    int foo = 0;
    
    int bar = Foo + 3;
    ... won't compile, simply due to a wrong capitalization. And when you start having variables like tempWeaponDPSCalculationValue; or something, capitalization is going to be a very important thing to master.

    Take the time to make your english (or whatever your native language is) a bit more concise and you'll start to see that carry over into your programming.

    Programming is a VERY specific skill to have. You must remember your semicolons at the end of every line, you must follow proper indention and syntax for all your loops/if statements, and you must spell the functions and macros EXACTLY right otherwise your code won't compile.

    Also, the worst part, you'll spend a ton of time staring a simple code block trying to figure out why it won't compile only to realize you typed "Deposit" instead of "deposit" and you'll kick yourself

    Good luck!

  15. #15
    Naver
    Guest

    At least with Visual Studios, ctrl+space is a godsend.

  16. #16
    :3
    Join Date
    Nov 2006
    Posts
    653
    BG Level
    5

    I just started using Eclipse for java and it checks for errors on the fly, and gives you ways of fixing it, which I thought was amazing.

  17. #17
    Relic Shield
    Join Date
    Jul 2008
    Posts
    1,951
    BG Level
    6
    FFXIV Character
    Audrey Weaver
    FFXIV Server
    Behemoth
    FFXI Server
    Asura

    Visual Assist is an incredibly useful tool to have for this kind of thing.

  18. #18
    E. Body
    Join Date
    Sep 2007
    Posts
    2,019
    BG Level
    7
    FFXI Server
    Fenrir

    Quote Originally Posted by Kaelan? View Post
    Visual Assist is an incredibly useful tool to have for this kind of thing.
    this x 1000000000

    I cannot code in Visual Studio without Visual Assist anymore.

  19. #19
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    Quote Originally Posted by viperlasson View Post
    I just started using Eclipse for java and it checks for errors on the fly, and gives you ways of fixing it, which I thought was amazing.
    Just wait until you use the professional edition of intelliJ. I don't know how I code in anything else (like a putty console window for school).

  20. #20
    E. Body
    Join Date
    Sep 2007
    Posts
    2,019
    BG Level
    7
    FFXI Server
    Fenrir

    Quote Originally Posted by Eliseos View Post
    Just wait until you use the professional edition of intelliJ. I don't know how I code in anything else (like a putty console window for school).
    intellij is sweeeeeeet for java.

Similar Threads

  1. Java Code help
    By Skie in forum Tech
    Replies: 11
    Last Post: 2010-10-11, 14:37
  2. c++ code help version 3.
    By Skie in forum Tech
    Replies: 3
    Last Post: 2009-12-04, 17:56
  3. CS Code help, number program
    By Skie in forum Tech
    Replies: 8
    Last Post: 2009-11-13, 13:57
  4. Windows XP code help
    By Etice in forum Tech
    Replies: 3
    Last Post: 2009-07-16, 14:25
  5. Help please, POL Error codes
    By Hubie in forum Tech
    Replies: 3
    Last Post: 2008-04-13, 19:25