Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Program not waiting for input

Posted on 2009-04-13
2
364 Views
Last Modified: 2012-06-21
Please view code below...

Using Bloodshed Dev-C++ 4.9.9.2 compiler...

I'm curious why when I compile and run my code the first section involving string s1 runs just fine; prints string, asks what char I want (the letter 'a' in this case- passes to function, etc.

next section, s2, doesn't ask what char I want.  It prints string, prints "Letter to be removed", then prints string again.  Output below:

String is: abracadabra
Letter to be removed?
a
String is: brcdbr


String is: abracadabra
Letter to be removed?
String is: abracadabra


Press any key to continue . . .

I copied and pasted the code from s1, and only changed s1 to s2, so I don't understand why this is happening.  
/*
 
Using pointers, write a function, rmchr, that takes a string and a character as 
arguments, removing all occurrences of the character from the string, watching 
for sequential occurences.  rmchr should not leave holes in the string.
 
Put the string both before and after the call to rmchr to demonstrate that the 
original string was modified
*/
 
#include <stdio.h>
#include <string.h>
 
main()
{
    void rmchr(char string[], char remove);     //function to remove a character
    void printstring(const char string[]);      //function to print a string
       
    
    char s1[]="abracadabra";                          //test string s1-s5
    char s2[]="abracadabra";                          
    char s3[]="abracadabra";                          
    char s4[]="aaaa";                                 
    char s5[]="aaaa";                                 
    char letter;                                      //letter to be removed
    
    printstring(s1);                                  //print original string
    printf("Letter to be removed?\n");
    scanf("%c", &letter);                             //gets letter to remove
    rmchr(s1, letter);                                //calls function, passes variables
    printstring(s1);                                  //print altered string
    printf("\n\n");
    
    printstring(s2);                                  //print original string
    printf("Letter to be removed?\n");
    scanf("%c", &letter);                             //gets letter to remove
    rmchr(s2, letter);                                //calls function, passes variables
    printstring(s2);                                  //print altered string
    printf("\n\n");
    
    system("Pause");
    return 0;
}
 
//function to remove a letter
void rmchr(char string[], char remove)
{
    char temp[strlen(string+1)];                      //temp array
    char *ptr = string;                               //pointer to start of string
    char *endptr = string + strlen(string);           //pointer to end of string
    char *tempptr = temp;                             //pointer to start of temp string
    
    while(ptr != endptr)
    {
         if(*ptr == remove)                           
              ptr++;
         else
         {
              *tempptr = * ptr;                       //copies letter to temp string
              ptr++;                                  //if it doesn't match char 
              tempptr++;                              //to remove
         }
    }
    *tempptr = '\0';                                  //null 0 at end of temp string
    
    strcpy(string, temp);                             //copies to original string
}
 
//function to print string
void printstring(const char string[])
{
     printf("String is: %s\n", string);               
}

Open in new window

0
Comment
Question by:Geisrud
2 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 125 total points
ID: 24131873
Hi Geisrud,

You have to be very careful of newline characters.  When you call scanf() or any of its sister functions, input is scanned until a character that does not match for the format string is found.  If the format specifier is a string type, the rest of the string is copied, just as you'd expect.

But if the format specifier is numeric (d, i, f, etc.) the scan stops at the first non-digit and the character is kept in the buffer to be used as the first character of the next read operation.  Irritatingly, this includes newline characters.

You'll need to modify the scanf() statement so that it skips the trailing newline.  Either that or call fflush(stdin) before pausing at the scanf().


Good Luck,
Kent
0
 
LVL 14

Author Comment

by:Geisrud
ID: 24132271
I guess I don't 100% understand what's going on with that (just more thought required on my part...), but I changed my scanf statement (lines 29 and 36 in my code sample) to a getch statement:

letter=getch();

and I get results I can work with.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question