// c program to add 10 elements entered by the user
            #include <stdio.h>  
            int main ()
            {   
              int i;    
              float elements[10];       //declaring the array
              float sum = 0;

              for (i = 0; i < 10; ++i)
                {
                  printf ("Enter element %d: ", i + 1); //getting the elements from the user
                  scanf ("%f", &elements[i]);   //storing the elements
                  sum += elements[i];   //adding integers entered by the user to the sum variable

                }

              printf ("sum of above entered elements is : %.4f", sum);  //printing the sum to four decimal places
              return 0;
            }

Dani AI

Generated

A reliable approach is to read each input line as a string (fgets) and parse it with strtod/strtof. That lets you: (1) accept signed/decimal/exponent formats, (2) detect non-numeric trailing characters, and (3) reprompt cleanly when the input is invalid. This avoids the common traps seen in the thread: using isdigit on a whole token (it only checks single digit characters) and mixing scanf with string buffers (which can leave input behind or use the wrong types). tried isdigit and a char buffer; asked for the expected behavior — the example below reads exactly 10 valid floats and sums them, reprompting on bad input.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

int main(void) {
    const int N = 10;
    char line[128];
    int got = 0;
    double sum = 0.0;

    while (got < N) {
        printf("Enter element %d: ", got + 1);
        if (!fgets(line, sizeof line, stdin)) break;

        /* skip leading whitespace */
        char *p = line;
        while (isspace((unsigned char)*p)) p++;
        if (*p == '\0' || *p == '\n') {
            printf("empty input, try again\n");
            continue;
        }

        errno = 0;
        char *end;
        double v = strtod(p, &end);
        if (end == p || errno == ERANGE) {
            printf("invalid or out-of-range number, try again\n");
            continue;
        }

        /* ensure only whitespace follows the number */
        while (isspace((unsigned char)*end)) end++;
        if (*end != '\0' && *end != '\n') {
            printf("unexpected characters after number, try again\n");
            continue;
        }

        sum += v;
        got++;
    }

    printf("sum of entered elements is : %.4f\n", sum);
    return 0;
}

Notes: use strtof if you want single-precision storage; check errno==ERANGE to catch range errors; prefer the fgets+strtod pattern whenever you need robust validation and clean error recovery.

Recommended Answers

All 4 Replies

kindly help because i have tried and tried but i could not get the exact output i wanted

Line 12 seems to input floats. But your reply now writes you can not get the exact output you wanted but didn't share what happened versus what you wanted.

     **   I tried to use *isdigit* but i could not get the the exact output i wanted **





         #include <stdio.h>
            #include<stdlib.h>
            #include<string.h>
            #include<time.h>
            #include<ctype.h>
            int main ()
            {   
              int i;    
              char elements[10];        //declaring the array
              float element;
              float sum = 0;

              for (i = 0; i < 10; ++i)
                {
                  printf ("Enter element %d: ", i + 1); //getting the elements from the user 
                  //fgets(elements[i], 10, stdin);
                  scanf ("%s", &(elements[i])); //storing the elements 

                  if (isdigit(elements[i]))
                 { 
                    element=atof(elements[i]);
                     sum += element;    //adding elements  entered by the user to the sum variable  
                 }
                else
                 {printf("invalid input()\n");
                     break;
                 }
                }

              printf ("sum of above entered elements is : %.4f", sum);  //printing the sum to four decimal places
              return 0;
            }

First you appear to have mark this solved.

Second, you mention "output" a second time yet you neglect to share what the output was and share what you wanted the output to be.

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.