Hello,
I need help with code...
I need to write "ERROR" and return 1 if the user enters nothing.
I tried many things so i think there is a problem with scanf.
All code with Polish words:
#include <stdio.h> #include <stdlib.h> #include <time.h> int sum(const float* tab, unsigned int size, float *result); int avg(const float* tab, unsigned int size, float *result); int main() { float ile; float a[10]; float *tab=a; float wynik; float *result=&wynik; int i=0; float podana; int size = 0; int k=0; for(i=0;i<10;i++) { printf("Podaj %d liczbe: ",i+1); scanf("%f",&podana); if(podana==0) { // KOMUNIKAT WYÅšTWIETLONY PRZEZ PROGRAM JEST NIEWÅAÅšCIWY; POWINNO BYĆ ERROR printf("Zle dane\n"); return 0; } else { *(tab+i)=podana; size++; k++; } } printf("\nWielkosc tablicy: %d\n",size); for(i=0;i<10;i++) { printf("%f ",* (tab+i)) ; } sum(tab, size, result); avg(tab, size, result); return 0; } int sum(const float* tab, unsigned int size, float *result) { int i=0; float ile; for(i=0;i<10;i++) { ile+=*(tab+i); } *result= ile; printf("\nSuma wynosi: %.2f", *result); return 0; } int avg(const float* tab, unsigned int size, float *result) { int i=0; float ile=0; for(i=0;i<10;i++) { ile+=*(tab+i); } *result= ile/size; printf("\nSrednia wynosi: %.2f", *result); return 0; }
The code to change:
for(i=0;i<10;i++) { printf("input a number"); scanf("%f",&number); //There should be an if scanf is enter it return 1. if(number==0) { printf("Bad input\n"); return 0; } }