I was trying to use scanf but when I run the code the code goes on forever. And when I stop the code I get
[Done] exited with code=1 in 28.291 second
My question is what am I doing wrong? Here is the code I was trying to execute. My editor is VS code 1.21.0.
#include <stdio.h>
#include <string.h>
int main(){
char midInitial;
printf("What is your middle initial? ");
scanf(" %c", &midInitial);
printf("Middle inital &c ", midInitial);
}
The code does not run forever, it is waiting for the user to complete the input. Standard input is line buffered, so the user must type enter after the character for scanf() to process it. It will skip any white space characters, store the first non white space character into midInitial and return 1.
Another possible explanation is the environment you use does not support console input. Run your program from shell interpreter window in Windows.
There is another problem: the format "Middle inital &c " is incorrect, use % for conversion specifiers.
Here is a corrected version:
#include <stdio.h>
int main() {
char midInitial;
printf("What is your middle initial? ");
if (scanf(" %c", &midInitial) == 1) {
printf("Middle initial: %c\n", midInitial);
}
return 0;
}
go to code-runner extention setting and make sure <Code-runner: Run In Terminal>
checked
int main(){ char midInitial;
printf("What is your middle initial? ");
scanf(" %c", &midInitial);
printf("Middle inital %c ", midInitial); (see it should be %c)
}
Related
this is my program, this is not complete yet but, when I run this program, the single character reading part of it won't read, I checked without adding (&)ampersand. I tried many ways with arrays and so on, but I can't get the result. please help
#include <stdio.h>
#include <stdlib.h>
int main()
{
int F,S,E,SM,EM;
char t1,t2;
//*S=starting time*//
//*SM=start minute*//
//*F=ending time*//
//*FM=end minute*//
printf("\nEnter the start time: Hour : Minute");
scanf("%d %d",&S,&SM);
printf("\nEnter a for AM Enter p for PM");
scanf(" %c",&t1);//problem
printf("\nEnter end time: Hour: Minute");
scanf("%d %d,",&E,&EM);
printf("\nEnter A for AM: Enter P for PM");
scanf(" %c ",&t2);//problem
return 0;
}
}
Scanf() will try to consume ALL the input, which means that the '\n' from pressing enter will carry over to the next scanf call. This is alleviated by you using a leading space character in your format " %c". However, if the format of the input does not exactly match the actual input, i. e. format is "%d %d", but the input is, say " 1 1", scanf() will fail to write the format. I think this is why it's failing for you after compiling and testing out your code.
Consider adding some logging and error checking into this code snippet, so that it's easier to pin down these errors. Even something as simple as printf("%d %d\n", S, SM); after a scanf() call is much better than not tracking your results at all.
So, I have a printf, that asks for the users middle initial, then I have a scanf under that, then I output the users middle initial. My problem is that my printf is displaying after my scanf
C Code
#include <stdio.h>
#include <string.h>
int main(void) {
char middleInitial;
printf("What is your middle initial? ");
scanf(" %c", &middleInitial);
printf("Middle initial %c", middleInitial);
}
So as you can see, there are two printf's. My scanf is running before my first printf displays the question.
Example (This is what I'm getting in my terminal)
$ ./a.exe
c
What is your middle initial? Middle initial c
What I want
$ ./a.exe
What is your middle initial? c
Middle initial c
By the way, the c is what the user inputs
Call fflush(stdout) before your call to scanf().
type fflush(stdout); just before your first scanf() should be after first printf()
In my windows pc I face the same issue. If you use different operating system you might not need it.
I want to make a program which take Roll No and Full name as input and simply display it
My code is . this code skip scaning value of n through gets function. Why this error occur and how to over come this?
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
char n[30];
printf("enter your roll no");
scanf("%d",&r);
printf("enter your full name");
gets(n);
printf("roll no is %d ",r);
printf("name is %s ",n);
getch();
}
while the below code scan the first gets value and skips the second one.
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
char n[30], f[30];
printf("enter your roll no");
scanf("%d",&r);
printf("enter your full name");
gets(n);
printf("enter your full name of your father ");
gets(f);
printf("roll no is %d ",r);
printf("name is %s ",n);
printf("father name is %s ",f);
getch();
}
The code DOES NOT skip scanning the value of 'n'.
I believe that when you run the program, you enter the Roll No and then press the ENTER key on your keyboard.
This is the cause.
As soon as you press the ENTER key, the escape sequence '\n' is saved in the array n. Your gets() command is executing perfectly.
In the second case, the variable 'n' stores the escape sequence and the next variable 'f' takes the string you enter next.
To make your code work just enter your scanf statement like this:-
scanf("%d ",&r);
Notice the space after %d.
Try this code-
#include<stdio.h>
int main(void)
{
int r;
char n[30], f[30];
printf("Enter your roll no");
scanf("%d ",&r); // I have inserted a space after %d
printf("Enter your full name");
gets(n);
printf("Enter your full name of your father ");
gets(f);
printf("\nRoll no is %d ",r);
printf("\nName is %s ",n);
printf("\nFather name is %s ",f);
return 0;
}
TIP:- You must try not to use gets() and puts()
You can read more about it here.
The simple solution for the problem is to add fflush(stdin); between scanf(); and gets();
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
char n[30],fn[30];
clrscr();
printf("\nEnter roll ");
scanf("%d",&r);
fflush(stdin);
printf("\nEnter name ");
gets(n);
printf("\nEnter father name ");
gets(fn);
printf("\n\nRoll %d",r);
printf("\nname %s",n);
printf("\nfather name %s",fn);
getch();
}
Using scanf instead of gets will solve your problem:
scanf("%s", n); // Read in your name
Please note that when reading in any string like this you should use safe functions that are passed the length of the string (for example scanf_s from MSDN).
I don't know why it gets skipped but what you could do to avoid any other confusion like fflush(stdin) or fgets etc etc.
Just use gets(string) on the next line. So when it skips the first gets command it goes onto the other one.
Try that
Cheers,
;)
I just had the same problem two hours ago, but to solve this situation easily, all you have to fo is to add a "getchar()" after the "scanf()" and before the "gets()", so that the extra "\n" goes to the "getchar()" and you can type as you want in the next "gets()".
I was also facing the same problem as mentioned above.. so with the help of the answers mentioned here and using hit and trial method, I found that when we press enter after giving input to any variable using scanf(), \n is stored in the next gets() function.. and next time it doesn't take any input from the keyboard.. so to avoid this just use getchar() in between the scanf() nd gets() nd also remember that getchar() takes only 1 character.. so don't give any extra input to scanf() as again this will be stored and will be used in gets() nd the problem will remain the same....
hope this will help..
thank u..
I am using getchar() in order to read characters and put them on a table, as well as scanf in order to get an integer.
The problem with the scanf() is that it doesn't wait for the users' input but reads from the buffer the last character given on the previous line, with getchar().
I tried sscanf, fflush(stdin); etc but I'm getting still the same behavior.
#include <stdio.h>
#include <stdlib.h>
main()
{
int i, choice, tmp_day, tmp_month;
char name[5];
printf("insert choice(1-3):\n");
scanf("%d",&choice);
printf("name: ");
for (i=0;i<5;i++) name[i]=getchar();
name[5] = '\0' ;
printf("day (1-31): ");
scanf("%d",&tmp_day);
printf("month (1-12): ");
scanf("%d",&tmp_month);
printf("\n%d %d", tmp_day, tmp_month);
}
Any idea?
Thanks in advance.
Detailed discussion about fflush(stdin) which not necessarily portable.
http://c-faq.com/stdio/gets_flush2.html
After each scanf use this statement:
fflush(stdin);
Why does following program produce two output message at the same time, without asking for any input from the user???
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char input;
do {
printf("Enter a single character: \n");
scanf("%c", &input);
printf("The ordinal value is %d. \n",input);
} while(input != '#');
return 0;
}
The output is followings:
Enter a single character:
s
The ordinal value is 115.
Enter a single character:
The ordinal value is 10.
Enter a single character:
Terminal input is read line at a time unless you specify otherwise; scanf reads one character as specified, leaving the newline you typed afterward to send the line in the input buffer for the next pass of the loop. Consider reading input by lines and using sscanf() or similar to parse those lines.
Just insert getchar(); after your call to scanf. This will eat the newline. The suggestion to use scanf("%c\n", &input); seems sound, but I've never found it to work well; I wonder if anyone can tell me why?