Whenever I call One scanf it works, then the second one, bypasses the whole scanf, and gives me the default value for it
how do we get around 2 scanf's?????
help me someone smart!!!!
thanks
greg
Whenever I call One scanf it works, then the second one, bypasses the whole scanf, and gives me the default value for it
how do we get around 2 scanf's?????
help me someone smart!!!!
thanks
greg
After every call to scanf(), you must flush the input buffer. Read the board's faq on how to flush the input buffer and why you must do it.
Search the board, many people asked a lot of times.
anyway some people will tell you to use fgets() and then convert of specified type.
you can see this here, it will work in a loop:
I'm not sure this is the best way, but search the board about the scanf() question, yes I know it's an infinite loop, it was just to show this will work in a loop. CTRL+C or CTRL+Z to stop the program.Code:#include <stdio.h> #include <stdlib.h> int main(void) { char buf[BUFSIZ]; int num; while(1) { printf("Enter a number: "); fgets(buf,sizeof(buf),stdin); num = atoi(buf); //converts the data printf("%d * 10 = %d\n",num,num*10); } system("PAUSE"); return 0; }
Last edited by Vber; 03-16-2003 at 09:00 AM.
Does any of this help you DrJones? I don't understand your problem, without more details or some sample code.
gg
Code:include <stdio.h> #define FLUSH_INPUT while( fgetc( stdin ) != '\n' ) main( ) { char string[256]; int n; fscanf( stdin, "%s", string ); // Read in a string FLUSH_INPUT; // Make sure the input buffer is empty fscanf( stdin, "%d", &n ); // Read in a number FLUSH_INPUT; // Make sure the input buffer is empty fprintf( stdout, "%s\n%d\n", string, n ); }
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie