0

I am struggling to figure out why my scanf() is reading a white space character. I have searched and found the problem can be fixed by adding a space between the quotation and %c. Example:

scanf(" %c..);

However, this does not solve my problem.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>

int main(void) {
    char ch, ch1, ch2, ch3, ch4;
    ch = 'a';

    unsigned short int;

    double b = INFINITY;

    short u;
    char c;
    float f;

    printf("%c\n", ch);

    printf("%d\n", ch);

    printf("%d\n", SHRT_MAX);

    printf("%lf\n", b);

    printf("Enter char int char float: \n");
    scanf("%c %d %c %f", &ch1, &u, &ch2, &f); // This line reads correctly. Ex.  
                                              // a 5 b 5.5 

    printf("You entered: %c %d %c %0.3f\n", ch1, u, ch2, f);

    printf("Enter char float int char: \n");
    scanf(" %c %f %d %c", &ch3, &f, &u, &ch4); // This line reads    5.5 5 a 
                                               // Here is where the first %c
                                               // is being read as a white space.
    printf("You entered: %c %0.3f %d %c\n", ch3, f, u, ch4);
    return 0;
}

Edit Here is what I am getting.

a
97
32767
1.#INF00
Enter char int char float:
a 5 b 5.5
You entered: a 5 b 5.500
Enter char float int char:
c 5.5 5 d
You entered:   5.500 5 d

Process returned 0 (0x0)   execution time : 11.929 s
Press any key to continue.

Edit #2

I have tried the following - all resulting in the same output.

printf("Enter char int char float: \n");
scanf("%c %d %c %f", &ch1, &u, &ch2, &f);
printf("You entered: %c %d %c %0.3f\n", ch1, u, ch2, f);

fflush(stdin);

printf("Enter char float int char: \n");
scanf(" %c %f %d %c", &ch3, &f, &u, &ch4);
printf("You entered: %c %0.3f %d %c\n", ch3, f, u, ch4);

Edit #3

Here is a more concise version of my problem.

    printf("Enter char int char float: \n");
    scanf("%c %d %c %f", &ch1, &u, &ch2, &f); // This line reads correctly. Ex.  
                                              // a 5 b 5.5 

    printf("You entered: %c %d %c %0.3f\n", ch1, u, ch2, f);

    printf("Enter char float int char: \n");
    scanf(" %c %f %d %c", &ch3, &f, &u, &ch4); // This line reads    5.5 5 a 
                                               // Here is where the first %c
                                               // is being read as a white 
                                               // space. I am expecting there 
                                               // to be a character here. Not 
                                               // a white space. See Edit #1 
                                               // for output results.
    printf("You entered: %c %0.3f %d %c\n", ch3, f, u, ch4);
    return 0;
}
Pustafix
  • 21
  • 5

3 Answers3

0

Try this:

char ch5;

scanf("%c %c %f %d %c",&ch5,&ch3, &f, &u, &ch4);

It worked for me. All I've done is making another char variable to catch the trailing newline character.

Nipun Thennakoon
  • 3,586
  • 1
  • 18
  • 26
0

I have found the solution. However, I am still unsure why some people had a successful return while I did not.

The answer, for me at least, is in the short u;.

The short should be int.

I don't know enough about C to explain the reason. It actually further confuses me because my problem with the %c, not the %d.

Pustafix
  • 21
  • 5
-1

Reason : The reason that you are getting this problem is because scanf() reads the return key that you hit as an input into the buffer. Read more about this here :

https://cboard.cprogramming.com/c-programming/88352-scanf-also-reading-return-character-when-i-enter-value-hit-return.html

Solution: Just add a getchar() between the two scanf() statements and it is working fine.

scanf("%c %d %c %f", &ch1, &u, &ch2, &f); // This line reads correctly. Ex.  
                                          // a 5 b 5.5 
getchar();
printf("You entered: %c %d %c %0.3f\n", ch1, u, ch2, f);

printf("Enter char float int char: \n");
scanf(" %c %f %d %c", &ch3, &f, &u, &ch4); // This line reads    5.5 5 a 
                                           // Here is where the first %c

Output :

a

97

32767

inf

Enter char int char float:

You entered: a 1 b 2.500

Enter char float int char:

You entered: b 2.500 1 f

Link to Submission : https://ideone.com/GPd3HK

Varad Bhatnagar
  • 599
  • 1
  • 7
  • 19
  • That explanation is nonsense. `" "` at the beginning of the format string already consumes all available whitespace. – melpomene Apr 14 '18 at 13:24
  • I have tried the `getchar();` but does not change anything. I am currently rewriting the section to figure out why I am not getting what I am expecting. Thanks for your input. – Pustafix Apr 14 '18 at 13:32
  • @Pustafix Could be a compiler issue. As it seems to be working fine on online IDEs. – Varad Bhatnagar Apr 14 '18 at 16:40
  • @VaradBhatnagar I am certain it is a compiler issue. I have reinstalled my programs and tried updating the compiler but still have the issue. Thanks for the help. – Pustafix Apr 14 '18 at 18:36