I know I'm probably writing questions along the same thing every time, but there is still stuff I don't get.
Code:
#include<stdio.h>
int main()
{
int num1, num2, sum, count; /*Assign int variables of num1, num2, sum, count*/
count = 0;
char dummy, operation; /*Assign char variables of dummy, operation*/
printf("Enter first number:"); /*Ask user for 1st number*/
scanf("%d", &num1); /*Read 1st number*/
printf("Enter operation (+ or - or * or /):"); /*Ask user for operation*/
scanf("%c%c%c", &dummy, &operation, &dummy); /*Read operation*/
printf("Enter second number:"); /*Ask user for 2nd number*/
scanf("%d", &num2); /*Read 2nd number*/
while (count <= 2); /*While count is smaller than or equal to 2 program loops*/
{
if ( operation == '+' ) /*If condition if operation is +*/
{
sum = num1 + num2; /*Sum if addition*/
printf("The sum of %d and %d is %d" ,num1, num2, sum); /*Print answer to screen*/
}
if ( operation == '-' ) /*If condition if operation is -*/
{
sum = num1 - num2; /*Sum if subtraction*/
printf("Subtracting %d from %d is %d" ,num1, num2, sum); /*Print answer to screen*/
}
if ( operation == '*' ) /*If condition if operation is **/
{
sum = num1 * num2; /*Sum if multiplication*/
printf("Multiplying %d by %d is %d" ,num1, num2, sum); /*Print answer to screen*/
}
if ( operation == '/' ) /*If condition if operation is /*/
{
sum = num1 / num2; /*Sum if division*/
printf("Dividing %d by %d is %d" ,num1, num2, sum); /*Print answer to screen*/
}
count = count + 1; /*count adds one*/
}
return 0;
}
This will compile, and will go so far as the second number input but then the program just stalls.
I think if I crack this one I might be on the way to doing the others because whatever error I'm committing here is probably holding me back elsewhere.
I understand somebody told me to use else but I tried that with last week's assignment and the basic program (without allowing 3 inputs) wouldn't work so I guessed I had to use four ifs. To do otherwise now would lead to me being even more confused I think.