Thread: structure

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    227

    structure

    can anyone please tell me why this is not letting me input the char and only the integer.. ?

    Code:
    struct hm{ 
    
       int i;
       char ch;
       }s;
    
    int main()
    {
       printf("enter and integer :");
         scanf("%d",&s.i);
        printf("Enter a charater :\n");
         scanf("%c",&s.ch);
         printf("%d  %c\n",s.i ,s.ch);
         return 0;
       }
    thanx

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Do a board search for topics on flushing the input buffer. The problem is that there is junk being left behind in the input buffer and is being used as input for the char. Just one word of advice, don't use fflush(stdin). Do a search as to why you shouldn't.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    so then what should i use.....? sorry im still new at this.. started learning structure today..

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by xlordt
    so then what should i use.....? sorry im still new at this.. started learning structure today..
    You'd know what to use if you had done a board search like the poster suggested.

    while(fgetc(stdin)!='\n');

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use
    >while (getchar() != '\n');
    .. but you should really do a board search to find these types of thing out

    [edit]Dam man, beat by 0 minutes
    Last edited by Hammer; 09-16-2002 at 03:33 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM