This is your program with correct indentation.
Since you didn't waste any time indenting it, you didn't see that if the user selects 1 to the first question he's asked, the rest of the program is nearly all inside the "else" to that if statement right at the top of the program. So the whole heart of the program, is skipped over.
Here's what it looks like:
Your code jumps from the blue line at the top, all the way to the blue code at the bottom!Code:#include<stdio.h>
int main(void)
{
/*Create variables.*/
int number1;
int operation;
int total;
int cont;
int result;
/*Ask the user if they'd like to start a new calculation.*/
printf("Would you like to perform a new calculation? Enter 1 for [Yes] or 2 for [No].\n");
scanf("%i", &cont);
/*If yes, set the total to zero.*/
if(cont == 1)
total = 0;
else if(cont == 2)
{
/*Ask user to assign a value to "operation"*/
printf("Please choose an operation. Enter 1 for [Addition], 2 for [Subtraction]");
printf("3 for [Multiplication], or 4 for [Division])\n");
scanf(" %i", &operation);
/*If the operation is [1], begin the addition sequence.*/
if(operation == 1)
{
/*Assign value to the number.*/
printf("Enter the integer that you'd like to add.\n");
scanf(" %i", &number1);
/*Add the numbers to find their sum.*/
result = total + number1;
/*Display the answer.*/
printf("The sum is %d.\n", result);
total = result;
}
/*Start the subtraction block if [2] is entered for operation.*/
if(operation == 2)
{ /*Assign value to the number.*/
printf("Enter the integer you'd like to subtract.\n");
scanf(" %i", &number1);
/*Perform the subtraction operation.*/
result = total - number1;
/*Display the answer.*/
printf("The difference is %d.\n", result);
total = result;
}
/*If multiplication is chosen, start this block.*/
if(operation == 3)
{
/*Ask for a value to be assigned.*/
printf("Enter the integer that you'd like to multiply.\n");
scanf(" %i", &number1);
/*Perform the multiplication sequence.*/
result = 0;
while(number1 > 0)
{
result += total;
number1--;
}
/*Display the result.*/
printf("The product is %d.\n", result);
total = result;
} //end of if operation == 3
/*Division block for operation == 4.*/
if(operation == 4)
{
/*Ask for a value to be assigned.*/
printf("Enter the integer that you'd like to divide by.\n");
scanf(" %i", &number1);
/*Perform the division operation.*/
while(total > 0)
{ total -= number1;
result++;
}
/*Display the result.*/
printf("The quotient is %d.\n", result);
total = result;
}
/*Ask the user if they'd like to start a new calculation.*/
printf("Would you like to perform a new calculation? (Enter 1 for [Yes] or 2 for [No].\n");
scanf(" %i", &cont);
}
else
{
/*Ask the user if they'd like to start a new calculation.*/
printf("Would you like to perform a new calculation? \n");
printf("Enter 1 for [Yes] or 2 for [No].\n");
scanf(" %i", &cont);
/*Ask user to assign a value to "operation"*/
printf("Please choose an operation. Enter 1 for [Addition]");
printf("2 for [Subtraction], 3 for [Multiplication]\n");
printf("or 4 for [Division])\n");
scanf(" %i", &operation);
} //end of possibly the worlds biggest else statement?
result = getchar(); //just holds the console window open
return(0);
}
If you had been indenting your code, you would have seen that in a minute.
Why aren't you stepping through your code, line by line, to see these problems?