Problems?

This is a discussion on Problems? within the C++ Programming forums, part of the General Programming Boards category; Im pretty new to C++, This being my first attempt at classes, anyone know what the problem with this code ...

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Problems?

    Im pretty new to C++, This being my first attempt at classes, anyone know what the problem with this code it?
    Code:
    #include "stdafx.h"
    #include <iostream.h>
    typedef unsigned short int USHORT;
    int main(int argc, char* argv[])
    {
    	class Employee
    	{
    	public:
    		float salary;
    		USHORT age;
    		char level;
    		USHORT yearswithcompany;
    		float worth(USHORT yearswithcompany, float salary)
    		{
    			int temp = yearswithcompany * 12;
    			int temp = temp * salary;
    			return temp;
    		}
    	}
    
    	Employee Evan;
    	Evan.salary = 3400.23;
    	Evan.age = 14;
    	Evan.level = 'Manager'
    	Evan.yearswithcompany = 4;
    	Evan.worth(Evan.age, Evan,salary)
    	cout << "Evans earnings with the company total" << Evan.worth << "\n";
    
    	return 0;
    }
    Stupid example.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I don't know about you, but my compiler won't let me flesh out the functions inside the class definition. Try something like this and see if that works better.

    Code:
    class Employee
    {
      public:
        char *level;
        float salary;
        int age;
        int yearswithcompany;
        float worth(int, float);
    }
    
    float Employee::worth(int x, float y){
      float temp;
      temp = (float)x * 12;
      temp *= y;
      return temp;
    }
    You were also trying to return an int when the function was supposed to return a float. Also, I changed your unsigned shorts to regular ints, it really has no bearing on how this particular code will function so if you need unsigned short, then feel free to change it back.

    Keep in mind that you can't send a string to a char data type, it has to be either a char* or an array.

    -Prelude
    Last edited by Prelude; 09-17-2001 at 05:07 PM.

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    1. While it is possible to declare and define classes inside functions, they aren't very useful there and should be placed globally.

    2. You are returning a float from your worth() member function, but the calculations are done with integers so the return value would be trunciated.

    3. 'char level' allocates enough storage for one character only. If you want to store a string you either need a character array or use the C++ string class.

    4. You call the worth function that returns a float but do not store this value anywhere and then try to access it in your output statement. You either want to create another variable in your class to store this info, use a variable in the calling function or call the worth() function directly in your output statement.


    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    typedef unsigned short int USHORT;
    
    class Employee
    	{
    	public:
    		double salary;
    		USHORT age;
    		string level;
    		USHORT yearswithcompany;
    		double worth(USHORT yearswithcompany, float salary)
    		{
    			double temp = yearswithcompany * 12;
    			temp = temp * salary;
    			return temp;
    		}
    	};
    
    
    int main(int argc, char* argv[])
    {
    	
    
    	Employee Evan;
    	Evan.salary = 3400.23;
    	Evan.age = 14;
    	Evan.level = "Manager";
    	Evan.yearswithcompany = 4;
    	
    	cout << "Evans earnings with the company total: " <<setprecision(8)
                    << Evan.worth(Evan.age, Evan.salary)<< "\n";
    
    	return 0;
    }

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Wow, it works Thanks a bunch, but theres one part I don't understand, what does "setprecision(8)" do?

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    setprecision sets the number of digits displayed after the decimal point on floating point numbers.
    In your original post, you also forgot the ; after the class definition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 03:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21