#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Unexplained program crash


    Learning C and having an issue with a very simple program. I have changed the dimensions on numbers that should be float to int and it has note had an effect. It is probably a very stupid error or maybe a compiler issue. Thoughts?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
       
    
    int main()
    {
        
        char name[10];
           int wage=0;
           int hrs=0;
          const float tax_rate= 1.13;
          float pay=0;
          
          
        // Insert Header
      printf("**************************************************\n");
      printf("**************************************************\n");
      printf("**                                              **\n");
      printf("**                                              **\n");
      printf("**          Employee Pay Calculator             **\n");
      printf("**                                              **\n");
      printf("**                                              **\n");
      printf("**************************************************\n");
      printf("**************************************************\n\n\n\n");
      
      // Get employee name, hourly pay, hours worked
      // then compute the pay and return the value
      
    
    
      printf("Enter employee's name: \n");
      scanf("%s", name);
      
      printf("Enter hours %s worked this pay period: \n", name);
      scanf("%i", hrs);
      
      printf("Enter %s pay rate: \n", name);
      scanf("%i", wage);
      
      
    pay = wage * hrs;
    pay = pay / tax_rate;
      
      printf("\n%s, pay is: %f, this pay period. \n", name, pay);
      
      system("PAUSE");	
      return 0;
    }
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,993
    Rep Power
    9397
    Code:
    char name[10];
    1. Are you entering names that are no more than 9 characters long? That's all your array allows for (because that 10 has to include the \0).


    Code:
    scanf("%i", hrs);
    scanf("%i", wage);
    2. sprintf() needs the memory address of where to store the stuff it reads in; the variable itself is not enough. Use a & to get their addresses, like
    Code:
    scanf("%i", &hrs);
    scanf("%i", &wage);
    If you're wondering, since name is an array it's already an address. On the other hand name[0] is not an address so if you passed that you'd have to use a & (which would be weird because it's easier to just pass name by itself).

    Comments on this post

    • astonecipher agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0
    I did the name array to only allow for the first name, I changed it to [25] characters now. I also added the pointers (&) in the variables. However the math portion is still not giving what I am expecting. The response is either 0, a gig number, or a hex number without giving the value of pay=(wage * hrs) / tax_rate;
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    Originally Posted by astonecipher
    I did the name array to only allow for the first name, I changed it to [25] characters now. I also added the pointers (&) in the variables. However the math portion is still not giving what I am expecting. The response is either 0, a gig number, or a hex number without giving the value of pay=(wage * hrs) / tax_rate;
    Try casting one of these to float...Like below
    Code:
    pay = (float)wage * hrs;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by G4143
    Try casting one of these to float...Like below
    Code:
    pay = (float)wage * hrs;
    Not doing anything different. It is atleast no longer crashing.

    This is the current code I am testing with revision.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
         
    
    
    int main()
    {
        
        char name[25];
           float wage;
           float hrs;
          float tax_rate =1.13;
          float pay;
          
          
        // Insert Header
      printf("**************************************************\n");
      printf("**************************************************\n");
      printf("**                                              **\n");
      printf("**                                              **\n");
      printf("**          Employee Pay Calculator             **\n");
      printf("**                                              **\n");
      printf("**                                              **\n");
      printf("**************************************************\n");
      printf("**************************************************\n\n\n\n");
      
      /*Get employee name, hourly pay, hours worked
      then compute the net pay and return the value
      */
      printf("Enter employee's first name: \n");
      scanf("%s", name);
      
      printf("Enter hours %s worked this pay period: \n", name);
      scanf("%f", &hrs);
      
      printf("Enter %s pay rate: \n", name);
      scanf("%.2f", &wage);
    
     pay = (float)wage * hrs;
     // pay = pay / tax_rate; expelled for testing
      
      
      printf("\n%s's, pay is: $%2.f, this pay period. \n", name, pay);
      
      system("PAUSE");	
      return 0;
    }
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    10
    Rep Power
    0

    Working properly


    Okay, the casting did help. I initialized all of my variables to 0, then recompiled and it is now giving expected values. Thank you.

IMN logo majestic logo threadwatch logo seochat tools logo