Scan statements executed before print - c

I have this code.
#include <stdio.h>
int main(void)
{
int dogs;
printf("How many dogs do you have?\n");
scanf("%d", &dogs);
printf("So you have %d dog(s)!\n", dogs);
return 0;
}
To my knowledge, when executing, the program will say 'How many dogs do you have' and I enter a number. Instead, I get a blank page, and I have to first enter a digit. Then I get
How many dogs do you have?
So you have 3 dog(s)! (if I input 3)
I am using Eclipse + MinGW, and its my first time using C, so I'm not sure what I have/had to set up.

I managed to solve this problem. The problem was that eclipse was first processing all the scan statements, and then processing all the print statements. This would be hard to solve if your program has more than one scan statement.
The fix is somewhat simple. Download the binary fix from the following link.
You then need to paste the starter.exe in the path where you have Eclipse installed.
In my case it was this eclipse\plugins\org.eclipse.cdt.core.win32.x86_64_5.2.0.201309180223\os\win32\x86_64
Yours may very a bit. Overwrite the existing starter.exe and it should work.

printf() buffers output. In general (in UNIX) stdlib is smart enough to flush stdout before reading stdin but it might not be the case in MingW. Consider using fflush() or outputting to stderr.

Related

How can I get stdin of any sort to work with emscripten?

I have been trying to use emscripten. However, trying to get any input in node makes it puke (or ignore input entirely.)
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <emscripten.h>
int main(void){
//fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
int number;
printf("Please enter a number.\n");
scanf("%i", &number);
printf("This is a compile test for emscript. \n By the way, your number was: %i",i);
}
I have looked at this link, this link (some of that is clearly obsolete) and a number of example files that are meant to be used with --pre-js. NOTHING seems to work.
I'm starting to think I'm an idiot because a google search for "emscripten scanf" for the last year reveals nothing but less than a page of utterly useless results, so I'm assuming I'm missing something obvious.
Regardless, the only results I can get include: node hangs when you use scanf, node ignores scanf, or node lets you hit enter an infinite number of times before scanf works.
Googling "emscripten stdin" is slighty more fruitful, providing this with the "recommended" way to ..... aww, who am I kidding. (But I hope I've proved I put some real effort into this before giving up and asking.)
Thanks in advance.
(PS: When using the html version, this seems to mirror the node behavior, looping dialog box and all.)
Edit: This example sort of works in the web browser - it gives me the prompt screen and otherwise seems to work pretty normally (but the prompt loops over and over.)

inability to input sentences with white space

I'm trying to make the 20 questions game using c. The following is the code excerpt of one of the functions.
int question_input(void)
{
char q[100];
int i=0;
printf("Enter the question or say if you want to guess\n");
scanf("%[^\n]",q);
i=check_if_guess(q);
if(i==0)
{
printf("Say yes or no\n");
scanf("%s",q);
}
return i;
}
I get a general protection fault when I execute and on debugging, I found that the problem is with the
scanf("%[^\n]",q)
statement.
If the same statement is changed to "%s", then, I get no segmentation fault. Functions like gets (general protection fault) and fgets(doesn't ask for input at all) also fail to take inputs.
The thing which is more curious is that when I execute these statements in a seperate file, without the rest of the code, they execute properly.
Even if I try "%99[^\r\n]", it shows the same.
Please help
Well, I found an answer to my question by myself. One of my previous functions in my code had
scanf("%d",&n)
It is a known fact that after we enter our input we press the enter key. This is taken as a '\n' by the compiler and is stored in the buffer. My next scanf statement( the one which I got error in)(scanf("%[^\n]",q) straight away took the '\n' from the buffer. Hence it could not take any input, since \n terminates input. The compiler I use is turbo C++. In some cases of empty strings, it reports an error. Hence I got the error message. To solve this problem, I had to use
getchar()
statement before the scanf("%[^\n]",q) statement, so that the '\n' is removed from the buffer and taken as an input for the getchar statement.

Incomprehensible issue in a loop (C on Clion)

I'm learning C in my school for 2 months and I have to make a Halma's Game in the console, so I'm a newbie for now.
I use Clion and CodeBlock but I prefer Clion for his internal console.
The game work pretty good, but I've got 2 weird issues that I can't fix by myself:
- My loop is presumed to stop with the scanf,but it doesnt.
("saut" variable).
It work only in the 2nd(or more) passage in the loop.
-The variable "h" increment during the loop (in order to ignore the next condition when you are out the loop), but the program always get in.
I don't know why..
Check my code:
MY CODE (IN FRENCH DON'T WORRY ABOUT
Thanks for your help.
Robin.
On some platforms (especially Windows and Linux) you can use fflush(stdin);:
Do modify your code like :-
while(saut==1)
...
....
scanf("%d",&saut);
fflush(stdin);
}
fflush(stdin);
these is another trick to solve this issue :-
It goes into an infinite loop because scanf() will not consumed the input token if match fails. scanf() will try to match the same input again and again. you need to flush the stdin.
if (!scanf("%d", &sale.m_price)) fflush(stdin)

Appcode can't get scanf right

I'm really new to AppCode, I've started taking C classes and wanted to use our school license for AppCode.
Most of the others are using CodeLite and make gcc projects, when they build and run it they get a terminal window, is this possible for AppCode?
Also it requires me to input my scanf values first before any other code will be executed (see example below). This way if I have multiple scanf's it requires me to input them all at once which gets really hard to memorize if you have 10's of them.
I've searched a lot on the internet but couldn't find the answers I'm looking for, thanks a lot!
EDIT: Better example
puts("Give a value");
int value;
scanf("%d", &value);
Requires the scanf input before it prints out "Give a value", while in CodeLite it prints out "Give a value" first and afterwards it awaits the scanf input.

Simple C program labelled as virus

I was writing a program to input a string and output alternate characters in the string and I could compile it once, and building it made my antivirus program report that it is a virus (Gen:variant.graftor.74557).
Does any of my code causes something malicious to be called a virus
#include<stdio.h>
#include<string.h>
void altchar()
{
char a[50];
printf("Enter a string");
gets(a);
int i=0;
for(i=0;i<strlen(a);i+=2)
printf("%c",*(a+i));
}
int main()
{
altchar();
return 0;
}
Other c programs compiles very smoothly with no clashes with my AV.
Update:
My AV has no problem with gets() function, and other programs that use gets works smoothly.
Update 2:
By the way, I can run the program exactly once, then it is moved to quarantine.
And the output is nothing and the compiler tells me
Process returned 1971248979 (0x757EDF53) execution time: -0.000 s
For curious minds, I use Bitdefender Antivirus!
The "virus" detected is actually a placeholder name for the F-Secure generic trojan detector. It looks into programs for suspect behaviour. Unfortunately that kind of analysis is bound to sometimes producing false positives.
Maybe your harmless code matches some known malware behaviour on a byte code level? Try making a small change to your code and see if the problem goes away. Otherwise you can submit your program (info on the paged linked above) as a false positive to help them improve their database.
The classic methodology of an antivirus software is to take a few bytes (very well chosen bytes) from an infected file and use those as an identifier string ... it searches the executables and if the bytes match with some bytes from an executable (this check is most of the time done when opening (running) an executable, or when performing a full system scan) then it marks it as a virus.
Fix your code (as per comments), and recompile ... see what happens :)

Resources