# Thread: Need help with basic calculation program.

1. 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:

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("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;
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;
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("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);
}```
Your code jumps from the blue line at the top, all the way to the blue code at the bottom!

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?

2. I changed a few things so the addition function works. I don't know if that's what you want, however.

Here's the code:

Code:
```#include<stdio.h>

int main(void)
{
/*Create variables.*/
int number1, number2;
int operation;
int total;
int cont;
int result;

do  {
/*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;
/*Ask user to assign a value to "operation"*/
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 first integer that you'd like to add.\n");
scanf(" %i", &number1);
printf("Now enter the second integer to be added.\n");
scanf(" %i", &number2);
/*Add the numbers to find their sum.*/
result = number1 + number2;
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;
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;
}
} //end of if(cont == 1)
}while(cont == 1);

result = getchar();  //just holds the console window open
return(0);
}```

3. Weird, I thought I had a couple of more posts here but I don't see them.

Anyway, many thanks to everyone for the help. I really appreciate it, probably couldn't have gotten it done without some of your suggestions/fixes.

Here's the code that I ended up with:

Code:
```#include<stdio.h>

int main(void)
{
/*Create variables.*/
int number1;
int operation;
int total;
int cont;
int result;
int loop = -1;

while(loop == -1)
{
/*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].)\nEnter 3 to [Terminate].\n");
scanf("%i", &cont);

/*Terminate the program if the user assigns [3] to cont.*/
if(cont == 3)
{
return(0);
}
/*If yes, set the total to zero.*/
if(cont == 1)
{
total = 0;
}

/*Ask user to assign a value to "operation"*/
printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 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;
printf("The sum is %d.\n", result);
total = result;
}

/*Start the subtraction block if [2] is entered for operation.*/
else 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;
printf("The difference is %d.\n", result);
total = result;
}

/*If multiplication is chosen, start this block.*/
else 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;
if(number1 > 0)
{
while(number1 > 0)
{
result += total;
number1--;
}
}
if(number1 < 0)
{
while(number1 < 0)
result -= total;
number1++;
}

/*Display the result.*/
printf("The product is %d.\n", result);
total = result;
}

/*Division block for operation == 4.*/
else 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.*/
result = 0;
if(total > 0)
{

if(number1 > 0)
{
while(total > 0)
{
total -= number1;
result++;
}
}
else if(number1 < 0)
{
while(total > 0)
{
total += number1;
result--;
}
}

}
else if(total < 0)
{

if(number1 > 0)
{
while(total < 0)
{
total += number1;
result++;
}
}
else if(number1 < 0)
{
while(total < 0)
{
total -= number1;
result++;
}
}

}
/*Display the result.*/
printf("The quotient is %d.\n", result);
total = result;
}

else
{
printf("That command is not recognized.\n");
}
}

return(0);
}```

4. Here's the code that I ended up with:
It still needs proper indentation