Thread: Char Array Prob

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    53

    Char Array Prob

    I'm trying to store three characters in an array and then print all three what is wrong with this?

    here's the code:

    #include <stdio.h>


    char array[3];

    int main()
    {
    int x;
    printf("Please enter your first initial\n");
    scanf("%c",&array[0]);
    printf("Please enter your second initial\n");
    scanf("%c",&array[1]);
    printf("Please enter your third initial\n");
    scanf("%c",&array[2]);
    printf("Your initials are: ");
    for(x=0;x<3;x++)
    {
    printf("%c", array[x]);
    }

    return 0;
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    First, use code tags.

    Second, what happens when you run the program?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Try to be more descriptive of your problem next time. Anyway, maybe this can help:
    Code:
    #include <stdio.h>
    int main()
    {
      char array[3];
      int x;
    
      printf("Please enter your first initial: ");
      scanf(" %c",&array[0]);
      printf("Please enter your second initial: ");
      scanf(" %c",&array[1]);
      printf("Please enter your third initial: ");
      scanf(" %c",&array[2]);
      printf("Your initials are: ");
      for(x=0;x<3;x++)
        {
          printf("%c", array[x]);
        }
    
      printf("\n");
    
      return 0;
    }
    $ENV: FreeBSD, gcc, emacs

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Use fflush(stdin) after scanf statements. When you press enter actually 13 gets stored in a[1] and a[2].
    Saravanan.T.S.
    Beginner.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by saravanan_ts
    Use fflush(stdin) after scanf statements. When you press enter actually 13 gets stored in a[1] and a[2].
    Oh dear, oh dear.... where have you been?

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I don't know why but for some reason this works for me:
    Code:
    scanf();
    and it clears the buffer (or at least allows me to use scanf again)

    any ideas on why this works?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>any ideas on why this works?
    Ask your compiler
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    tell me about it.
    When I was taking the C++ class at my old high school I asked my teacher if I could use C (Because at the time, I knew C more than C++) and he said its ok. I had a problem when scanf wasn't flushing (LOL) and he said I needed to clear the buffer. So I'm like, ok... scanf() ? It worked on that compiler, some REALLY REALLY old MSVC++ Compiler. But it doesn't work now (just tried it).
    Hmm, it was really odd...
    maybe I did scanf("") instead...
    I think that's what I did actually...
    either way, it worked and it's weird.
    My Teacher was arguing with me that it wouldn't work but when it compiled... heheh ^_^

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM