-2
#include <stdio.h>
#include <limits.h>

int main() {

    enum loop {NO ,YES};
    enum loop okloop = YES;
    int i=0;

    char s[8];
    int lim=6;
    char c;

    while (okloop==YES)
    {
        if (i>=lim-1)
            okloop=NO;
        else if ((c=getchar())!='\n')
            okloop=NO;
        else if (c==EOF)
            okloop=NO;
        else {
            s[i]=c;
            ++i;
        }
    }
    for (i=0;i<5;++i)
        printf("this is the character %c\n",s[i]);
    return 0;
}

I am just inputing a character from the keyboard and stored it in an array; and then I have to print the character stored in the array to screen, but the output is not as I have expected.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
shubhamm
  • 15
  • 3

3 Answers3

3

As per my understanding, you need to change

else if ((c=getchar())!='\n')

to

else if ((c=getchar()) == '\n')

Otherwise, you'll end up storing nothing valid in s[i].

Also, as a sidenote

  1. Always initialize the local variables.
  2. add a return 0; in your main().
  3. think of null terminating s[i] after reading inputs, just in case you want to use it as a string later.

Good practice.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

Why can't you just do:

while((c = getchar()) != '\n' && i<lim-1)
    s[i++]=c;

Your code unnecessarily uses lot of variables which can be avoided by the above loop.


[addition by alk]

A more readable version:

size_t i = 0;
int c = EOF;

...

while (
  ('\n' != (c = getchar())) && 
  (i < lim)
)
{
  s[i] = c;
  ++i;
}

Just tiny changes make things better readable, easier to understand, less error prone, more stable.

A less elegant but even more obvious solution:

size_t i = 0;

...

while (i < lim)
{
  int c = getchar();
  if ('\n' == c)
  {
    break;
  }

  s[i] = c;
  ++i;
}
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • 2
    Although valid, your code "*unnecessarily*" obsfucate things. The most condensed solution does not need to be the "best". – alk Jan 10 '15 at 10:23
  • You mind me touching your answer? – alk Jan 10 '15 at 10:26
  • Please see my updates to your answer .. ;-) – alk Jan 10 '15 at 10:34
  • With my last proposal there also is the nice side effect that the variable `c`, which is the temporary "read-buffer", is only visible during reading, that is within the loop. Doing so follows the pattern "Do only define what you need." – alk Jan 10 '15 at 10:39
  • I do not see any unnecessary checks. There ought to be even more checks: for example testing for `EOF` is fully missing in your answer. – alk Jan 10 '15 at 10:56
  • 1
    Regarding my usage of "*obsfucated*" in this context: http://www.ioccc.org/ – alk Jan 10 '15 at 11:17
0

This is all that you need to do:

#include <stdio.h>

int main()
{
  int i=0, j;
  char s[8];
  char c;

  while (i < 8)
  {
    c = getchar();
    if (c == EOF)
      break;
    else if (c != '\n')
      s[i++]=c;
  }

  for (j = 0; j < i; ++j)
    printf("this is the character %c\n",s[j]);

  return 0;
}

Loop through for user input (8 characters max). When EOF (Ctrl + D) is entered by user, the while loop will exit and whatever characters entered till that point will be printed as output. If \n is given as input, it will be ignored / not saved in the array.

Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27