NO

Author Topic: salary prints strange number - why  (Read 3974 times)

jwzumwalt

  • Guest
salary prints strange number - why
« on: April 29, 2009, 06:59:33 AM »
If some kind c guru could tell me why the salary prints out strange numbers I would appreciate it. Atempts to use %f don't seem to work either :(
--------

Code: [Select]
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define HIT_ANY_KEY printf("\n\t Hit any key to continue..."); while(1){if(_kbhit()){break;}}
#define NEWLINE printf("\n");

void main()
{
   /* Structure declaration */
   struct employee
   {
     float salary[1];                    // social security number
     char doh[20];               // date of hire
     char name[20];
     char dept[20];
     char job[20];
   };

   char temp[20] = {0};
   struct employee our_employee[50];  // Structure variable declaration
 
   printf("\n\t Enter the employee's first name: " );
   scanf("%s", our_employee[1].name);     // Read the horse's name 

   printf("\n\t What is the hourly salary? ");
   scanf("%f", &our_employee[1].salary);

   printf("\n\t What is the hire date? ");
   scanf("%s", our_employee[1].doh);     // Read the horse's age   

   printf("\n\t What dept are they asigned to? ");
   scanf("%s", our_employee[1].dept);   // Get the mother's name 

   printf("\n\t What is their job name? ");
   scanf("%s", our_employee[1].job);   // Get the father's name   

    NEWLINE; 
   /* Now tell them what we know */
   printf("\n\t %s was hired on %s and is being paid %d hr.\n \
    They are employed as a %s in the %s dept.",
          our_employee[1].name, our_employee[1].doh, our_employee[1].salary, our_employee[1].job, 
    our_employee[1].dept );

   NEWLINE;
   HIT_ANY_KEY;
 }

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: salary prints strange number - why
« Reply #1 on: April 29, 2009, 08:19:06 AM »
   /* Structure declaration */
   struct employee
   {
     float salary;                  // here you do not need an array
     char doh[20];               // date of hire
     char name[20];
     char dept[20];
     char job[20];
   };


printf("\n\t %s was hired on %s and is being paid %d hr.\n \  // <= here you use %d for a float
          They are employed as a %s in the %s dept.",
          our_employee[1].name, our_employee[1].doh, our_employee[1].salary, our_employee[1].job, 
     our_employee[1].dept );

This is my printf-line:
   printf("\n\t %s was hired on %s and is being paid %5.2f hr.\n \
      They are employed as a %s in the %s dept.",
      our_employee[1].name, our_employee[1].doh, our_employee[1].salary, our_employee[1].job, 
      our_employee[1].dept );

If you have an array for salary and use %d you print out the address of the array.
best regards
 Alex ;)

jwzumwalt

  • Guest
Re: salary prints strange number - why
« Reply #2 on: April 29, 2009, 11:53:55 PM »
Thank you for the assistance. However, when I used your suggestion, the printout returns a zero amount for the salary. Here is the current code as you suggested....

Code: [Select]
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define HIT_ANY_KEY printf("\n\t Hit any key to continue..."); while(1){if(_kbhit()){break;}}
#define NEWLINE printf("\n");

void main()
{
   /* Structure declaration */
   struct employee
   {
     float salary[1];                  // social security number
     char doh[20];                     // date of hire
     char name[20];
     char dept[20];
     char job[20];
   };

   char temp[20] = {0};
   struct employee our_employee[50];  // Structure variable declaration
 
   printf("\n\t Enter the employee's first name: " );
   scanf("%s", our_employee[1].name);     // Read the horse's name 

   printf("\n\t What is the hourly salary? ");
   scanf("%f", our_employee[1].salary);

   printf("\n\t What is the hire date? ");
   scanf("%s", our_employee[1].doh);     // Read the horse's age   

   printf("\n\t What dept are they assigned to? ");
   scanf("%s", our_employee[1].dept);   // Get the mother's name 

   printf("\n\t What is their job name? ");
   scanf("%s", our_employee[1].job);   // Get the father's name   

    NEWLINE; 
   /* Now tell them what we know */
     printf("\n\t %s was hired on %s and is being paid %5.2f hr. \
      \n\t They are employed as a %s in the %s dept.",
      our_employee[1].name, our_employee[1].doh, our_employee[1].salary, our_employee[1].job, 
      our_employee[1].dept );


   NEWLINE;
   HIT_ANY_KEY;
 }

output:
« Last Edit: April 30, 2009, 12:01:44 AM by jwzumwalt »

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: salary prints strange number - why
« Reply #3 on: April 30, 2009, 08:07:14 AM »
Thank you for the assistance. However, when I used your suggestion, the printout returns a zero amount for the salary. Here is the current code as you suggested....

Code: [Select]
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define HIT_ANY_KEY printf("\n\t Hit any key to continue..."); while(1){if(_kbhit()){break;}} // <<== I don't like macros like this. It breaks the C-syntax. Use inline functions. ** aN
#define NEWLINE printf("\n");

void main()
{
   /* Structure declaration */
   struct employee
   {
     float salary;                  // <<== i removed here the "[1]", because you need not an array ** aN
     char doh[20];                     // date of hire
     char name[20];
     char dept[20];
     char job[20];
   };

   char temp[20] = {0};
   struct employee our_employee[50];  // Structure variable declaration
 
   printf("\n\t Enter the employee's first name: " );
   scanf("%s", our_employee[1].name);           // <<== If you start with 1 you let the our_employee[0] empty **aN

   printf("\n\t What is the hourly salary? ");
   scanf("%f", &our_employee[1].salary);         //  <<==  If you use no array, you need here again the &  ** aN

   printf("\n\t What is the hire date? ");
   scanf("%s", our_employee[1].doh);     // Read the horse's age   

   printf("\n\t What dept are they assigned to? ");
   scanf("%s", our_employee[1].dept);   // Get the mother's name 

   printf("\n\t What is their job name? ");
   scanf("%s", our_employee[1].job);   // Get the father's name   

    NEWLINE; 
   /* Now tell them what we know */
     printf("\n\t %s was hired on %s and is being paid %5.2f hr. \
      \n\t They are employed as a %s in the %s dept.",
      our_employee[1].name, our_employee[1].doh, our_employee[1].salary, our_employee[1].job, 
      our_employee[1].dept );


   NEWLINE;                    // this looks like a variable, but it is a function call. I don't like. ** aN
   HIT_ANY_KEY;
 }

You forgot to remove the array from salary. If you want to have an an array for salary you must also use it as array in the rest of you code.  ;)
« Last Edit: April 30, 2009, 08:32:32 AM by AlexN »
best regards
 Alex ;)

jwzumwalt

  • Guest
Re: salary prints strange number - why
« Reply #4 on: May 02, 2009, 03:51:59 AM »
I assume you mean something like this...

     float salary;                     // salary
     char doh[20];                         // date of hire
     char name[20];
     char dept[20];
     char job[20];
...


When I remove the salary array it errors immediately after a user input; during execution :-(

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: salary prints strange number - why
« Reply #5 on: May 02, 2009, 04:09:52 PM »
Thank you for the assistance. However, when I used your suggestion, the printout returns a zero amount for the salary. Here is the current code as you suggested....


I mean something like this:
Code: [Select]
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void inline HIT_ANY_KEY(void)
{
  printf("\n\t Hit any key to continue...");
  while(1)
  {
    if(_kbhit())
    {
       break;
    }
  }
}
void inline NEWLINE(void)
{
  printf("\n");
}

void main()
{
/* Structure declaration */
struct employee
{
float salary;                  // social security number
char doh[20];                     // date of hire
char name[20];
char dept[20];
char job[20];
};

char temp[20] = {0};
struct employee our_employee[50];  // Structure variable declaration

printf("\n\t Enter the employee's first name: " );
scanf("%s", our_employee[1].name);     // Read the horse's name 

printf("\n\t What is the hourly salary? ");
scanf("%f", &our_employee[1].salary);

printf("\n\t What is the hire date? ");
scanf("%s", our_employee[1].doh);     // Read the horse's age   

printf("\n\t What dept are they assigned to? ");
scanf("%s", our_employee[1].dept);   // Get the mother's name 

printf("\n\t What is their job name? ");
scanf("%s", our_employee[1].job);   // Get the father's name   

NEWLINE(); 
/* Now tell them what we know */
printf("\n\t %s was hired on %s and is being paid %5.2f hr. \
\n\t They are employed as a %s in the %s dept.",
our_employee[1].name, our_employee[1].doh, our_employee[1].salary, our_employee[1].job, 
our_employee[1].dept );


NEWLINE();
HIT_ANY_KEY();
}



look for the difference! ;)
best regards
 Alex ;)

jwzumwalt

  • Guest
Re: salary prints strange number - why
« Reply #6 on: May 02, 2009, 06:56:44 PM »
I got it!  Thanks for all the help...