1

I need to write a program in c that gets integers as input from the user.

input example:

10 20 50 70

The user presses Enter and then the input is over.
I can't think of a condition to make it happen. I tried to write:

int grades[1000];
int i=0;

while(scanf("%d", &grades[i])!=EOF)
{
     i++;
}

It is not working.

5
  • 4
    perhaps read the documentation of scanf() instead of making assumptions about its return value. Also, do NOT use scanf(). Use fgets() along with strtol(). Commented Jan 6, 2015 at 18:14
  • No. This won't work. scanf() is going to block and wait for more numbers to come. Use fgets() instead to read the whole line, and then sscanf() to extract each number. Commented Jan 6, 2015 at 18:15
  • Explain " not working." Commented Jan 6, 2015 at 18:31
  • What should happen if "10 20 xyz 70" is entered? Commented Jan 6, 2015 at 18:39
  • we have some restricting rules so we can't use most of the c functions. I found a solution int here, someone wrote a scanf using bitwise operations. did'nt fully understand that but it's short and works Commented Jan 7, 2015 at 17:39

2 Answers 2

2

Reading a line of user input and then parsing is really the best approach as with @The Paramagnetic Croissant

If code can not pre-define an input buffer size or must parse the line while it comes in then using scanf("%d",... is OK. Non-elegant code occurs with finding the '\n'.

#define N 1000
int grades[N];
int i=0;

for (i=0; i<N; i++) {
  // Consume leading white-space, but not \n
  int ch;
  while ((ch == fgetc(stdin)) != '\n' && isspace(ch));

  // normal exit
  if (ch == '\n' || ch == EOF) break;

  ungetc(ch, stdin);
  if (1 != scanf("%d", &grades[i])) {
    // Non-numeric data
    break;
  }
  i++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried something along these lines but am totally unskilled with scanf() due to a life(CPU)long aversion. Marked up.
@Weather Vane I feel your pain. Although code can do this all with scanf(), it is ugly. Even here, code uses fgetc()/ungetc() for the finding \nemo part.
1

If you need read an entire line, then read an entire line, simple as that. If you google "C read line", you will most probably end up reading the documentation of fgets(). Then you google "C convert string to integer", and you perceive that there exists a function called strtol() in the C standard library. Armed with these two weapons, and applying some logic, you can deduce something like this:

const size_t max_numbers = 1000; // however many

int numbers[max_numbers];
size_t index = 0;

char buf[LINE_MAX];
while (index < max_numbers && fgets(buf, sizeof buf, stdin)) {
    char *p = buf;
    char *end;
    while (index < max_numbers && *p && *p != '\n') {
        numbers[index++] = strtol(p, &end, 10);
        p = end;
    }
}

2 Comments

After numbers[..., suggest adding some error tests like if (p == end) break; to deal with non-numeric input.
@chux right. Also, one must reset and check errno, so using strtol() absolutely correctly is a bit ugly; still better than scanf() & co., though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.