Thread: structure giving weird output

  1. #1
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    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
    Last edited by bluetxxth; 02-14-2010 at 01:52 PM. Reason: missing words

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    See the FAQ under "scanf()". fgets() ( instead of scanf() ) is easier to use.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    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....

  5. #5
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    structure giving weird output

    Quote Originally Posted by Salem View Post
    > 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?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well nothing seems obviously wrong with the struct.

    How are you running the program - from within the IDE?

    What about if you create a separate cmd console, then run the executable manually?

    This initialises the struct to see if it changes
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    
      struct employee {
        char name[30];
        char address[100];
        int age;
        float salary;
      } emp1 = {
        "fred",
        "flintstone",
        42,
        12345.6
      };
    
      if ( feof(stdin) ) {
        printf("Not good - stdin at EOF already\n");
      }
    
      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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User bluetxxth's Avatar
    Join Date
    Feb 2010
    Location
    sweden
    Posts
    43

    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?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes,
    Start menu -> run...
    then enter cmd.exe

    Then cd to your project directory and run the executable manually.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 05:17 PM
  2. Weird Output when I compile?
    By unejam2005 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2005, 01:46 AM
  3. Weird special character text after output
    By Dan17 in forum C++ Programming
    Replies: 13
    Last Post: 12-07-2005, 03:41 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM