Thread: Need help with basic calculation program.

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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("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);
    }
    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?
    Last edited by Adak; 03-05-2009 at 06:26 PM.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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("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 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;
          	    /*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;
             }
          } //end of if(cont == 1)
       }while(cont == 1);
    
       result = getchar();  //just holds the console window open
       return(0);
    }

  3. #18
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    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;
    /*Display the answer.*/
    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;
    /*Display the answer.*/
    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. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Here's the code that I ended up with:
    It still needs proper indentation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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