-4

I have a problem with my code in C. All I have done is this:

#include <stdio.h>

int main()
{
    float zahlen[2];
    for (int i = 0; i < 2; i++) {
        printf("%d. Zahl", i + 1);
        scanf_s("%d", &zahlen[i]);
    }
    printf("Division: %f\n", zahlen[0]/zahlen[1]);
    printf("Produkt: %f\n", zahlen[0]*zahlen[1]);
    printf("Summe: %f\n", zahlen[0]+zahlen[1]);
    printf("Diffenrenz: %f\n", zahlen[0]-zahlen[1]);
    printf("Mittelwert: %f\n", (zahlen[0]+zahlen[1])/2);
    getchar();
    return 0;
}

Would appreciate your help. Thanks.

Serkan K.
  • 59
  • 5

2 Answers2

1

Your scanf_s() function is trying to read an integer in base 10 and store it into a float variable. Therefore when you try to enter 3.14 for the first number, scanf_s() will stop at the "." character (but leave it in the input stream). When you try to read the second decimal integer, it will enter an infinite loop waiting for a character it can consume.

Short answer: Change the %d in scanf_s() to %f.

jdc
  • 680
  • 1
  • 8
  • 11
0

You handle floats

 float zahlen[2];

You scan for integers, using "%d":

scanf_s("%d", &zahlen[i]);

Whatever problem you are referring to, this needs to be fixed.
To do so read the documentation for scanner functions and afterwards:

http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
How to read / parse input in C? The FAQ

Yunnosch
  • 26,130
  • 9
  • 42
  • 54