3 days coding this without luck. help.s.o.s.

This is a discussion on 3 days coding this without luck. help.s.o.s. within the C Programming forums, part of the General Programming Boards category; this is SUPPOSED to compute 4 types of salaries but it symply doesn't work....can u help me out?? please note ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Unhappy 3 days coding this without luck. help.s.o.s.

    this is SUPPOSED to compute 4 types of salaries but it symply doesn't work....can u help me out?? please note that im not allowed to use any other structures or functions because it is an assigment...thanks

    #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 );
    }

    switch ( paycode = getchar() ) {

    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( "\nEnter new Paycode number: " );
    break;
    }

    if ( paycode == 1 ){
    salary1 = 2500.00;
    printf( "Salary is $%.2f\n\n\n", salary1 );
    }
    if ( paycode == 3 ){
    printf( "Enter # of hours worked: " );
    scanf( "%d", &hours );

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

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

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

    printf( "Salary is $%.2f\n\n\n", salary3 );
    }

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

    salary2 = 250.00 + ( 6.5 * weeklysale ) / 100;
    printf( "Salary is $%.2f\n\n\n", salary2 );
    }

    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just by glancing at your code the problem is probably here. When you enter the paycode does your default case print the error?
    Code:
    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 ); 
    } 
    
    switch ( paycode = getchar() ) {  /* Error here */
    Here's what I think is happening, but it's late and I may just be dillusional. scanf reads your paycode, but it also reads a newline with that. When getchar puts a value into paycode it will read the newline and your default case will be true. A fix would be to eat the newline after scanf or remove getchar() completely since it's redundant in this case.
    Code:
    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( getchar() != '\n' ); /* Eat the newline */ 
    } 
    
    switch ( paycode ) { 
    /*  Paycode already has a value, no need putting it 
    ** in again.
    */
    -Prelude
    My best code is written with the delete key.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Code:
    while ( id != -1 ) { ...... }
    this piece of code will loop forever because inside the brackets id doesn't change, only paycode does. try changing id to paycode(i see more "inconviences" in your code, but i'll let you sort em out.)
    for a truly messy program(which will fix this error, but create many others) try:
    Code:
    union {
    int paycode;
    int id;
    }
    Last edited by ygfperson; 02-10-2002 at 09:41 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >which will fix this error, but create many others
    That would normally be considered a bad thing. I would avoid a union in this program, dealing with the oddities of a union would be a waste with such a small program.

    >this piece of code will loop forever
    Thank you, I missed that little error. I'll get some sleep eventually, probably when my internet connection dies

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    still malfunctioning!!! s.o.s.... heeelp!

    i corrected the getchar() thing. is functioning better but still printing things that should not appear...also i want after it calculates a salary to print "enter employee (-1 to end)" again--how do i fix these problems---here is the code..compile and see what im saying...thanks



    #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 );

    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( "\nEnter new Paycode number: " );
    break;
    }

    if ( paycode == 1 ){
    salary1 = 2500.00;
    printf( "Salary is $%.2f\n\n\n", salary1 );
    }
    if ( paycode == 3 ){
    printf( "Enter # of hours worked: " );
    scanf( "%d", &hours );

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

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

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

    printf( "Salary is $%.2f\n\n\n", salary3 );
    }

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

    salary2 = 250.00 + ( 6.5 * weeklysale ) / 100;
    printf( "Salary is $%.2f\n\n\n", salary2 );
    }

    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;

    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It's hackish, but it works now. If I remember I'll come up with a more elegant solution tommorow, or you can.
    Code:
    #include <stdio.h> 
    
    int main() 
    { 
      double weeklysale, salary1, salary2, salary3 = 0, salary4, rate; 
      int id, paycode = 0, 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 ( paycode != -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 ); 
        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( "\nEnter new Paycode number: " ); 
            break; 
    	} 
        if ( paycode == 1 ){ 
          salary1 = 2500.00; 
          printf( "Salary is $%.2f\n\n\n", salary1 ); 
    	} 
        if ( paycode == 3 ){ 
          printf( "Enter # of hours worked: " ); 
          scanf( "%d", &hours ); 
    
          printf( "Enter hourly rate of the worker ($00.00): " ); 
          scanf( "%lf", &rate ); 
          if ( hours <= 40 ) 
            salary3 = hours * rate; 
          if ( hours > 40 ) 
            salary3 = (hours - 40) * (1.50 * rate) + (40 * rate); 
          printf( "Salary is $%.2f\n\n\n", salary3 ); 
    	} 
        if ( paycode == 2 ) { 
          printf( "Enter gross weekly sales of the worker ($00.O0): "); 
          scanf( "%lf", &weeklysale ); 
    
          salary2 = 250.00 + ( 6.5 * weeklysale ) / 100; 
          printf( "Salary is $%.2f\n\n\n", salary2 ); 
    	} 
        if ( paycode == 4 ){ 
          printf( "Enter # of items produced by the worker: "); 
          scanf( "%d", &itemsproduced ); 
    
          printf( "Enter hourly rate of the worker ($00.00): " ); 
          scanf( "%lf", &rate ); 
    
          salary4 = itemsproduced * rate; 
          printf( "Salary is $%.2f\n\n\n", salary4 ); 
    	} 
      } 
      return 0; 
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    thanks

    it is much better but still is not functioning 100 percent....thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advancing Days
    By nhubred in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2009, 06:22 PM
  2. Replies: 9
    Last Post: 03-20-2009, 05:22 PM
  3. some help on structures!!
    By dbz4life in forum C Programming
    Replies: 4
    Last Post: 11-26-2008, 09:38 AM
  4. while, sentinel, if, switch
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2001, 10:50 PM
  5. Coding Contest....
    By Koshare in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 10-14-2001, 04:32 PM

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