I created this code to make a basic calculator after viewing a few examples. I added the function to continue the calculations myself. I am wondering if there is a way to make this more efficient as I feel I've used some unnecessary code but I can't make it work without.
I also was looking for feedback on my codes readability and how to improve if it is of a poor quality.
#include <stdio.h>
int main ()
{
int cancel = 1;
char o, end = 'Y';
float numone = 0.0, numtwo = 0.0;
/* Explaining structure and receiving inputs */
printf("Please enter the two numbers you wish to manipulate!\n");
scanf("%f", &numone);
do {
scanf("%f", &numtwo);
printf("Please enter the operation you wish to perform (+, -, *, /)\n");
scanf(" %c", &o);
/* Checking for correct input and manipulating numbers with regards to user input */
switch(o){
case '+':
printf("%.1f + %.1f = %.1f\n", numone, numtwo, numone + numtwo);
break;
case '-':
printf("%.1f - %.1f = %.1f\n", numone, numtwo, numone - numtwo);
break;
case '*':
printf("%.1f * %.1f = %.1f\n", numone, numtwo, numone * numtwo);
break;
case '/':
printf("%.1f / %.1f = %.1f\n", numone, numtwo, numone / numtwo);
break;
default:
/* Error message for incorrect input */
printf("Error! Incorrect input!\n");
break;
}`enter code here`
/* Option to continue calculations */
printf("Would you like to perform a further calculation? (Y/N)\n");
/* Checking for answer and terminating if N, resuming if Y */
scanf(" %c%", &end);
if (end == 'N') {
return 0;
}
else {}
numtwo = 0;
/* Requesting new number to use as numtwo for calculations */
printf("Current number = %f please input a second number!\n", numone);
} while (end == 'Y');
return 0;
}
-Wallwhen compiling, e.g.gcc -Wall foo.c -o foo. Try and get into the habit of always enabling warnings - it will save you a lot of grief in the long run. \$\endgroup\$