#include <stdio.h>
int getUserInput();
int main()
{
char cOperator,
cCarryOn,
cAgain;
int dFirst,
dSecond,
dResult;
// printf("first is %d and second is %d", first, second);
printf("Enter first number");
scanf(" %d", &dFirst);
printf("Enter second number");
scanf(" %d", &dSecond);
printf("First number is %d and second number is %d\n", dFirst, dSecond);
printf("Is this what you want? (y/n)");
scanf(" %c", &cCarryOn);
while (cCarryOn != 'y' && cCarryOn != 'n')
{
printf("Type either 'y' or 'n' (without quotation marks)");
scanf(" %c", &cCarryOn);
}
if (cCarryOn == 'n')
{
printf("Program will now start again.\n");
main();
}
else
{
printf("OK, let's continue.\n");
}
printf("What operator? +, -, * or /?");
scanf(" %c", &cOperator);
while (cOperator != '+' &&
cOperator != '-' &&
cOperator != '*' &&
cOperator != '/')
{
printf("Try again");
scanf(" %c", &cOperator);
}
switch (cOperator)
{
case '+':
dResult = dFirst + dSecond;
break;
case '-':
dResult = dFirst - dSecond;
break;
case '*':
dResult = dFirst * dSecond;
break;
case '/':
dResult = dFirst / dSecond;
break;
default:
printf("Ah... Something wrong happened. Program will now restart");
main();
break;
}
printf("The final answer is %d\n", dResult);
printf("Perform another calculation? (y/n)");
scanf(" %c", &cAgain);
while (cAgain != 'y' && cAgain != 'n')
{
printf("Type 'y' or 'n' (without quotation marks)");
scanf(" %c", &cAgain);
}
if (cAgain == 'y')
{
main();
}
else
{
printf("Thank you for using this calculator. Bye!");
}
return 0;
}
Output (Example):
WHAT I NEED TO WORK ON:
- There seems to be a bug with stops the program from exiting after the user types 'n' when asked if (s)he wants to perform another calculation - see the red lines in the output.
- Clean up code, make it more consistent.
- Change the input values from type int to type double or float.
- Add more functionality: make the user be able to input more than two numbers, add square-root and other mathematical functions, etc.
- scanf() sometimes requires you to add a space before the "%-" bit in the quotation marks, otherwise the program sometimes goes loopy. I honestly don't know why!
No comments:
Post a Comment