Thread: Need help with Single Character

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Need help with Single Character

    Ok, so I am having trouble on trying to figure out how to populate my array with a single character. Everytime I run my program, it skips through the part where I ask the user to input the first character corresponding the person's home state (abbrev). Like for CA, enter C and store into speakerstate1[i][j].

    I think I'm suppose to use getchar() and putchar()

    Here's a piece of my code...

    Thank you!!

    Code:
      
    for( i = 0; i < 3; i++ )
    {
             speaker_info();
    
              printf( "\nBased on the information above. Please enter the first speaker/performer's series code:" );
    
                    for( j = 0; j < num; j++ )
                    {
                            scanf( "%c", &speakerchar[i][j] );
                            i++;  
    
                            printf( "\n\n\nPlease enter the first letter of the speakers home state:" );
                            scanf( "%c", &speakerchar[i][j] );
                            i++;
    
                            printf( "\nPlease enter the second letter of the speakers home state:\n\n" );
                            scanf( "%c", &speakerchar[i][j] );
                            i = 0;
                            speaker_info();
    
    
                            printf( "\nBased on the information above. Please enter the next speaker/performer's series code:" );
                    }
                    i = 3;
            }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    scanf() leaves behind the newline char: '\n', in the input stream. Your next scanf() (which is only looking for ONE char, see's that, says "thanks!", and the program doesn't wait for little old you.

    Remove each newline with:
    Code:
    getchar();
    After the scanf().

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Ahhh thank you so much. Makes sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Good single character option function
    By eponymous in forum C Programming
    Replies: 12
    Last Post: 03-15-2008, 06:17 PM
  3. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. How to strcopy a single character from a string
    By Stuu in forum C++ Programming
    Replies: 8
    Last Post: 04-18-2002, 01:45 AM