Discrepancy betwenn scanf and get_int [duplicate] - c

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years ago.
discrepency
Hi everyone,
Currently working on some Harvard's CS50 problems, I've noticed that substituting get_int by scanf makes my program behave erratically.
The problem at hand here consists in creating a plurality election mockup. And the simple fact that I replace get_int by scanf in order to register the number of votes during the election ends up signalling the first vote as 'invalid' (a function later in the code is charged of checking vote validity).
I've checked multiple times, used the debugger, but to no avail.
Any hints as to why that happens would be deeply appreciated.

scanf("%i", &vote_count) does not consume anything after the number. What comes after the number is most likely a newline character, which is what will be seen by get_string. That probably causes the first name to be an empty string.
You should see all that if you use a debugger.

Related

It's possible to read the text using scanf? [duplicate]

This question already has answers here:
How do you allow spaces to be entered using scanf?
(11 answers)
Closed 3 years ago.
I need to read the text using scanf, but what I know scanf is reading characters to the first space. I don't know that it's possible to read more words into the array by scanf and i have no any idea.
That's not correct; scanf reads according to the format string you give it.
Nothing keeps you from using a format that reads past spaces, returns, or whatever else, for example scanf("%50c",buffer); will read 50 characters, no matter what they are; or scanf("%[^|]", buffer); will read everything up the first |. Read up on the definition of the scanf family.

how to get input and print something at the same time in C [duplicate]

This question already has answers here:
Non blocking getch()
(2 answers)
Closed 3 years ago.
I am making a typing game in C Language in this game letters come from the top of the window and user has to type that letter to delete it and to stop it from touching the specific boundary line if it touches the boundary line user lose points and ultimately lose the game. Now I am having trouble to print the list of the characters and take input at the same time. If I tried to use the scanf() or getch() function they both stop the printing process is there any way that I could get input from a user without stopping the printing process.
Kbhit Function really Works for that problem.

can we use for loop without condition? [duplicate]

This question already has answers here:
What is the full "for" loop syntax in C?
(6 answers)
Closed 5 years ago.
My question is simple,thus I will not go in deep
can we use for() loop without condition like this
for(;;space+=1)
{
printf(" ");
break;
}
Of course you can. An empty condition is taken to evaluate to 1.
for (;;){/*ToDo - your code here*/} is idiomatic C.
Yes it is perfectly correct to do so.
But since you have provided a break immediately after printf, it will only execute once. I'm not sure whether this is what you wanted. But if so, then this works fine.

Difference between scanf and scanf_s in C [duplicate]

This question already has answers here:
Difference between scanf and scanf_s
(3 answers)
Closed 8 years ago.
Although my program gives the required output. There are many warnings showing scanf() shouldn't be used and try using scanf_s() instead.
Possible cause of this warning??
This is happening to all the programs which are using the scanf function. Even simple addition of numbers.
With scanf, with some format arguments it is possible to crash your program if you use unlimited size inputs. scanf_s requires to provide size of output buffers, thus limiting possibility of buffer overflows (provided you specify output buffer sizes correctly).

fflush(stdin) before gets() in c [duplicate]

This question already has answers here:
Using fflush(stdin)
(7 answers)
Closed 8 years ago.
Okay , i was solving a problem in code chef (very easy).
It briefly states that :
-A question as a string will be given , and another string has to be produced which does not have any letter used in question string. Uppercase and lowercase are assumed to be same.
-If all alphabets have been used just print ~ sign.
My whole logic is correct except i caught error in my input and this was caused by using fflush(stdin) before gets(). Please explain why was this an error ? Ignore the rest of the code.
Link to problem :http://www.codechef.com/problems/NOLOGIC/
Link to wrong solution :http://www.codechef.com/viewsolution/3881817
Link to corrected solution :http://www.codechef.com/viewsolution/3881827
Pls Note :In corrected code i have only made change in not using fflush(stdin) and using getchar()
Being that the behavior of fflush(stdin) is implementation specific; and being that the implementation (for this question) is unknown, the behavior will also be unknown.
You may safely expect the behavior of fflush(stdin) to be unpredictable.

Resources