Thread: need some help?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    14

    need some help?

    hey this is my first time writting C++ code and im having a bit of trouble switching over from c. i was wondering if someone wouldnt mind having a look at my code to see if they can spot anything that seems wrong. any help would be appreciated. thanks


    employee.cpp
    Code:
    #include <iostream>
    #include <string>
    
    #include "employee.h"
    
    using namespace std;
    
    void ReadEmployees( Employee * a, int N)
    {
    
    	int i;
    
    	for(i = 0 ; i < N ; i++)
    	{
    		a[i] = CollectData();
    	}
    
    }
    
    double FindLowestSalary( Employee * a, int N)
    {
    	double low = a[0].salary; //salary of first employee
    	int i;
    
    	for(i = 0 ; i < N ; i++) 
    	{
    		if (a[i].salary <= low)
    			low = a[i].salary;
    	}
    
    	return low;
    }
    
    double FindHighestSalary(Employee * a, int N)
    {
    	double max = a[0].salary; //salary of first employee
    	int i;
    
    	for(i = 0 ; i < N ; i++) 
    	{
    		if (a[i].salary > max)
    			max = a[i].salary;
    	}
    
    	return max;
    
    }
    
    Employee CollectData()
    {
    	cout << "Entering Employee Data\n";
    	Employee * e;
    	cout << "Enter ID:  ";
    	cin >> e.ID;
    	cin.ignore();
    
    	cout << "Enter first name: ";
    	getline(cin, e.firstname);
    	cin.ignore();
    
    	cout << "Enter last name: ";
    	getline(cin, e.lastname);
    	cin.ignore();
    
    	cout << "Enter address: ";
    	getline(cin, e.address;
    	cin.ignore();
    
    	cout << "Enter city: ";
    	getline(cin, e.city);
    	cin.ignore();
    
    	cout << "Enter state: ";
    	getline(cin, e.state);
    	cin.ignore();
    
    	cout << "Enter zipcode: ";
    	cin >> e.zipcode;
    	cin.ignore();
    
    	cout << "Enter phone number: ";
    	cin >> e.phonenumber;
    
    	cout << "Enter salary: ";
    	cin >> e.salary;
    
    	cout << endl << endl;
    
    	return e;
    }
    
    Employee RasieSalary(Employee * a, int N)
    {
    	int i;
    	for(i = 0; i < N ; i++)
    	{
    		a[i] = (.06 * a[i]) + a[i];
    	}
    	return Employee;
    }
    
    void DisplayArrayOfEmployees(Employee * a, int N)
    {
    	int i;
    	
    	for ( i = 0; i < N; i++)
    	{
    		a[i] = DisplayEmployee();
    }
    
    void DisplayEmployee(Employee e)
    {
    
    	cout << e.ID << "\t" << e.lastname << "\t" << e.salary << endl;
    
    }

    employee.h
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    typedef struct     {
    			int ID;
    			string firstname;
    			string lastname;
    			string address;
    			string city;
    			string state;
    			string zipcode;
    			string phonenumber;
    			double salary;
    		   }			Employee;
    
    // Where Function Prototypes For Cpp Files Go.
    
    Employee CollectData(); // Returns Employee
    
    void DisplayEmployee( Employee e ); // Give Employee a variable name. Returns Nothing/Void
    
    double FindHighestSalary( Employee * a, int N ); // To find a higest salary we must use an Array. (So We Can Loop Through It)
    
    void ReadEmployees( Employee * a, int N ); // Reads an Array of Employees Returns Nothing.
    
    void DisplayArrayOfEmployees(Employee(Employee * a, int N)); // displays the array of employees by calling DisplayEmployee function
    
    Employee RaiseSalary(Employee * a, int N); // 6&#37; raise returns Employee
    
    double FindLowestSalary( Employee * a, int N); // lowest number in arrray reutnrs lowest
    main.cpp
    Code:
    #include <iostream>
    #include <string>
    
    #include "employee.h"
    
    using namespace std;
    
    int main()
    {
    	int N;
    	Employee * a ;
    	int again;
    
    	for( ; ; )
    {
    
    	cout << "How many this time: ";
    	cin >> N;
    	a = new Employee[N];
    
    	ReadEmployees(a, N);
    	DisplayArrayOfEmployees(a ,N);
    	FindLowestSalary(a, N);
    	RaiseSalary(a, N);
    	DisplayArrayOfEmployees(a, N);
    
    	cout << "Enter 0 to end, non-zero to repeat; ";
    	cin >> again;
    	if( !again ) break;
    
    }
    	free(Employee);	
    	
    	return 0;
    }
    Last edited by greatonesv; 11-19-2008 at 03:09 PM. Reason: finished code

  2. #2
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I dunno if it makes any difference, but I've always been told that using capitals in programming is bad practise :P
    And what kind of struct is that o.O? I'm no pro, just want to learn
    Currently research OpenGL

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1.
    Code:
    Employee RasieSalary(Employee * a, int N)
    {
    	int i;
    	for(i = 0; i < N ; i++)
    	{
    		a[i] = (.06 * a[i]) + a[i];
    	}
    	return Employee;
    }
    Typo in name of function and returning a type instead of an instance of said type. The calculation itself is all kinds of wrong. Is there a reason to actually return anything here?


    #2
    Code:
    void ReadEmployees( Employee * a, int N)
    {
    
    	int i;
    
    	for(i = 0 ; i < N ; i++)
    	{
    		a[i] = CollectData();
    	}
    
    }
    
    Employee CollectData()
    {
    	Employee * e;
            ...
    	return e;
    }
    e is an Employee* but function return type indicates your return an Employee (in which case, the call and assignment in the ReadEmployees function may or may not be incorrect since a[i] is of type Employee but you are attempting to return a pointer in the CollectData function).


    #3.
    Code:
    void DisplayArrayOfEmployees(Employee * a, int N)
    {
    	int i;
    	
    	for ( i = 0; i < N; i++)
    	{
    		a[i] = DisplayEmployee();
            // Missing brace '}' here?
    }
    
    void DisplayEmployee(Employee e)
    {
    
    	cout << e.ID << "\t" << e.lastname << "\t" << e.salary << endl;
    
    }
    Missing brace in DisplayArrayOfEmployees function and function DisplayEmployee expects an argument of type Employee but none is provided during function call within DisplayArrayOfEmployees. The assignment itself in the DisplayArrayOfEmployees function is wrong, you probably wanted to pass a[i] to the DisplayEmployee function as an argument.

    #4.
    Code:
    void DisplayArrayOfEmployees(Employee(Employee * a, int N));
    ???

    That's it for a quick look.
    Last edited by hk_mp5kpdw; 11-19-2008 at 03:17 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Err, you should really start using classes. Why would use C++ if you don't?
    This is what it should be:
    Code:
    class Employee
    {
        Your struct members
        Your functions that include Employee
    }
    It is not a complicated example to demonstrate the usefullness but it will teach you the basics.

    Another thing is that you pass N as a parameter in a lot of functions. You wouldn't do that normally. Why? Because you would use a std::vector<Employee> instead of an array of Emploees. There are a lot of reasons to do so. One is that a a vectors has its length stored inside the class. Meaning if you have a vector a you can do a.size and find the size and not having a separate variable N. Among other things...

    So I would recommend for starters using a class instead of a struct. A few things will change, but it will get you started to Object Orienting Programming

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    we have to use structs for this assignment, but i am lost as to what i am doing lol.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    thanks for the help guys, im just gunna give up on this one. i dont know what im doing so its no use. plus everytime i try to compile for some reason my .cpp files become unreadable and i have to keep starting over. thanks anyways..

  7. #7
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Damn, what cruel compiler would do that? :O
    Anyways, as to classes and struct's, I'm reading a book from 2000, it tells me to use struct's, is it because it's outdated?
    Currently research OpenGL

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by greatonesv View Post
    thanks for the help guys, im just gunna give up on this one. i dont know what im doing so its no use. plus everytime i try to compile for some reason my .cpp files become unreadable and i have to keep starting over. thanks anyways..
    Are you possibly doing something like "g++ -o x.cpp x.cpp" - yes g++ will then overwrite the input file with the object file - which is not very pleasant on the eyes for a human.

    Don't use -o with the same name as the source file!

    If you can't stop the compiler from putting rubbish in your source file, try this trick:
    before you compile, copy the file from x.cpp to y.cpp, and compile y.cpp - that way, you can compile y.cpp and thrash it, and still have x.cpp intact.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed