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 :(
--------
#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;
}
Quote from: jwzumwalt on April 29, 2009, 06:59:33 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.
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....
#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:
(http://www.visitor.neatinfo.com/public/temp.png)
Quote from: jwzumwalt 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....
#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. ;)
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 :-(
Quote from: jwzumwalt 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....
I mean something like this:
#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! ;)
I got it! Thanks for all the help...