I'm trying to use scansets to filter out out some textfiles.

#include <stdio.h>
int main( void)
{
   int i;
   char str[ 80], str2[ 80];
   scanf("%d %[abcdefg] %s", &i, str, str2);
   printf("%d %s %s", i, str, str2);
   getchar();
   return 0;
}

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?

Hi VinC,

#include <stdio.h>
int main( void)
{
int i;
char str[ 80], str2[ 80];
scanf("%d %[abcdefg] %s", &i, str, str2);
printf("%d %s %s\n", i, str, str2);
system("pause");
return 0;
}

Use

system("pause");

instead.

Code is fine and it works as expected in my compiler(gcc 3.4.5).
Try using some other input mechanism which doesn't leave stuff back in the input stream.

Ok that's weird. Using system("PAUSE") gives me the correct output but why is this? It seems to me that they should be equivalent in this situation but apparently I have a misunderstanding.

I've been taught to use getchar() over system("PAUSE)http://www.gidnetwork.com/b-61.html

I'm using Dev-C++

Did it manage to work with getchar() or system("PAUSE")?

Hi VinC,

system("pause");

Use instead.

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.

fgets(tmpstr, 80, stdin);
    sscanf(tmpstr,"%d %[abcdefg] %s", &i, str, str2);

[edit]Since you already learned this from GID, you could check out the scanf() series there. [/edit]

commented: Rightly Said - [Grunt] +1

Thank you for all the help!

> 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.

char buff[BUFSIZ];
if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
  if ( sscanf( buff, "%d %[abcdefg] %s", &i, str, str2) == 3 ) {
    printf("%d %s %s\n", i, str, str2); 
  } else {
    // some error
  }
}

Since you already learned this from GID, you could check out the scanf() series there.

Will do. I couldn't help but notice that the author of the articles on GID is also WaltP! :eek:

Hi all,
I've just suggested the easiest and the worst way to use when you have windows running.
Simply,i don't recommend it for your commercial&/ important projects.

Good luck.

Stupid question. I've noticed that most of the posts seem to sink to the very bottom of the page. Is it possible to reply to a specific post without this happening?

Stupid question. I've noticed that most of the posts seem to sink to the very bottom of the page. Is it possible to reply to a specific post without this happening?

This is a stack, so no. All you have to do to respond to a specific post and still be understood is use "Reply with Quote" button.

Although you can change the Display modes at the top of the page, the default is Linear mode, but you can change to Threaded or Hybrid mode where the posts will appear in a different order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.