1

I have the following application written in C:

The application basically presents the user with a word containing one incorrect letter. The user is requested to provide the position of the incorrect letter and replace it with a new letter.

The problem is that if I try to change letter number 4 (array index 3), the new word would be Act instead of Actually. If I do it programmatically, that is, change this line

string[letter_number - 1] = change;

to this

string[letter_number - 1] = 'u'

everything works fine. How can I solve this problem please? Thanks.

1
  • You are discarding the return value of scanf. This is a serious programming error, and there's little point guessing around until you fix this.
    – Kerrek SB
    Oct 5, 2012 at 14:42

3 Answers 3

1

Replace your scanf_s by a simple scanf and you will be done. Or else you can use

scanf_s("%d ", ...);

and remove the getchar();

This works for me :

#include <string.h>
#include <stdio.h>

int main()
{
  char string[9] = "Actwally";
  int letter_number;
  char change;

  printf("---Spot the Odd Letter Out---\n\n");
  printf("The word below contains one letter which is incorrect:\n\n");
  printf("Word: %s\n\n\n", string);
  printf("Please provide the position of the incorrect letter and propose a new   letter\n\n");
  printf("Position of incorrect letter: ");
  scanf("%d ", &letter_number);
  printf("\nProposed new letter: ");
  scanf("%c ", &change);
  string[letter_number - 1] = change;
  printf("\n\nThe new word looks like this %s\n\n\n", string); 
  if(strcmp("Actually", string) == 0)
  {
    printf("You are right!  Congratulations!");
  }
  else
  {
    printf("Sorry, but you have not guessed the word.  Better luck next time!");
  }
  printf("\n\n\nPlease press enter to exit the program");
  getchar();
}
2
  • Thanks. Now I can finally remove getchar(). But my problem still persists :(
    – Matthew
    Oct 5, 2012 at 14:51
  • Thank you so much. Your answer helped me to solve the problem :)
    – Matthew
    Oct 5, 2012 at 15:11
0

Verify that your inputs are correct before using them. It sounds as if change gets set to 0, terminating the string.

I'm not sure about your getchar() calls between the scanf() calls, they could lose input.

4
  • Without the getchar(), the program does not allow me to provide the second input (the new letter) for some reason or another. Is there a way around it? I am kind a newbie to C. Moreover, I am programming in Visual Studio. Does this create any problems?
    – Matthew
    Oct 5, 2012 at 14:41
  • I don't think the getchar has an effect though as the program changes the string at the correct position, so the value in letter_number is not lost. The problem lies with the value in change.
    – Matthew
    Oct 5, 2012 at 14:43
  • @Matthew, lose the get_char and write the scanf with " %c" (with space before %) to skip whitespace.
    – Shahbaz
    Oct 5, 2012 at 14:44
  • or write the previous one with "%d ", but anyway replace the scanf_s
    – Eregrith
    Oct 5, 2012 at 14:49
0

Matthew,

Basically I tested your code in GCC compiler. I was in need to change the following things to make it work

  • The newline character in the "scanf_s" shouldn't be there. The reason for this is as follows. If a white space character (include spaces, newline and tab characters) is present within the format specifiers, the scanf function will read and ignore (from stdin) any whitespace characters encountered before the next non-whitespace character . As a consequence of this, in this specific use case; your program execution will wait infinitely in this scanf statement even after you have entered your input followed by an enter key.

scanf_s("%c\n", &change); //Change this as below

scanf("%c", &change);

  • As per me there is less purpose to use "getchar" after scanfs. The most proper way should be flush the input buffer after the first scanf, so that the second scanf actually expects user input rather to picking from the input buffer.

PS: Please be aware that fflush(stdin) wont work with GCC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.