structure giving weird output
Hello there,
I have written a structure as an example and I am having trouble compiling it... I don't see what is wrong with it I think it has to do with some other thing but I am not sure...
The code is as follows:
Code:
/*
* test1.c
*
* Created on: Feb 9, 2010
* Author
*/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
struct employee {
char name[30];
char address[100];
int age;
float salary;
} emp1;
printf("Enter the name of the employee: ");
scanf("%s", emp1.name);
printf("Enter the address of the employee: ");
scanf("%s", emp1.address);
printf("Enter the age of the employee: ");
scanf("%d", &emp1.age);
printf("Enter the salary of the employee: ");
scanf("%f", &emp1.salary);
printf("the age of emp1 is: %s\n", emp1.name);
printf("the age of emp1 is: %s\n", emp1.address);
printf("the age of emp1 is: %d\n", emp1.age);
printf("the salary of emp1 is: %f\n", emp1.salary);
return 0;
}
and I get this as an output:
Enter the name of the employee: Enter the address of the employee: Enter the age of the employee: Enter the salary of the employee: the age of emp1 is: ץ0wM
the age of emp1 is: '
the age of emp1 is: 77
the salary of emp1 is: 0.000000
Please can anyone help me? is there something wrong with the code?
-------------------------------------------------
windows vista x64 ultimate
eclipse-SDK-3.5.1-win32
mingw-w32-bin_i686-mingw_20100123_sezero
MSYS-1.0.11
structure giving weird output
Thnx for the answer.... I will try that as well however I am still wandering if the structure is wrong...? it seems to me that there is nothing wrong with it, that it is the compiler that is giving me problems but I am not sure....
structure giving weird output
Quote:
Originally Posted by
Salem
> Enter the name of the employee: Enter the address of the employee: Enter the age of the employee: Enter the salary of the employee:
That all this appeared on one line seems to suggest there is more to your program than what you're showing here.
It looks like stdin has reached EOF, causing all scanf() calls to return immediately, and leaving all your emp data uninitialised.
The structure was supposed to be a test I made to understand pointers but I never moved on to the pointers because the structure is giving these problems so there is no more to the structure than what I wrote.
To make it simpler, if I merely use a printf in a hellow world program then the output is okay. However as soon as I add a scanf then it stops working properly:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char whatever[30];
printf("Enter whatever: ");
scanf("%s",whatever);
printf("what you wrote is: %s\n", whatever);
return 0;
}
I get the following upon building
**** Internal Builder is used for build ****
gcc -O0 -g3 -Wall -c -fmessage-length=0 -oprint.o ..\print.c
gcc -oprint.exe print.o
Build complete for project print
Time consumed: 305 ms.
But this output which is totally crazy:
Enter whatever: what you wrote is:
This is not what it is supposed to do. It is supposed to prompt for a name and then tell me the name... or is there something I am missing..? In addition to this it goes on an endless loop which is only broken when I click on terminate.
Any ideas?
structure giving weird output
When I paste and build what you added I get the following:
Enter the name of the employee: Enter the address of the employee: Enter the age of the employee: Enter the salary of the employee: the age of emp1 is: fred
the age of emp1 is: flintstone
the age of emp1 is: 42
the salary of emp1 is: 12345.599609
What I do is I that I make it in eclipse and then I save, build and execute..... but it always gives me this problem... what can it be?
How do create a separate cmd console? you mean to do it with cmd instead of using eclipse?