2022-10-26

Double showing a lot of zeros with printf

I wanted to write a little calculator, which I have already done by using cout and cin, and I used double instead of int to not only get integers as an output.

At school, I then saw that we're going to use printf() and scanf(). To practice the new commands, I wanted to rewrite my program, but when I run my program I only see a lot of zeros after the comma as an output. Does anybody know why?

I wanted to rebuild a calculator with double instead of int to not only get integers as a result.

This is the code:

#include <stdio.h>

using namespace std;

int main(){
    printf ("Taschenrechner\n\n");
    int zahl1, zahl2;
    char rechop;
    double erg;
    
    printf ("Gib die Rechnung ein: ");
    scanf ("%d", &zahl1);
    scanf ("%c", &rechop);
    scanf ("%d", &zahl2);
    
    if (rechop == '+'){
        erg = zahl1+ zahl2;
        printf ("Ergebnis: ");
        printf ("%f", erg);
    }
    else if (rechop == '-'){
        erg = zahl1 - zahl2;
        printf ("Ergebnis: ");
        printf ("%f", erg);
    }
    else if (rechop == '*'){
        erg = zahl1 * zahl2;
        printf ("Ergebnis: ");
        printf ("%f", erg);
    }
    else if (rechop == '/'){
        erg = zahl1 / zahl2;
        printf ("Ergebnis: ");
        printf ("%f", erg);
    }
    else {
        printf ("Keine gültige Rechenoperation!");
    }
    return 0;
}


No comments:

Post a Comment