C: scanf and printf error during second prompt input [duplicate] - c

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

Related

C Program Crashing... 'Read in and Output Various Data Types'

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);

Can somebody tell me why I have this runtime error?

I have created a program on Pelles C, however, when I run it, it is skipping straight to the end of the function simply saying "press any key to continue"
#include <stdio.h>
int main()
{
char letter;
int num1, num2;
printf("Enter any one keyboard character ");
scanf("%c", &letter);
printf("Enter 2 integers seperated by a space ");
scanf("%d %d", &num1, &num2);
printf("Numbers inputted were %d and %d \n" num1, num2);
printf("letter input %c", letter);
printf(" Stored at: %p \n", &letter);
return 0;
}
Can anybody tell me why this is happening ?
printf("Numbers inputted were %d and %d \n" num1, num2);
^
You missed a , before num1 in above printf statement.
printf("Numbers inputted were %d and %d \n",num1, num2);

Printing ASCII-code values and their equivalent chars in C

How could I print char for given ASCII code value?..
I saw this question, my problem is similar, just contrary
Below is my program, but it doesn't work.
int main (){
int code; // code that user enters
char ch; //"codes" related ASCII charcter
printf("Enter a ASCII code value: \n");
scanf("%d", &code);
ch=code;
printf(" %c is cherechter that have %d ASCII code\n", &ch ,&code);
system("PAUSE");
return 0;}
In your code, you have to change your printf statement
printf(" %c is cherechter that have %d ASCII code\n", &ch ,&code);
to
printf(" %c is cherechter that have %d ASCII code\n", ch ,code);
because, to print the values, you don't need to supply the address of the variables. The variable name is enough.
Change your code to:
int main (){
char code; //change from int to char
char ch;
printf("Enter a ASCII code value: \n");
scanf("%c", &code);//ASCII is a character not integer
ch=code;
printf(" %c is cherechter that have %x and %d ASCII code\n", ch ,code,code);//don't print the address access the value
system("PAUSE");
return 0;}

printf ouput is being destroyed by a "\n" after scanf

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.

Switch small program does not let me see result

I am learning the switch statement of C. This is my small program and it runs and does the calculation but doesn't let me see the result of the operation. The black window shows up so that I input the numbers and the operator and then for a fraction of a second shows the result and disappears. Any help is appreciated.
#include <stdio.h>
int main(int argc, char *argv[])
{
int num1, num2, ans=0;
char ch, name;
printf("Enter a value: ");
scanf("%d", &num1);
printf("Enter a second value: ");
scanf("%d", &num2);
printf("Input * To multiply\
+ To add\
- To subtract: ");
scanf(" %c", &ch);
switch(ch)
{
case'*':
ans=num1 * num2;
printf("%d times %i equals: %i",num1,num2,ans);
break;
case'+':
ans=num1+num2;
printf("%i plus %i equals: %d",num1,num2,ans);
break;
case'-':
ans=num1-num2;
printf("%d minus %d equals: %d",num1,num2,ans);
break;
default:
printf("Range numbers");
}
getchar();
return ch;
}
Probably due to output buffering. Add newlines (\n) last in your formatting strings.
As a newbie, you should end all your printf format string with an escaped newline \n, i.e. printf("%i plus %i equals %d\n", num1, num2, ans); (or you should call fflush(stdout); just after the end of the switch before the getch and before all your scanf).

Resources