character occurrence program not working

This is a discussion on character occurrence program not working within the C Programming forums, part of the General Programming Boards category; Hi, the following program prompts the user to enter a few lines of string and a character as a search ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    character occurrence program not working

    Hi, the following program prompts the user to enter a few lines of string and a character as a search key and outputs the total number of times that character appeared in those lines entered. However the bug is that the program prints "Enter string" twice and then i can't enter the first string. How come?

    thnx in advance

    Code:
    /* character occurence count */
    
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       int   occurrenceCount = 0;
       char   *searchPtr;
       char string[ 400 ];
       char tempStr[ 150 ];
       int numLines;
       int i;
       char key;
    
       printf( "Number of lines to be inputted: " );
       scanf( "%d", &numLines );
    
    
    
       for ( i = 0; i < numLines; i++ ) {
          printf( "Enter string: " );
          gets( tempStr );
          strcat( string, tempStr );
       }
    
       printf( "Enter search character: " );
       scanf( "%c", &key );
    
       searchPtr = strchr( string, key );
    
       while ( searchPtr != NULL ) {
          ++occurrenceCount;
          searchPtr = strchr( searchPtr + 1, key );
       }
    
       printf( "\n\nTotal of occurrence character \'%c\' appeared in line(s) entered: %d\n\n",
               key, occurrenceCount );
    
       system( "PAUSE" );
       return 0;
    }

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Hey Nutshell,

    The user is prompted to enter a number ("Number of lines to be inputted:") and scanf() puts the value into the variable numLines, but doesn't pull the carriage-return off the buffer (stdin). Then when you reach gets() ("Enter string:"), the carriage return causes gets() to grab an empty line.

    Consider using fgets() to get data from the user.
    Jason Deckard

  3. #3
    Unregistered
    Guest
    i haven't learnt fgets() yet. Anyway to fix the problem using my original code?

    thnx

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    the above reply was mine, forgot to log in...

  5. #5
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Yeah, a cheap trick would be to put in a gets() right after the scanf(). This would pull the carriage-return off stdin. If you are allowed to fflush(), that would be the proper way to clean up stdin.
    Jason Deckard

  6. #6
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Re: character occurrence program not working

    Something like this, Nutshell:

    Code:
    printf( "Number of lines to be inputted: " );
    scanf( "%d", &numLines );
    
    /* Here is the new statement */
    gets( tempStr );
    
    for ( i = 0; i < numLines; i++ ) {
      printf( "Enter string: " );
      gets( tempStr );
      strcat( string, tempStr );
    }
    I've tried your code with that change and it works fine.
    Jason Deckard

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    thnx worked fine
    but if only a newline is needed can i just print '\n' after that scanf() ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not working
    By HAssan in forum C Programming
    Replies: 5
    Last Post: 01-11-2009, 02:45 AM
  2. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-02-2008, 12:56 AM
  3. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  4. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 04:21 AM
  5. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 11:14 PM

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