printf is ouput is being destyoryed by a "\n" after I run scanf. I am attempting to use.
scanf ("%*c");
to chomp the "\n" but its not working... here is the code
printf("Enter char float int char:", char4, deci2, num2, char5);
scanf ("%c %f %d %c", &char4, &deci2, &num2, &char5);
scanf ("%*c");
printf("You entered: '%c' %.3f %d '%c' " ,char4 ,deci2, num2, char5 );
and it outputs to
Enter char int char float:a 5 a 5.5
You entered: 'a' 5 'a' 5.500
Enter char float int char:a 5.5 6 b
You entered: '
' 0.000 0 ''
The line
scanf ("%c %f %d %c", &char4, &deci2, &num2, &char5);
is picking up the stray newline left in the input buffer from your previous scanf call. You can work around that by putting a space in front of the first %c:
scanf (" %c %f %d %c", &char4, &deci2, &num2, &char5);
This will tell scanf to skip over any leading whitespace (blanks, newlines, tabs, etc.) before reading the next non-whitespace character.
The line
printf("Enter char float int char:", char4, deci2, num2, char5);
is a bit of a head-scratcher; it won't cause any problems (the excess arguments are evaluated, but otherwise ignored), but it looks wrong, and indicates some confusion.
In this line:
printf("Enter char float int char:", char4, deci2, num2, char5);
remove the char4, deci2, num2, and char5. If you're still seeing issues as Carl Norum suggests, then try consuming the newline character left over from your first scanf as such:
scanf ("%c %f %d %c", &char4, &deci2, &num2, &char5);
getchar();
printf("You entered: '%c' %.3f %d '%c' " ,char4 ,deci2, num2, char5 );
It's the remaining \n in the buffer from your first read that's still hanging around. You need to eat that one before doing the second scanf, not after.
Editorial note: You should really include all of the relevant code in your question rather than just a subset. I could only infer this to be your problem from the output you provided. It contains the string Enter char int char float which isn't present in your example code.
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 months ago.
I am stuck in this program, help me, it'll be appreciatable. But, scanning the characters together might work. I want to know why is scanf not asking for the character in 2nd time.
#include <stdio.h>
int main()
{
printf("--Mathematical operations on character to get other character--\n");
char a, b;
printf("Enter the 1st character: \n");
scanf("%c", &a);
printf("Enter the 2nd character: \n");
scanf("%c", &b);//It's not scanning 2nd time, instead moving forward to printf function
printf("\nThe ASCII equivalent for addition of %c and %c is %c\n", a, b, (a + b));
printf("\nThe ASCII equivalent for subtraction of %c and %c is %c\n", a, b, (a - b));
printf("\nThe ASCII equivalent for multiplication of %c and %c is %c\n", a, b, (a * b));
printf("\nThe ASCII equivalent for division of %c and %c is %c\n", a, b, (a / b));
printf("\nThe ASCII equivalent for modular division of %c and %c is %c\n", a, b, (a % b));
return 0;
}
After scanf("%c", &a);, the next character in the input stream is the \n from the user pressing return or enter. Then scanf("%c", &b); reads that character.
To tell scanf to skip the \n, use scanf(" %c", &b);. The space tells scanf to read all white-space characters until a non-white-space character is seen.
So I wrote this program using coderunner,
#include <stdio.h>
int main()
{
int num1, num2;
scanf("%d%d", &num1, &num2);
if (num1 > num2)
printf("The min is:%d\n ", num2);
else
printf("The min is:%d\n ", num1);
return 0;
}
The problem is that the program wont run. It keeps showing this and then it stops after a while:
Removing the scanf fixed the issue, I've tried other programs using scanf and it was fine. Any ideas?
How do you expect scanf() to interpret e.g. 123 or 1232 as two integers? Chances are all digits you enter are "eaten" by the first %d, and then scanf() waits for more for the second.
You must use some separation, or some non-numeric character between them:
scanf("%d/%d", &num1, &num2);
This tells scanf() to expect a slash between the two numbers. You could just use whitespace (without any in the format string, as pointed out in comments) too of course.
Also, you should check the return value before relying on the numbers:
if(scanf("%d %d", &num1, &num2) == 2)
{
}
In the following code
scanf("%s", input);
sscanf(input," %s %f ", change, &grades);
printf("%s\n", change);
printf("%f", grades);
grades will be printed as 0.00, no matter the input. Why doesn't sscanf recognise float format?
The first call to scanf is looking for a sequence of characters delimited by whitespace. If your input looks something like this:
test 98.3
Then input will only contain the string test.
If you want to read a full line of text so you can later parse it with sscanf, use fgets instead which will read a line:
char input[100];
char change[10];
float grades;
fgets(input, 100, stdin);
sscanf(input," %s %f ", change, &grades);
printf("%s\n", change);
printf("%f", grades);
Input:
test 96.5
Output:
test
96.500000
scanf("%s", str) reads from stdin upto a newline or whitespace that mean you are reading input as change78.12 here change is change string value and 78.12 is float grade value. when you'll do sscanf(str, "%s %f", change, &grade) since str is valid %s type so it will read by change only so nothing left for grade so it takes default as 0.00 . If you want to read from stdin and then do scanf then do this.
// input format is (separated by whitespace)
// change 78.5
fgets(input, input_length, stdin);
sscanf(input,"%s %f ", change, &grades);
printf("%s\n", change);
printf("%f", grades);
This question already has answers here:
Problems with character input using scanf()
(4 answers)
Closed 7 years ago.
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);
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
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
Updated
I am having some trouble with Question 2 on Chapter 4 from "A Guide to C Programming" by Paul Kelly.
As far as I can tell, my syntax is correct, however when the program reaches line 36 the program auto fills the scanf(); variable slots.
I can not seem to find a solution to this problem anywhere.
Here is my code. I have put a marker beside line 36
/*
Program to demonstrate single scanf function
to read various data types and output results.
*/
#include <stdio.h>
main(){
int first;
int second, third, fourth;
float principle, rate, time;
char keyVal1, keyVal2;
char c;
int i;
float f;
double d;
printf("\nPlease Enter an Integer\n");
scanf("%d", &first);
printf("\nYou Entered %d\n", first);
printf("\nThank You, Please Enter 3 more integers\n");
scanf("%d %d %d", &second, &third, &fourth);
printf("\nYou Entered %d %d and %d\n", second, third, fourth);
printf("\nGreat,
now please enter decimal values for principle,
rate and time.\n");
scanf("%f %f %f", &principle, &rate, &time);
printf("\nYou Entered %.2f %.2f and %.2f\n", principle, rate, time);
printf("\nPlease Enter any 2 characters\n");
scanf(" %c %c", &keyVal1, &keyVal2);
printf("\nYou Entered %c and %c\n", keyVal1, keyVal2);
// ***36
printf("\nNow Enter any other character,
followed by an integer and 2 decimal numbers\n");***
scanf(" %c %d %f %lf ",&c, &i, &f, &d);
printf("\nYour character was %c.\nYour integer was %d\nYour
decimal numbers were %.2f and %.2lf\n", c, i, f, d);
}
scanf("%1s %1s", &keyVal1, &keyVal2);
Use %c specifier instead of %1s .
scanf(" %c %c", &keyVal1, &keyVal2);
Similarly ,
scanf("%c %d %f %lf ",&c, &i, &f, &d);
leave a space before %c -
scanf(" %c %d %f %lf ",&c, &i, &f, &d);
You need to leave space because as ENTER is pressed after previous scanf '\n' remains in stdin .
scanf("%1s %1s", &keyVal1, &keyVal2);
it should be...
scanf(" %c %c", &keyVal1, &keyval2);
you need to use the c identifier, that will end the crashing, now after you do that you'll notice that the first character doesn't seem to be read in. This is because you have a printf() right before, so your scanf() will read in the last char in the printf() which was the \n character. We have to fix this by putting a space before the identifier, so it won't read in the last value in stdin.
Also you need to change the printf() statements to the same identifiers otherwise an 'undefined action will occur'.
Also, the last scanf() change it to this...
scanf(" %c %d %f %lf",&c, &i, &f, &d);
By taking out the space in after the last identifier, the program works well.
the problem is with this line:
scanf(" %c %d %f %lf ",&c, &i, &f, &d);
the space at the end of the format string means that all 'white space' is consumed until a non-white space character is encountered in the input stream.
so the scanf() never completes until a non white space character is entered by the user, after entering the 4 requested values.
To fix the problem, remove the trailing space in the format string.
scanf(" %c %d %f %lf",&c, &i, &f, &d);