I'm trying to use scansets to filter out out some textfiles.
int main( void)
char str[ 80], str2[ 80];
scanf("%d %[abcdefg] %s", &i, str, str2);
printf("%d %s %s", i, str, str2);
When I input "123abcdtye" I expect the output to be "123 abcd tye". Instead my program exits as if it reaches the return statement even though I have put in a getchar() to pause the program. What is going on?
No, don't. Learn your tools. Using scanf() to read characters is even worse than using gets() because not only does it allow you to blow your buffer boundaries but as you can see it also leaves your input stream dirty. The \n is left in the stream for your getchar() to read.
Use a combination of fgets() and sscanf() for safety and to process the input stream cleanly.
> gives me the correct output but why is this?
Because scanf() is full of wonderful surprises - the most usual being that it leaves the \n on the input stream - just ready for the getchar() to return with immediate success.
I'd suggest you ALWAYS use fgets() to read a line of input, then use sscanf() (or something else) to extract information from the buffer.
fgets() leaves the input stream in a far more consistent state than scanf() ever can.
Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"