0

i need help with short Code in C. I must read floats on input line seperated with space and input is ended with float 0 or EOF.

How to do this if i dont know how many numbers or in input, or how it works and ask to EOF if i am reading just numbers and not chars?

Thanks for any response.

example of input in one line:

12 11 10 45 50 12 EOF
12 10 11 45 0 

int main(void)  
{
    float num;
    float sum = 0;

    do{
       scanf("%f", num);
       sum += num;
    } while(EOF || num == 0);
    return 0;
}
user2899587
  • 189
  • 2
  • 3
  • 11

6 Answers6

1

From the man page of scanf -

scanf returns the number of items successfully matched and assigned which can be fewer than provided for, or even zero in the event of an early matching failure. The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs.

This means that scanf will return EOF only when it encounters EOF as the first input when it is called because EOF must be preceded with a newline '\n' else it won't work (depending on the OS). You must also account for the matching failure scanf may encounter.

#include <stdio.h>

int main(void) {
    float num;
    float sum = 0;
    int val;

    while((val = scanf("%f", &num)) != EOF && val == 1) {
        sum += num;
    }

    if(val == 0) {
        printf("matching failure. input is not a float.\n");
    }
    else {
        printf("end of input.\n");
    }

    return 0;
}
ajay
  • 9,402
  • 8
  • 44
  • 71
0

From scanf reference:

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.

So, you may rewrite your do-while loop to something like

int retval;
while((retval = scanf("%f", &num)) != EOF && retval > 0 && num != 0) {
    sum += num;
}

if(retval == 0) {
    printf("input read error.\n");
}

to match your constraints.

Also note you need to prefix your variable with & when passing it to scanf(), since the function expects a pointer to deal with (you need to pass variable address).

EDIT:

see this topic concerning EOF problems in Windows

Community
  • 1
  • 1
Mauren
  • 1,955
  • 2
  • 18
  • 28
0

You can re write your code like this

         int main(void) 
         {
            float num;
            float sum = 0;
            do
            {
               scanf("%f", &num);
               sum += num;
            } while((!feof(stdin)) && (num != 0));

            printf("%f", sum);
            return 0;   

          }

Here feof indicates end of input stream.

Dipika
  • 584
  • 2
  • 12
  • This will give the wrong answer if the `scanf` conversion fails. The value in `num` will be the last successful value, and get added into the sum twice. – Floris Mar 12 '14 at 19:10
0

The following may be a slightly more robust way to do this:

#include <stdio.h>
#include <string.h>

int main(void) {
  int sum=0;
  int num;
  char *p;
  char buf[1000];
  fgets(buf, 1000, stdin);
  p = strtok(buf," ");
  while(p!=NULL) {
    if(sscanf(p, "%d", &num) == 1) sum+=num;
    p = strtok(NULL, " ");
  }
  printf("the sum is %d\n", sum);
}

Test:

> testme 
1 2 3 4 0

the sum is 10

> testme 
1 2 3 4 ^D
the sum is 10

Note - you have to enter ctrl-D twice to get the desired effect when you are at the end of a line.

Floris
  • 45,857
  • 6
  • 70
  • 122
0

you can get your doubt clear by reading "C programming a modern approach by K N King"

This book provides proper clarification on this topic

Sahil
  • 1
  • 1
0

Test the result of scanf() for 0, 1 or EOF.
Test the value scanned for 0.0.

int main(void) {
  float num;
  float sum = 0;
  int cnt;

  while ((cnt = scanf("%f", &num)) == 1) {
    if (num == 0.0) break;
    sum += num;
  }

  // cnt should be EOF, 0 or 1      

  if (cnt == 0) {
    printf("Input is not a number\n");
  }
  else {
    printf("Sum %f\n", sum);
  } 
  return 0;
}

Although, in general, scanf() returns values EOF, 0, 1, ... "number of format specifiers", a value of 0 occurs rarely. Example input is "+".

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256