0

I am having a problem scanning chars into an array. Every time I do it will skip the next scan and go to the next. I know what is happening because the input also adds '\n' to the input but I do not know how to remedy the cause of it. Here is some sample code:

char charray [MAX], ffs;
int inarray [MAX], i;


for (i = 0; i < MAX; i++)
{
    charray[i] = getchar();
    printf ("%c\n",charray[i]);
    scanf ("%d", &inarray[i]);
    printf ("%d\n",inarray[i]);
}
user972450
  • 27
  • 1
  • 5
  • Take a look at http://stackoverflow.com/questions/844847/peek-at-input-buffer-and-flush-extra-characters-in-c – Jan S Sep 30 '11 at 09:11

3 Answers3

0

You can do like this.

while((c = getchar()) != '\n')
{
    putchar(c);
}

this may solve your problem. or you can go till EOF also.

pmg
  • 106,608
  • 13
  • 126
  • 198
Chaithra
  • 1,130
  • 3
  • 14
  • 22
0

You are reading from the standard input with 2 functions: getchar() and scanf(). You need to understand how they work.

getchar() is easy: it returns the next available character in the input stream (or waits for one or returns EOF)

scanf("%d", ...) is more complex: first, it optionally discards whitespace (spaces, enters, tabs, ...), then it reads as many characters as possible to represent an integer, and stops at the first character that can't be used for integers, like a '\n'.

As you have them in a loop, your getchar() call will get the character that stopped the scanf() and the next scanf() will procedd from there.

If your input is something like "q1w22e333r4444" (with MAX == 4), your program will work.

If your input is something like

q 1
w 22
e 333
r 4444

after the first time through the loop (where charray[0] gets 'q' and inarray[0] gets 1), the getchar() will get '\n' leaving the 'w' "ready" for scanf, which of course fails ... and is then "caught" by the next getchar(); and the "22" gets assigned in the 3rd time through the loop (to inarray[2]).

So, you need to review your code.

Also, scanf() returns a value. Use that value

if (scanf("%d", &inarray[i]) != 1) /* error */;
pmg
  • 106,608
  • 13
  • 126
  • 198
-1

You should actually scan a string into the array directly, rather than characters using scanf("%s",&charray);

However your code will work if you add a while(getchar() != '\n' ); statement. This will get all characters till the '\n'.

charray[i] = getchar();
do{
    c = getchar();
}while(c != '\n' && c!= EOF);
printf ("%c\n",charray[i]);
scanf ("%d", &inarray[i]);
do{
    c = getchar();
}while(c != '\n' && c!= EOF);    
printf ("%d\n",inarray[i]);
Jan S
  • 1,831
  • 15
  • 21
  • 1
    -1 for recommending a undefined behaviour construct. `fflush` is not defined for input streams (or input/output streams where the last operation was a read): see 7.19.5.2 in the [C99 Standard](http://www.open-std.org/JTC1/sc22/wg14/www/docs/n1256.pdf). – pmg Sep 30 '11 at 08:47
  • @pmg I was wondering how an fflush() could be used for input streams, but it seemed to work. I was not aware of the standard though. Read up a bit more on it, and have edited my answer. It should work fine now. Thank you for your input :) – Jan S Sep 30 '11 at 09:06
  • -1 revoked, but I'll leave the comment above. The fact that `fflush(stdin)` works in your implementation does not make it any less Undefined Behaviour (Windows specifically describes the behaviour of `fflush(stdin)` -- with a barely visible note about it being an extension -- but relying on that is locking yourself to the Windows implementation, see [MSDN Refernce](http://msdn.microsoft.com/en-us/library/9yky46tz(v=vs.71).aspx)) – pmg Sep 30 '11 at 09:13
  • @pmg Yes that's fine. From what i've understood from the documentation, `fflush(stdin)` is reliable only in Microsoft's C. Is that right? Or is it unreliable even in MS's C? – Jan S Sep 30 '11 at 09:21
  • It is reliable with Microsoft C (or, more accurate, with Microsoft libraries, eg: `gcc` on Windows, using the system libraries, will work the same as Microsoft compiler). But if your code is ever going to get compiled in other implementations all bets are off. Add a `#ifndef _WIN32` / `#error needs Windows for flush(stdin)` / `#endif` – pmg Sep 30 '11 at 09:26