Problem with my simple calculator and closing after execution

0 Itai Sasson · October 8, 2014
#include <stdio.h>
#include <stdlib.h>
int main()
{
      char letter;
  float num1,num2;
  printf("What operation do you want to use? \n\tA)Addition\n\tB)Subtraction\n\tC)Multiplication\n\tD)Division\n\n\t");
  scanf("%c",  &letter);
 
printf("Please enter a number:   ");
  scanf("%f",  &num1);
 
printf("Please enter a second number:   ");
  scanf("%f",  &num2);
 
if (letter == 'A' || letter == 'a');
      printf("%f + %f = %f\n", num1, num2, num1 + num2);
 
else if (letter == 'B' || letter == 'b');
      printf("%f - %f = %f\n", num1,num2, num1 - num2);
  
else if (letter == 'C' || letter == 'c');
  printf("%f x %f = %f\n",  num1, num2, num1 * num2);
 
 else if (letter == 'D' || letter == 'd');
  printf("%f / %f = %f\n",  num1, num2, num1 / num2);
    return 0;
}

I have made this simple calculator for myself ( I am a beginner at coding) and it was working before, but I tried to add in some more things and it did not work so I deleted them. Now it is not building it. Please reply if you see a problem with it. (sorry if it is a simple mistake and I wasted your time)

Also can someone please tell me how to make the program not close after the execution.



Post a Reply

Replies

Oldest  Newest  Rating
+1 Горан Куга · October 12, 2014
Your code is not building because you need to remove semicolons after the if statements.
 if (letter == 'A' || letter == 'a')
printf("%f + %f = %f\n", num1, num2, num1 + num2);

else if (letter == 'B' || letter == 'b')
printf("%f - %f = %f\n", num1,num2, num1 - num2);

else if (letter == 'C' || letter == 'c')
printf("%f x %f = %f\n", num1, num2, num1 * num2);

else if (letter == 'D' || letter == 'd')
printf("%f / %f = %f\n", num1, num2, num1 / num2);

And your program is closing after execution because you don't have line system("pause"); in the end of your code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char letter;
float num1,num2;
printf("What operation do you want to use? \n\tA)Addition\n\tB)Subtraction\n\tC)Multiplication\n\tD)Division\n\n\t");
scanf("%c", &letter);

printf("Please enter a number: ");
scanf("%f", &num1);
printf("Please enter a second number: ");
scanf("%f", &num2);

if (letter == 'A' || letter == 'a')
printf("%f + %f = %f\n", num1, num2, num1 + num2);
else if (letter == 'B' || letter == 'b')
printf("%f - %f = %f\n", num1,num2, num1 - num2);
else if (letter == 'C' || letter == 'c')
printf("%f x %f = %f\n", num1, num2, num1 * num2);
else if (letter == 'D' || letter == 'd')
printf("%f / %f = %f\n", num1, num2, num1 / num2);

system("pause");
return 0;
}

And i also want to share my own version of calculator. I also provide same code with comments  http://pastebin.com/rqgZNiT3.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

/*
Math calculator by Goran
*/

int main()
{
    char answer[15];
    float firstNumber, secondNumber;

    printf("What operation do you want to use ?\n- Addition\n- Subtraction\n- Multiplication\n- Division\n\n");
    scanf(" %s", answer);
    answer[0] = toupper(answer[0]);

    if(!(answer[0] == 'A' || answer[0] == 'S' || answer[0] == 'M' || answer[0] == 'D')){
        printf("\n");
    }

    while(!(answer[0] == 'A' || answer[0] == 'S' || answer[0] == 'M' || answer[0] == 'D')){
        printf("You need to enter correct operation: ");
        scanf(" %s", answer);
        answer[0] = toupper(answer[0]);
    }

    switch(answer[0]){
        case 'A':
            printf("\nEnter first number: ");
            scanf(" %f", &firstNumber);
            printf("Enter second number: ");
            scanf(" %f", &secondNumber);
            printf("Result: %.2f + %.2f = %.2f \n", firstNumber, secondNumber, firstNumber + secondNumber);
            break;
        case 'S':
            printf("\nEnter first number: ");
            scanf(" %f", &firstNumber);
            printf("Enter second number: ");
            scanf(" %f", &secondNumber);
            printf("Result: %.2f - %.2f = %.2f \n", firstNumber, secondNumber, firstNumber - secondNumber);
            break;
        case 'M':
            printf("\nEnter first number: ");
            scanf(" %f", &firstNumber);
            printf("Enter second number: ");
            scanf(" %f", &secondNumber);
            printf("Result: %.2f * %.2f = %.2f \n", firstNumber, secondNumber, firstNumber * secondNumber);
            break;
        case 'D':
            printf("\nEnter first number: ");
            scanf(" %f", &firstNumber);
            printf("Enter second number: ");
            scanf(" %f", &secondNumber);
            printf("Result: %.2f / %.2f = %.2f \n", firstNumber, secondNumber, firstNumber / secondNumber);
    }

system("pause");
return 0;
}
+1 xander dominic · August 19, 2015
//Try this one//


#include
#include

int main()
{
printf("CALCULATOR\n\n");
puts("INSTRUCTION:  Just like any other calculator, you can calculate numbers as much as you like but remember to press space everytime you calculate as in 2 + 2.");
printf("\n\n");




    int l = 1;
    int n = 2;

// WHILE LOOP keeps the calculator program running//

 while (l < n){

    int a = 1;
    int b = 2;

    char o;
    float num1,num2;
    printf("\n");
    scanf("%f",&num1); scanf("%s",&o); scanf("%f",&num2);
    switch(o) {
        case '+':
            printf("\n = %g\n",num1+num2);
            break;
        case '-':
            printf("\n = %g\n",num1-num2);
            break;
        case '*':
            printf("\n = %g\n",num1*num2);
            break;
        case '/':
            printf("\n = %g\n",num1/num2);
            break;
        default:
            /* If operator is other than +, -, * or /, error message is shown */
            printf("Error! operator is not correct\n\n");

            break;
    }
 }
    return 0;
}


0 Itai Sasson · October 9, 2014
Thank you for helping but I am still getting this error: expected '=', ',', ';', 'asm' or '_attribute_' before '{' token.

I really appreciate you guys trying to help out!
0 Itai Sasson · October 10, 2014
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
  • 1

C

107,091 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator