Thread: plz hlp me. program not running properly

  1. #1
    jfl
    Guest

    Unhappy plz hlp me. program not running properly

    this program is supposed to calculate the salary of four types of employees. what is wrong? i have to use the switch structure and it doesnt seems to work. please keep in mind that im only allowed to use the variables and structures used here...its a newbie assignment. thanks in advance.








    #include <stdio.h>

    int main()
    {

    float weeklysale, salary1, salary2, salary3, salary4, rate;
    int id, paycode, hours, itemsproduced;

    printf( "\nEnter employee's ID number (-1 to end): ");
    scanf( "%d", &id );

    if ( id > 99 ) {
    printf ("Wrong ID Number. Please enter a new one (2 digits max.): ");
    scanf( "%d", &id );
    }

    while ( id != -1 ) {
    printf( "\nSelect a Paycode:\n1 is for managers\n2 is for commission workers\n" );
    printf( "3 is for hourly workers\n4 is for pieceworkers\n\n" );
    printf( "Enter chosen Paycode: ");
    scanf( "%d", &paycode );


    while ( paycode > 0 && paycode < 5 ) {

    switch ( paycode ) {

    case '1':
    paycode = 1;
    break;

    case '2':
    paycode = 2;
    break;

    case '3':
    paycode = 3;
    break;

    case '4':
    paycode = 4;
    break;

    case '\n': case' ':
    break;

    default:
    printf( "Incorrect Paycode number entered. " );
    printf( "Enter new Paycode number:\n" );
    break;

    }
    }


    if ( paycode == 1 )
    salary1 = 2500.00;
    printf( "Salary is $%.2f\n\n\n", salary1 );

    if ( paycode == 2 ){
    printf( "Enter # of hours worked: " );
    scanf( "%d", &hours );

    printf( "Enter hourly rate of the worker ($00.00): " );
    scanf( "%f", &rate );

    if ( hours <= 40 )
    salary2 = hours * rate;

    if ( hours > 40 )
    salary2 = ( float ) (hours - 40) * (1.50 * rate) + (40 * rate);

    printf( "Salary is $%.2f\n\n\n", salary2 );
    }
    if ( paycode == 3 ) {
    printf( "Enter gross weekly sales of the worker ($00.O0): ");
    scanf( "%f", &weeklysale );

    salary3 = 250.00 + ( 6.5 * weeklysale ) / 100;
    printf( "Salary is $%.2f\n\n\n", salary3 );
    }
    if ( paycode == 4 ) {
    printf( "Enter # of items produced by the worker: ");
    scanf( "%d", &itemsproduced );

    printf( "Enter hourly rate of the worker ($00.00): " );
    scanf( "%f", &rate );

    salary4 = itemsproduced * rate;
    printf( "Salary is $%.2f\n\n\n", salary4 );
    }

    }
    return 0;
    }

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Well, your switch is syntaxically correct, but you are effectively saying: if paycode = 1 then paycode = 1. What's the point?
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  3. #3
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Oh, actually it's not. putting quotes around an expression makes it a char (ie an ASCII representation of the number). Is this what you want?
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    while ( paycode > 0 && paycode < 5 ) { 
    
    switch ( paycode ) {
    That while loop effectively kills your program.

    I suggest taking out all the code that handles bad user input. Just assume that the user knows to enter 1-5, etc. You can add the error handling later.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    8
    alright, here is the code that i used for that same assignment, it looks like you have variations, so you can just go in and change what is needed, are you using the "how to program C" text book??

    #include <stdio.h>

    int main (void)

    {
    int paycode=0, hrsworked=0; //declarations
    float manager=0, wage=0, sales=0, items=0;
    float money=0, HW=0, CW=0, PW=0;

    printf("Please enter the employees paycode ->
    1=Manager\n\t\t\t\t\t
    2=Hourly Worker\n\t\t\t\t\t
    3=Commission Worker\n\t\t\t\t\t
    4=Pieceworker\n\n");

    //prompt
    do {
    switch (paycode) { //begining of case

    case '1':
    printf("Please enter weekly salary: ");
    scanf("%f", &manager); //reads float number
    printf("This employees weekly pay is $
    %.2f\n", manager);
    break;

    case '2':
    printf("Please enter amount of hours
    worked: ");
    scanf("%d", &hrsworked);
    printf("Please enter hourly wage: ");
    scanf("%f", &wage);

    if (hrsworked > 40) HW=((40 * wage) + ((hrsworked-40) *
    ( wage * (float)1.5)));
    else HW=(hrsworked*wage);

    printf("This employees weekly pay is $
    %.2f\n", HW);
    break;

    case '3':
    printf("Plese enter employees sales for the week: "); scanf("%f", &sales); //reads float number
    CW = (250 + (sales * float(.057))); //
    printf("This employees weekly pay is $%.2f\n", CW); //prints pay
    break;

    case '4':
    printf("Please enter amount of items sold: "); //prompt
    scanf("%f", &items); //reads float number
    printf("Please enter amount of money employees receives for each item sold: ");
    //prompt
    scanf("%f", &money); //reads float number
    PW = items * money;
    printf("This employees weekly pay is $%.2f\n", PW); //prints pay
    break;

    }
    }while ( ( paycode = getchar() ) !='~'); //while condition


    return 0; //indicates program ran successfully
    }

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    didn't this appear in a post before, the exact same problem?
    as i said before, you have an endless loop because id will never equal -1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. Replies: 0
    Last Post: 04-27-2003, 02:04 AM