2016-04-10 21 views
-3

Ich habe gerade einen einfachen Taschenrechner mit C. erstellt Jetzt kann ich nur durch meinen Code Operationen 1 zu einer Zeit berechnen. Also welchen Code oder Schleife sollte ich verwenden, um meine Berechnung zu wiederholen oder eine andere zu tun, ohne mein Programm neu aufbauen und ausführen zu müssen. auch hier ist mein Code:Sehr einfache C-Funktion

`

#include <stdio.h> 
#include <stdlib.h> 

int main(void) 
{ 

    float first_value; 
    float second_value; 
    char Operation; 


    printf("Welcome to Shoeb's first calculator :)\nChoose your operation (a for Addition/s for Subtraction/m for Multiplication/d for Division) \nPress small o when done calculation ^_^\n"); 

    scanf("%s", &Operation); 


    if (Operation == 'a') { 

     printf("Type in your first number below.\n"); 
     scanf("%f", &first_value); 

     printf("Type in you second value below.\n"); 
     scanf("%f", &second_value); 

     printf("The result is %f", first_value+second_value); 

    } 



    if (Operation == 's') { 

     printf("Type in your first number below.\n"); 
     scanf("%f", &first_value); 

     printf("Type in you second value below.\n"); 
     scanf("%f", &second_value); 

     printf("The result is %f", first_value-second_value); 

    } 

    if (Operation == 'm') { 

     printf("Type in your first number below.\n"); 
     scanf("%f", &first_value); 

     printf("Type in you second value below.\n"); 
     scanf("%f", &second_value); 

     printf("The result is %f", first_value*second_value); 

    } 

    if (Operation == 'd') { 

     printf("Type in your first number below.\n"); 
     scanf("%f", &first_value); 

     printf("Type in you second value below.\n"); 
     scanf("%f", &second_value); 

     printf("The result is %f\n", first_value/second_value); 

    } 
     return 0; 
} 

`

+1

'scanf ("% s", & Betrieb);' -> 'scanf ("% c", & Betrieb);' und 'verwenden do..while' –

Antwort

1

Rename 'int main (void)' auf 'int Rechner (void)'. Erstellen Sie eine neue Haupt:

int main(void){ 
    while(1) calculator(); 
}; 
+0

Oh - und repariere den UB Cockup gepostet von Sourav :( –

+0

hat gut funktioniert bro vielen dank :) –

Verwandte Themen