patterncMinor
Structure and readability of basic calculator
Viewed 0 times
readabilitystructureandcalculatorbasic
Problem
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.
``
/ Option to continue calculations /
printf("Would you like to per
I also was looking for feedback on my codes readability and how to improve if it is of a poor quality.
``
#include
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 per
Solution
Minor stuff:
I'm getting several compiler warnings. Capture the returned variable
from
Your
I'm getting several compiler warnings. Capture the returned variable
from
scanf() to check if the user actually entered a number. main.cpp:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%f", &numtwo);
^Your
if-else statement needs to be fixed.else {
numtwo = 0;
}Code Snippets
main.cpp:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%f", &numtwo);
^else {
numtwo = 0;
}Context
StackExchange Code Review Q#68695, answer score: 3
Revisions (0)
No revisions yet.