Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,374 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,167 people online right now. Registration is fast and FREE... Join Now!




Any suggestions

 
Reply to this topicStart new topic

Any suggestions, Although this program works I would like some suggestions as to how it

browngod2002
post 29 Sep, 2006 - 02:18 PM
Post #1


New D.I.C Head

Group Icon
Joined: 16 Sep, 2006
Posts: 27



Thanked 1 times

Dream Kudos: 275
My Contributions


This program works but I would like some suggestions as to how I can make it better. One is how to make it re-ask for the paycode after entering one in. Another is when the wrong paycode is entered how to make the program return to the begining and ask for the paycode again. Any suggestions will be greatly appreciated. biggrin.gif

CODE

#include "stdafx.h"
#include <stdio.h>
/* The define symbolic constants */
#define PERCENT .057
#define OVERTIME 1.5
/* function prototype */
float managerpay(float salary);
float hourlypay(int hours, float pay);
float commpay(float commission, int sales);
float pwpay(float wage, int pieces);


int _tmain(void)
{

    int paycode;
    float salary;
    float commission;
    float wage;
    int pieces;
    float pay;
    int sales;
    int hours;

    printf("Enter the employee's pay code to calculate salary: ");
    scanf_s("%d", &paycode);

    /* The switch used to get the inputs, call the functions to calculate the pay
    for each paycode and display the results formatted to 2 decimal places */
    switch (paycode)
        {
        case 1:
            printf("Enter the managers salary: ");
            scanf_s("%f", &salary);

            printf("The managers pay is: $%.2f\n", managerpay(salary));
            
            break;

        case 2:
            printf("Enter the hours worked: ");
            scanf_s("%d", &hours);

            printf("Enter the salary: ");
            scanf_s("%f", &pay);

            printf("The employee's pay is: $%.2f\n",float (hourlypay(hours,pay)));

            break;

        case 3:
            printf("Enter the sales: ");
            scanf_s("%d", &sales);

            printf("Enter the commission: ");
            scanf_s("%f", &commission);            

            printf("The employee's pay is: $%.2f\n",commpay(commission,sales));

            break;

        case 4:
            printf("Enter the number of pieces made: ");
            scanf_s("%d", &pieces);

            printf("Enter the wage: ");
            scanf_s("%f", &wage);
            
            printf("The employee's pay is: $%.2f\n",pwpay(wage,pieces));

            break;

        default:
            printf("You didn't enter the correct pay code.\n");

                        
        }      

        getchar();
        getchar();

        return 0;
}

/* managerpay() : Takes in the salary for a manager for a year and
                  returns the weekly salary.

      Input: salary as a float.
      Output: the salary for a week as a float */

float managerpay(float salary)
{    
    return salary / 52;
}

/* hourlypay () : takes in the hours and pay for an hourly employee
                  and returns the weekly salary. If overtime has been
                  worked for the week then it will calculate the overtime
                  and add it to the weekly pay. If the hours are less than
                  40 the weekly pay will still be calculated.

     Input: hours as an int and pay as a float
     Output: the weekly pay as a float*/

float hourlypay(int hours, float pay)
{
    int pay2;

    if (hours == 40)    
        pay2 = int(hours * pay);
    else if (hours > 40)    
        pay2 = int(( pay * OVERTIME) * (hours - 40) + (pay * 40));
    else
        pay2 = int(hours * pay); /* this was not part of the program but I thought it
                                    would make a nice addition */

    return (float) pay2;
}

/* commpay() : takes in the sales and the commission for the commission worker
               and retuns their pay for the week.

    Input: commission as a float and sales as an int
    Output: the weekly pay for the worker as a float */

float commpay(float commission, int sales)
{
    float pay;
    

    pay =float( commission + PERCENT * sales);

    return pay;
}

/*pwpay () : takes in the number of pieces made for the week
             and the pay that the piece worker gets for each
             piece made.

    Input: wage as a float and pieces as an int
    Output: the weekly pay as a float */

float pwpay(float wage, int pieces)
{
    float pay;

    pay = wage * pieces;

    return pay;
}


This post has been edited by browngod2002: 29 Sep, 2006 - 02:24 PM
User is offlineProfile CardPM

Go to the top of the page

Jayman
post 29 Sep, 2006 - 08:56 PM
Post #2


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 6,839



Thanked 38 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


QUOTE(browngod2002 @ 29 Sep, 2006 - 03:18 PM) *

This program works but I would like some suggestions as to how I can make it better. One is how to make it re-ask for the paycode after entering one in. Another is when the wrong paycode is entered how to make the program return to the begining and ask for the paycode again. Any suggestions will be greatly appreciated. biggrin.gif


You can actually kill two birds with one stone using a while loop and adding a case 5.

I didn't indent your code further, just added the loop. I couldn't compile it since I don't have the stdafx.h file.
CODE

#include "stdafx.h"
#include <stdio.h>
/* The define symbolic constants */
#define PERCENT .057
#define OVERTIME 1.5
/* function prototype */
float managerpay(float salary);
float hourlypay(int hours, float pay);
float commpay(float commission, int sales);
float pwpay(float wage, int pieces);


int _tmain(void)
{

    int paycode;
    float salary;
    float commission;
    float wage;
    int pieces;
    float pay;
    int sales;
    int hours;

    printf("Enter the employee's pay code to calculate salary: ");
    scanf_s("%d", &paycode);

    while (paycode != 5)// start of while loop
    {
    /* The switch used to get the inputs, call the functions to calculate the pay
    for each paycode and display the results formatted to 2 decimal places */
    switch (paycode)
        {
        case 1:
            printf("Enter the managers salary: ");
            scanf_s("%f", &salary);

            printf("The managers pay is: $%.2f\n", managerpay(salary));
            
            break;

        case 2:
            printf("Enter the hours worked: ");
            scanf_s("%d", &hours);

            printf("Enter the salary: ");
            scanf_s("%f", &pay);

            printf("The employee's pay is: $%.2f\n",float (hourlypay(hours,pay)));

            break;

        case 3:
            printf("Enter the sales: ");
            scanf_s("%d", &sales);

            printf("Enter the commission: ");
            scanf_s("%f", &commission);            

            printf("The employee's pay is: $%.2f\n",commpay(commission,sales));

            break;

        case 4:
            printf("Enter the number of pieces made: ");
            scanf_s("%d", &pieces);

            printf("Enter the wage: ");
            scanf_s("%f", &wage);
            
            printf("The employee's pay is: $%.2f\n",pwpay(wage,pieces));

            break;
        
        case 5:
            getchar();
            getchar();

            return 0;
            break;
            

        default:
            printf("You didn't enter the correct pay code.\n");

                        
        }
        
        printf("Enter the employee's pay code to calculate salary: ");
        scanf_s("%d", &paycode);
    
        }//end while loop      

        
}

/* managerpay() : Takes in the salary for a manager for a year and
                  returns the weekly salary.

      Input: salary as a float.
      Output: the salary for a week as a float */

float managerpay(float salary)
{    
    return salary / 52;
}

/* hourlypay () : takes in the hours and pay for an hourly employee
                  and returns the weekly salary. If overtime has been
                  worked for the week then it will calculate the overtime
                  and add it to the weekly pay. If the hours are less than
                  40 the weekly pay will still be calculated.

     Input: hours as an int and pay as a float
     Output: the weekly pay as a float*/

float hourlypay(int hours, float pay)
{
    int pay2;

    if (hours == 40)    
        pay2 = int(hours * pay);
    else if (hours > 40)    
        pay2 = int(( pay * OVERTIME) * (hours - 40) + (pay * 40));
    else
        pay2 = int(hours * pay); /* this was not part of the program but I thought it
                                    would make a nice addition */

    return (float) pay2;
}

/* commpay() : takes in the sales and the commission for the commission worker
               and retuns their pay for the week.

    Input: commission as a float and sales as an int
    Output: the weekly pay for the worker as a float */

float commpay(float commission, int sales)
{
    float pay;
    

    pay =float( commission + PERCENT * sales);

    return pay;
}

/*pwpay () : takes in the number of pieces made for the week
             and the pay that the piece worker gets for each
             piece made.

    Input: wage as a float and pieces as an int
    Output: the weekly pay as a float */

float pwpay(float wage, int pieces)
{
    float pay;

    pay = wage * pieces;

    return pay;
}

User is offlineProfile CardPM

Go to the top of the page

shainab88
post 30 Sep, 2006 - 06:04 AM
Post #3


New D.I.C Head

*
Joined: 27 Sep, 2006
Posts: 2


My Contributions


hi i know this isn't a suggestion, but i don't know how to begin my own post. so i need help writing this program for a class, and i already have difficulty writing programs since im a beginner, and i have no idea where to start from for this program. i anyone could help me i would great appreciate it, and please send me a personal message. here are the guidelines to writing the program:

QUOTE

1) Create the main program file. At the top of this file, the following comment block
is required to receive full credit.
/*
Your Name
Your Class
Today’s Date
Programming Assignment #1
*/
After which, create a main function, with a return type of void and a parameter
list of type void.
2) Create a function and name it OutputHeader, which has a void return type and a
parameter list of void. Create a prototype for this function and put it at the
beginning of your program.
Inside of OutputHeader, use a series of printf statements to produce the following
output
Your Name
Your Class
Today’s Date
Programming Assignment #1
Call this function from your main function
3) Create a function and name it InputData, which has an int return type and a
parameter list of an array of type double.
Using a for loop, input 10 doubles from the keyboard and store them in the array
from the parameter list. Remember, to read in a type double, a format string of
“%Lf” must be used in the scanf statement. The function should keep a count of
the number of entries, and this value should be returned by the function.
4) Create a function and name it ShowData, which has a void return type, and a
parameter list of an array of type double and an integer. The integer is the length
of the array. Use a for loop to show the contents of the array. Show each index
and the associated value.
5) Create a function and name it FindAverage, which has a double return type, and a
parameter list of an array of type double and an integer. The integer is the length
of the array. Use a for loop to find the average of the data in the array. The
average should be returned by the function
6) Create a function and name it FindMinium, which has a double return type, and a
parameter list of an array of type double and an integer. The integer is the length
of the array. Use a for loop to find the minimum value stored in the array. The
minimum should be returned by the function.
7) Create a function and name it FindMaxium, which has a double return type, and a
parameter list of an array of type double and an integer. The integer is the length
of the array. Use a for loop to find the maximum value stored in the array. The
maximum should be returned by the function.
8) Create a function and name it FindVariance, which has a double return type, and a
parameter list of an array of type double and an integer. The integer is the length
of the array. Use a for loop to find the maximum value stored in the array. The
maximum should be returned by the function.
Variance is a statistical function, which contains several steps. First, the
difference between each data value and the average of all the data values is taken.
Next, this difference is squared to make a positive number. Then, each of these
squared values is summated. Finally, after the sum is complete, the total is
divided by the number of data points. This final value is the variance.
2 ) x (x
n
1 variance ∑ − =
The FindAverage function may be used to solve this section.
9) Create a function and name it FindStdDev, which has a double return type, and a
parameter list of an array of type double and an integer. The integer is the length
of the array.
At the beginning of the program, just after the #include<stdio.h>, and the
statement #include<math.h>. The math.h is the header to the C math routines
library, and contains many useful tools. For standard deviation, the square root
function must be used.
The square root command is:
StdDev=sqrt(Variance);
Standard Deviation is the square root of the Variance. The FindVariance function
may be used to solve for the Standard Deviation.
10) Create a function called Menu, which has a void parameter list and a void return
type.
The menu should look something like this:
1-Input Data
2-Show Array
3-Find Average
4-Find Minimum
5-Find Maximum
6-Find Variance
7-Find Standard Deviation
q-quit
Create a while loop that runs the menu until the ‘q’ character is
pressed. In this menu, however, a string is to be used.
Create some character array, for example, InputString, and then
use a scanf to read the user input.
Once this is done, take only the first character of the string
and use it to check which option from the menu.
For example:
char InputString[80];
char key=’0’;
scanf(“%s”,InputString);
key=InputString[0];
Use the menu routine to call each of the previous functions that
have been created. An if-then-else tree or a switch statement may
be used to call the functions.
After the function is called, print the result of the function to
the screen along with the name of the Menu Item
Call this function from your main function
Example Output:
Eric Becker
CSE 1311
September 18, 2006
Program Assignment 2
Menu
1-Input Data
2-Show Array
3-Find Average
4-Find Minimum
5-Find Maximum
6-Find Variance
7-Find Standard Deviation
q-quit
1
Please enter 10 doubles
10.0
1.0
1.45
-3.33
1.223
3.33
22.2
3.33
-22.23
12.3
Menu
1-Input Data
2-Show Array
3-Find Average
4-Find Minimum
5-Find Maximum
6-Find Variance
7-Find Standard Deviation
q-quit
2
The Array is
Array[0]=10
Array[1]=1
Array[2]=1.45
Array[3]=-3.33
Array[4]=1.223
Array[5]=3.33
Array[6]=22.2
Array[7]=3.33
Array[8]=-22.23
Array[9]=12.3
Menu
1-Input Data
2-Show Array
3-Find Average
4-Find Minimum
5-Find Maximum
6-Find Variance
7-Find Standard Deviation
q-quit
3
Average is 2.9273
Menu
1-Input Data
2-Show Array
3-Find Average
4-Find Minimum
5-Find Maximum
6-Find Variance
7-Find Standard Deviation
q-quit
4
Minimum is -22.23
Menu
1-Input Data
2-Show Array
3-Find Average
4-Find Minimum
5-Find Maximum
6-Find Variance
7-Find Standard Deviation
q-quit
5
Maximum is 22.2
Menu
1-Input Data
2-Show Array
3-Find Average
4-Find Minimum
5-Find Maximum
6-Find Variance
7-Find Standard Deviation
q-quit
6
Variance is 119.048
Menu
1-Input Data
2-Show Array
3-Find Average
4-Find Minimum
5-Find Maximum
6-Find Variance
7-Find Standard Deviation
q-quit
7
StdDev is 10.9109
Menu
1-Input Data
2-Show Array
3-Find Average
4-Find Minimum
5-Find Maximum
6-Find Variance
7-Find Standard Deviation
q-quit


This post has been edited by Dark_Nexus: 1 Oct, 2006 - 02:23 PM
User is offlineProfile CardPM

Go to the top of the page

browngod2002
post 30 Sep, 2006 - 07:44 AM
Post #4


New D.I.C Head

Group Icon
Joined: 16 Sep, 2006
Posts: 27



Thanked 1 times

Dream Kudos: 275
My Contributions


Thank yu jayman9 I was trying to get the loop to work but I was doing it wrong. I was trying to loop through the default case. I knew it was a better way to do this to make it more user friendly. Thank you again.

This post has been edited by Dark_Nexus: 1 Oct, 2006 - 02:24 PM
User is offlineProfile CardPM

Go to the top of the page

browngod2002
post 30 Sep, 2006 - 08:29 AM
Post #5


New D.I.C Head

Group Icon
Joined: 16 Sep, 2006
Posts: 27



Thanked 1 times

Dream Kudos: 275
My Contributions


Although you are a beginer it is not hard to write this program.
You have to start something and then you will get more help.
First open up your compiler and create the header, actually the compiler will do that for you all you have to do is put in the information that you already have.
Do this and when you get stuck then ask for help.
We can't do it for you

This post has been edited by Dark_Nexus: 1 Oct, 2006 - 02:24 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 05:56AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month