0

I have a function that asks the user for user variable and user number.

void numReplace(char infix[50])
{
    char userVar;
    int userNum;

    printf("Please enter the variable you want to change\n"); 
    scanf("%c", &userVar);

    printf("Please enter the replacement value for the variable\n"); 
    scanf("%d", &userNum);

    printf("%c %d", userVar, userNum);

    int i=0;
    char chrr;
    infix[50] = '\0';
    while((chrr=infix[i++])!='\0')
    {
            if (chrr == userVar){
                    chrr = userNum;
            }
    }
}

when running the program I should be asked the userVar and userNum. However the output is:

Please enter the variable you want to change
Please enter the replacement value for the variable
1

 1

It only takes in one variable, I don't see a problem with my codes. Can someone help me?

  • It's safe to assume that (used properly), `printf()` and `scanf()` work correctly. The difficulty is using them correctly, especially `scanf()`. – Jonathan Leffler Oct 31 '18 at 01:58
  • 1
    Assuming you call this function more than once, then the problem is that [`scanf()` leaves a newline in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). I closed the Q as a duplicate of that — then had second thoughts because I'd not read the question carefully enough, but now I can't rehammer it closed as a duplicate, but I think that is appropriate after all. The trouble is that after the first number is read, the newline is left in the input buffer to be read by the `scanf("%c", &userVar)` on the second call, which throws you off. – Jonathan Leffler Oct 31 '18 at 02:02
  • 1
    Among other things, infix[50] is not a valid subscript if 50 is the size the of the array. The last valid subscript is one less than the size of the array. You don't have to specify array size when you're passing in arrays (unless it's a 2d array where you have to specify 2nd dimension). – Austin Stephens Oct 31 '18 at 02:06

2 Answers2

0

Try adding a getchar(); after every call to scanf().

It's a workaround to the dangling newline character after you press <enter> when using scanf().

See this FAQ for more info.

Andy J
  • 1,479
  • 6
  • 23
  • 40
  • 1
    That is not a good solution to the problem, even if the diagnosis about "newline in the input" is mostly correct. Rather than a `getchar()`, use a blank before the `%c` in the format string.] – Jonathan Leffler Oct 31 '18 at 02:03
  • Could you help explain to me why `getchar()` is a bad solution to the dangling newline? Thanks! – Andy J Oct 31 '18 at 02:15
  • You originally had 'before every call to `scanf()`' — you subsequently changed 'before' to 'after', which is a dramatic difference (and your edit was about a minute after I entered my comment). I trust you can see why 'before' is catastrophically the wrong answer. Using `getchar()` after the `scanf()` is less damaging, but also insufficient for bullet-proofing the code. Suppose the user types `a`, blank, return; then the `getchar()` reads the blank and leaves the return in the input buffer — back to square zero. So, a loop that reads to newline is better. _[…continued…]_ – Jonathan Leffler Oct 31 '18 at 02:20
  • _[…continuation…]_ But a space `" %c"` in the input is also sufficient; it makes `scanf()` skip over newlines and blanks and tabs until it gets something that isn't white space. If the first character read isn't white space, `scanf()` is happy with that too; it skips zero or more white space characters. – Jonathan Leffler Oct 31 '18 at 02:24
  • 1
    Actually, the problem would be if the user typed a number and a space and a return, and then the next call to the function would have problems reading the newline if the `getchar()` read the blank — the basic issue remains "the user was more inventive than the programmer". – Jonathan Leffler Oct 31 '18 at 02:29
  • Thanks for your insights. Sorry about the fast edit; that was actually just a typing mistake caused by "must be first guy to answer". I also now understand why the `getchar()` isn't a bulletproof solution. – Andy J Oct 31 '18 at 02:35
  • 1
    I'm glad you fixed it — it was horrid unfixed — and brought me back so I could review my comment and so on. I should have put something like "You say 'add a `getchar()` _before_ every call to `scanf()`'; that is not a good solution…" so that it would be simpler for all to see why I objected to your answer, and it would be easy to see that you'd changed it. Sloppy commenting! (A good cross-reference on SO is [How to read/parse input in C — the FAQ](https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq).) – Jonathan Leffler Oct 31 '18 at 02:38
0

This uservar may have received the \n you entered last time. If you have input before this input, please accept the newline with getchar().

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256