getchar() error

This is a discussion on getchar() error within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #define EOL '

Thread: getchar() error

' int main( int argc, const char * argv[]) { char sent[ 50 ]; int ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    getchar() error

    Code:
    
    
    #include <stdio.h>
    #define EOL '\0'
    
    
    int main(int argc, const char * argv[])
    {
     char sent[50];
     int i=0;
     int cnt=0;
     char charac;
    
    puts("Please enter a sentence");
     scanf(" %[^\n]",sent);
    
        puts("Please enter the character to be checked");
        charac=getchar();
    
     while (sent[i]!=EOL) {
    
    
     if(sent[i]==charac)
            {
                ++cnt;
                ++i;
            }
     else
                ++i;
    
    
        }
    
    printf("\nCHARACTER OCCURRED %d TIMES",cnt);
    
    
    return 0;
    }
    
    



    hey guys,
    I'm trying to write a program that accepts a character from the user to compute the number of times the character appears in an inputted sentence. However, the compiler completely ignores the
    getchar function. So i tried putting the getchar function above the scanf and this time it works!!
    Can anyone explain this???

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Q: what is the last character you typed for the sentence?
    A: an ENTER

    That ENTER is what getchar() gets.

    Suggestion: use fgets() instead of scanf() to read the sentence.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    2
    hmm i haven't started on files yet but thank you very much for replying

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by qny View Post
    Suggestion: use fgets() instead of scanf() to read the sentence.
    Quote Originally Posted by abeljohny View Post
    hmm i haven't started on files yet but thank you very much for replying
    We have a FAQ for that: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.

    Note that the file they pass to fgets is stdin, which is the standard input device (your keyboard). So that call to fgets reads from the keyboard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone try getchar() please?
    By znum in forum C Programming
    Replies: 3
    Last Post: 05-12-2008, 04:14 PM
  2. why do I need 2 getchar()'s?
    By parx86 in forum C Programming
    Replies: 8
    Last Post: 04-12-2008, 06:01 PM
  3. getchar
    By pktcperlc++java in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2005, 07:31 PM
  4. getchar() error?
    By caroundw5h in forum C Programming
    Replies: 7
    Last Post: 03-20-2004, 08:01 PM
  5. Key Value - getchar();
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 12-30-2001, 09:53 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21