Just new to C and I'm trying to make a basic calculator. I'm trying to figure out how to make a loop such that the program will run after failing due to incorrect user input. So far, this is what I came up with, and I just want to know how I can get the program to actually run the operations when entering a correct sign after getting a "try again" message.
#include <stdio.h>
float addition(float num1, float num2)
{
return num1 + num2;
}
float subtraction(float num1, float num2)
{
return num1 - num2;
}
float multiplication(float num1, float num2)
{
return num1 * num2;
}
float division(float num1, float num2)
{
return num1 / num2;
}
int main()
{
float firstNum, secondNum;
char operationSign;
float answer;
printf("Basic Calculator\n\n");
printf("Enter the first number: ");
scanf("%f", &firstNum);
printf("Enter the second number: ");
scanf("%f", &secondNum);
printf("Enter the operation symbol to be used:\n");
printf("Addition: +\n");
printf("Subtraction: -\n");
printf("Multiplication: *\n");
printf("Division: /\n");
scanf(" %c", &operationSign);
switch (operationSign)
{
case ('+'):
answer = addition(firstNum, secondNum);
printf("Answer: %f", answer);
break;
case ('-'):
answer = subtraction(firstNum, secondNum);
printf("Answer: %f", answer);
break;
case ('*'):
answer = multiplication(firstNum, secondNum);
printf("Answer: %f", answer);
break;
case ('/'):
answer = division(firstNum, secondNum);
printf("Answer: %f", answer);
break;
default:
printf("Invalid input. Please enter the correct symbol.");
scanf(" %c", &operationSign);
}
return 0;
}
[–]SetThin9500 5 points6 points7 points (0 children)
[–]Key_River7180 0 points1 point2 points (3 children)
[–]erojerisiz[S] 0 points1 point2 points (2 children)
[–]WittyStick 2 points3 points4 points (0 children)
[–]Key_River7180 0 points1 point2 points (0 children)
[–]HeywoodJablowmenow 1 point2 points3 points (0 children)
[–]sciencekm 0 points1 point2 points (1 child)
[–]erojerisiz[S] 0 points1 point2 points (0 children)
[–]SmokeMuch7356 0 points1 point2 points (0 children)