Ignoring any characters and numbers after empty line in C

61 Views Asked by At

I've been coding in C and I have a slight problem. The problem is that i need to ignore all the chars and numbers when the input is just an empty line. I can keep on adding inputs, but after empty line they should be ignored and not be written into the array. What should I change? Btw, it's a program for Horner's scheme.

#include <stdio.h>

int main() {
    float pole[50];
    int opakovania = 0;
    float cislo = 0;
    float skenovanie;
    int zla_hodnota = 0;
    int zly_index = -1;

    while (opakovania < 50) {
        skenovanie = scanf("%f", &cislo);

        if (skenovanie == 1) {
            pole[opakovania] = cislo;
            opakovania++;
        } else if (skenovanie == EOF) {
            if (zla_hodnota) {
                if (zly_index == 0) {
                    printf("Nepodarilo sa nacitat zaklad x\n");
                } else {
                    printf("Nepodarilo sa nacitat polynom na %d mieste.\n", zly_index);
                }
                return 0;
            } else {
                break;
            }
        } else {
            zla_hodnota = 1;
            zly_index = opakovania;
            while (getchar() != '\n');
        }
    }

    if (opakovania == 0) {
        printf("Nepodarilo sa nacitat zaklad x.\n");
        return 0;
    }

    float vysledok = pole[1];
    for (int i = 2; i < opakovania; ++i) {
        vysledok = vysledok * pole[0] + pole[i];
    }

    printf("Vysledok je: %.2f\n", vysledok);

    return 0;
}

I expect my code to ignore all the characters and numbers when there is an empty line as an input.

0

There are 0 best solutions below