Exercise - calculate
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Exercise - calculate

  1. #1
    Join Date
    Oct 2013
    Posts
    4

    Exercise - calculate

    This is just an exercise.
    And i need to complete this.

    This is the question :

    Write a program in C++ that calculates the amount to be paid to an employee based on the hours worked and rate per hour. A user will enter hours worked and rate per hour for an employee. Your program should have a function payCheck that calculates and returns the amount to be paid. The formula for calculating the salary amount is as follows: for the first 40 hours, the rate is the given rate (the rate that a user has entered); for hours over 40, the rate is 1.5 times the given rate.


    i had write my own coding, but it keeps error. it is okay. But i still don't think i could share here?
    Still running, but anyone can help me?
    i will update my coding for you alls to check for any syntax or logic error.

    UPDATE : this is my coding.
    The problem is i need to calculate if the employee work for more than 40 hours which 1.5 times.

    Code:
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    #include <stdio.h>
    #include <iostream.h>
    
    using namespace std;
    
    
    int main(void)
    {
    	char *empname, *test, *final_output;
    	float hours=0, pay_rate=0, gross=0, net=0;
    
    //loop
    	do {
    		/*-- get first & last name --*/
    		cout<<"Please Enter Your Name ";
    		cin>>empname;
    
    		/*-- 
    			get pay rate with error checking on
    			null and 0 values
    		--*/
    		do {
    			cout << "Pay Rate: ";
    			cin >> test;
    			if (test == NULL) 
    				cout << "Invalid pay rate.";
    			else if(atof(test) != 0) 
    				break;
    			else
    				cout << "Invalid pay rate.";			
    		} while (1);
    		pay_rate=atof(test);
    			
    		/*--
    			get hours worked with error checking on
    			null and 0 values
    		--*/
    		do {
    			cout << "Hours Worked: ";
    			cin >> test;
    			if (test == NULL)
    				cout << "Invalid amount of hours.";
    			else if(atof(test) !=  0)
    				break;
    			else
    				cout << "Invalid amount of hours.";
    		} while (1);
    		hours=atof(test);
    
    		/*-- calculate values --*/		
    		gross=hours*pay_rate;
    
    		/*-- set out precision --*/
    		cout.precision(2);
    		cout.setf(ios::fixed | ios::showpoint);
    		
    		/*-- create output --*/
    		sprintf(final_output,"\n\n%s %s ssn(%s)\n------------------------------\n",
    				empname);
    		cout << final_output;
    		cout << "Gross: " << gross << endl;
    
    		/*-- do another? --*/
    		cout << "Calculate Another? (y/n)?";
    		cin >> test;
    
    		/*-- check first value of string for y/Y --*/
    	} while (!strncasecmp(test,"y",1));
    
    	return 0;
    }
    Last edited by khelkely; October 8th, 2013 at 01:33 PM.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,419

    Re: Exercise - calculate

    What kind of errors do you get? Compiler errors, linker, or run/time errors?
    If you do need any help you will have to show your code. Please, use Code tags while posting code snippets. See Announcement: Before you post....
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2013
    Posts
    4

    Re: Exercise - calculate

    Of course i will. Soon oaky. I just post it at first.

    Thank you for the reply.

  4. #4
    Join Date
    Oct 2013
    Posts
    4

    Re: Exercise - calculate

    This is my latest code
    It does not have an error, but i cannot calculate the rate of overtime 4O hours.
    i am still running my exercise and need help.

    Code:
    //Write a program in C++ that calculates the amount to be paid
    //to an employee based on the hours worked and rate per hour.
    //A user will enter hours worked and rate per hour for an employee.
    //Your program should have a function payCheck that calculates and
    //returns the amount to be paid. The formula for calculating
    //the salary amount is as follows: for the first 40 hours,
    //the rate is the given rate (the rate that a user has entered);
    //for hours over 40, the rate is 1.5 times the given rate.
    
    #include <stdlib.h>
    #include <stddef.h>
    #include <string.h>
    #include <stdio.h>
    #include <iostream.h>
    
    using namespace std;
    
    int main(void)
        {
        char *empname, *test, *final_output;
        float hours=0, pay_rate=0, emprate=0;
        float pay_rate_overtime=1.5;
    
    //variable
        final_output = new char[1024];
        empname = new char[50];
        test = new char[10];
    
    //loop
        do {
            cout << "\nPlease Enter Your Name : ";
            cin >> empname;
            
        do {
            cout << "\nEnter Your Pay Rate : ";
            cin >> test;
            if (test == NULL)
            cout << "Invalid pay rate.";
            else if(atof(test) != 0)
            break;
            else
            cout << "please enter digit only";            
            }
        while (1);
            pay_rate=atof(test);
        
        do {
            cout << "\nEnter Your Worked Hours: ";
            cin >> test;
            if (test == NULL)
            cout << "please enter digit only";
            else if(atof(test) !=  0)
            break;
            else
            cout << "Invalid amount of hours.";
            }
        while (1);
            hours=atof(test);
    
            /*-- calculate values --*/        
            emprate=hours*pay_rate;
    
            /*-- set out precision --*/
            cout.precision(2);
            cout.setf(ios::fixed | ios::showpoint);
            
            /*-- create output --*/
            sprintf(final_output,"\n\n Your Pay Rate is : \n------------------------------\n", empname);
            cout << final_output;
            cout << "Your Salary Is : " << emprate <<endl;
    
            /*-- do another? --*/
            cout << "\n\n Calculate Another? (y/n)?";
            cin >> test;
    
            /*-- check first value of string for y/Y --*/
        } while (!strncasecmp(test,"y",1));
    
        /*-- return memory to the heap --*/
        delete [] empname;
        delete [] test;
        return 0;
    }

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: Exercise - calculate

    There's no code in there to do anything with the overtime pay.

    Forget code for a minute and outline the steps you'd need to take if you were using a pencil and paper.

    FWIW, test can never be NULL the way your code is written.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Exercise - calculate

    Quote Originally Posted by khelkely View Post
    This is my latest code
    It does not have an error, but i cannot calculate the rate of overtime 4O hours.
    What compiler are you using?
    Code:
    #include <iostream.h>
    This header doesn't exist in any version of Visual C++ that is more than 10 years old. It is non-standard, and no (modern) book, teacher or tutorial should be using it. The proper header is:
    Code:
    #include <iostream>
    Then all stream functions are in the namespace std. This is a simple Hello World program in C++:
    Code:
    #include <iostream>
    int main()
    {
       std::cout << "Hello World" << endl;
    }
    or
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       cout << "Hello World" << endl;
    }
    Second, you have a memory leak, since you didn't delete "final_output". But why are you using new[] in this case? Why not just do this:
    Code:
    char final_output[1024];
    Third, as GCDEF pointed out, test can never be NULL. You allocated memory, the pointer points somewhere, and that somewhere can never be NULL, as new[] as you're using it never returns NULL -- it must return a valid pointer or throw an exception.

    Regards,

    Paul McKenzie

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,107

    Re: Exercise - calculate

    I'll add I'm not even sure why you're using new at all. Instead of

    final_output = new char[1024];

    char final_output[1024];

    would be better if you really need to use char arrays.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,455

    Re: Exercise - calculate

    Instead of using 'c' style char arrays, why not use c++ strings? Why use sprintf instead of just using cout?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center