0

The first half of this code runs with no issues. I can enter in the second set of char float int char input but not sure why my code displays zeros in display prompt.

/*Prompt the user and accept the following 4 types of values from a single 
input line: char int char float */
char ch1, ch2;
int num1;
float num2;
printf("Enter char int char float: ");
scanf("%c %d %c %f",&ch1, &num1, &ch2, &num2)   ;
printf("You entered: '%c' %d '%c' %.3f\n", ch1, num1, ch2, num2);


/*Prompt the user and accept the following types of values from a 
single input line: char float int char */
char ch3, ch4;
float num3;
int num4;
printf("Enter char float int char: ");
scanf("%c %f %d %c",&ch3, &num3, &num4, &ch4);
printf("You entered: '%c' %.3f %d '%c'\n", ch3, num3, num4, ch4);
Steph_B
  • 1
  • 1

2 Answers2

2

The space is the problem.

The first enter ENTER you stroke is considered as input for the second scan. Use a space like the following:

scanf("[SPACEHERE]%c %f %d %c",&ch3, &num3, &num4, &ch4);

Here for more details

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
0

You can use getchar(). getchar() takes an unknown char form stdin . use after first prinf.

printf("You entered: '%c' %d '%c' %.3f\n", ch1, num1, ch2, num2);

getchar();//add this