Thread: Need help with basic calculation program.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since your while loop starts with the asking for the operation, you need to stop your while loop at that point (don't ask again at the bottom).

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Quote Originally Posted by tabstop View Post
    Since your while loop starts with the asking for the operation, you need to stop your while loop at that point (don't ask again at the bottom).
    Changed to this:

    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);
    
    /*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 yes, set the total to zero.*/
    if(cont == 1)
    {
    	total = 0;
    
    while(cont == 1)
    {
    
    /*Perform addition sequence if the user inputs [1].*/
    if(operation == 1)
    {
    	/*Set total to zero.*/
    		total = 0;
    	/*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;
    }
    
    
    /*Perform subtraction sequence if the user inputs [2].*/
    if(operation == 2)
    {
    	/*Set total to zero.*/
    		total = 0;
    	/*Assign value to the number.*/
    	printf("Enter the integer you'd like to subtract.\n");
    	scanf("%i", &number1);
    	/*Perform the operation.*/
    		result = total - number1;
    	/*Display the answer.*/
    	printf("The difference is %d.\n", result);
    		total = result;
    }
    
    
    /*Perform the multiplication sequence if the user inputs [3].*/
    if(operation == 3)
    {
    	/*Set total to zero.*/
    		total = 0;
    	/*Ask for the integer to multiply.*/
    	printf("Enter the integer that you'd like to multiply.\n");
    	scanf("%i", &number1);
    		/*Perform the operation.*/
    			result = 0;
    			while(number1 > 0)
    				{
    					result = total + total;
    					number1--;
    
    				}
    	/*Display the answer.*/
    	printf("The product is %d.\n", result);
    		total = result;
    }
    	
    /*Perform the division sequence if the user inputs [4].*/
    if(operation == 4)
    {
    	/*Set total to zero.*/
    		total = 0;
    	/*Ask for the value to divide.*/
    	printf("Enter the integer that you'd like to divide.\n");
    	scanf("%i", &number1);	
    		/*Perform the operation.*/
    			result = 0;
    				while(total > 0)
    				{	total -= number1;
    					result++;
    				}
    	/*Display the answer.*/
    	printf("The quotient is %d.\n", result);
    		total = result;
    }
    }
    } /*Close the while loop for cont[1].*/
    
    /*If the user does not want to start a new operation, start the following sequence.*/
    if(cont == 2)
    {
    while(cont == 2)
    {
    
    /*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;
    
    }
    
    /*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;
    }
    }
    }
    
    return(0);
    }
    Now it gets stuck on the first operation that I choose. For instance, choosing addition keeps it asking for another number to add each time it finds a sum.

    I want to just use goto but apparently that's a forbidden practice...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 07:24 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM