C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-21-2002, 06:38 PM   #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;
}
Nutshell is offline   Reply With Quote
Old 01-21-2002, 07:40 PM   #2
B26354
 
Deckard's Avatar
 
Join Date: Jan 2002
Posts: 631
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
Deckard is offline   Reply With Quote
Old 01-21-2002, 08:27 PM   #3
Unregistered
Guest
 
Posts: n/a
i haven't learnt fgets() yet. Anyway to fix the problem using my original code?

thnx
  Reply With Quote
Old 01-21-2002, 08:29 PM   #4
Registered User
 
Nutshell's Avatar
 
Join Date: Jan 2002
Posts: 1,020
the above reply was mine, forgot to log in...
Nutshell is offline   Reply With Quote
Old 01-21-2002, 08:33 PM   #5
B26354
 
Deckard's Avatar
 
Join Date: Jan 2002
Posts: 631
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
Deckard is offline   Reply With Quote
Old 01-21-2002, 08:38 PM   #6
B26354
 
Deckard's Avatar
 
Join Date: Jan 2002
Posts: 631
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
Deckard is offline   Reply With Quote
Old 01-21-2002, 10:31 PM   #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() ?
Nutshell is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Program not working HAssan C Programming 5 01-11-2009 02:45 AM
Problem with my program i cant figure out... youareafever C Programming 7 11-01-2008 11:56 PM
Printing "shapes" based on character lines and number of line inputs matt_570 C++ Programming 11 10-07-2008 06:19 PM
Program Not working Right raven420smoke C++ Programming 2 09-16-2005 03:21 AM
Simple Program not working right (in C) DruzeTito C Programming 5 06-01-2002 10:14 PM


All times are GMT -6. The time now is 05:59 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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