Need help with basic calculation program.

This is a discussion on Need help with basic calculation program. within the C Programming forums, part of the General Programming Boards category; I'm trying to write a program for my C class and I'm having some trouble with it. We were told ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    24

    Post Need help with basic calculation program.

    I'm trying to write a program for my C class and I'm having some trouble with it. We were told to create a calculation program that performs addition, subtraction, multiplication, and division...using only addition and subtraction.

    I understand the concept of multiplication and division as repeated addition and subtraction (respectively), but I don't know how to translate that into C. We've just started talking about if statements and while loops, and the instructor told us that our program should end up with the two inside each other.

    Here's all I've been able to come up with so far:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    /*Create variables.*/
    	int number1;
    	int number2;
    	int operation;
    	int total;
    
    /*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);
    
    /*Perform addition sequence if the user inputs [1].*/
    if(operation = 1)
    {
    	/*Set total to zero.*/
    		total = 0;
    	/*Assign value to the first number.*/
    	printf("Enter the first interger that you'd like to add.\n");
    	scanf("%i", &number1);
    	/*Assign value to the second number.*/
    	printf("Now input the interger that you'd like to add to %d.\n", number1);
    	scanf("%i", &number2);
    	/*Add the numbers to find their sum.*/
    		total = number1 + number2;
    	/*Display the answer.*/
    	printf("The sum of %d and %d is %d.\n", number1,number2,total);
    	
    printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
    scanf("%i", &operation);
    
    }
    
    /*Perform subtraction sequence if the user inputs [2].*/
    if(operation = 2)
    {
    	/*Set total to zero.*/
    		total = 0;
    	/*Assign value to the first number.*/
    	printf("Enter the interger you'd like to subtract from.\n");
    	scanf("%i", &number1);
    	/*Assign value to the second number.*/
    	printf("Now input the interger that you'd like to subtract from %d.\n", number1);
    	scanf("%i", &number2);
    	/*Subtract the second number from the first to find the difference.*/
    		total = number1 - number2;
    	/*Display the answer.*/
    	printf("The difference of %d and %d is %d.\n", number1,number2,total);
    
    printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
    scanf("%i", &operation);
    
    }
    
    /*Perform the multiplication sequence if the user inputs [3].*/
    if(operation = 3)
    {
    	/*Set total to zero.*/
    		total = 0;
    	printf("Enter the interger that you'd like to multiply.\n");
    	scanf("%i", &number1);
    	printf("Now input the interger that you'd like to multiply %d by.\n", number1);
    	scanf("%i", &number2);
    
    printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
    scanf("%i", &operation);
    
    }
    
    /*Perform the division sequence if the user inputs [4].*/
    if(operation = 4)
    {
    	/*Set total to zero.*/
    		total = 0;
    	printf("Enter the interger that you'd like to divide.\n");
    	scanf("%i", &number1);
    	printf("Now input the interger that you'd like to divide %d by.\n", number1);
    	scanf("%i", &number2);
    
    printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
    scanf("%i", &operation);
    
    }
    
    
    return(0);
    }
    Thanks in advance for any help. I really just want to understand how to do this. Been reading over the textbook for hours but it's just not clicking.
    Last edited by StateofMind; 03-04-2009 at 10:55 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by StateofMind View Post
    I'm trying to write a program for my C class and I'm having some trouble with it. We were told to create a calculation program that performs addition, subtraction, multiplication, and division...using only addition and subtraction.

    I understand the concept of multiplication and division as repeated addition and subtraction (respectively), but I don't know how to translate that into C. We've just started talking about if statements and while loops, and the instructor told us that our program should end up with the two inside each other.

    Here's all I've been able to come up with so far:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    /*Create variables.*/
    	int number1;
    	int number2;
    	int operation;
    	int total;
    
    /*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);
    
    /*Perform addition sequence if the user inputs [1].*/
    if(operation = 1)   //should have two equal signs. One = is for assignment.
    {
    	/*Set total to zero.*/
    		total = 0;
    	/*Assign value to the first number.*/
    	printf("Enter the first interger that you'd like to add.\n");   //interger??
    	scanf("%i", &number1);
    	/*Assign value to the second number.*/
    	printf("Now input the interger that you'd like to add to %d.\n", number1); //interger??
    	scanf("%i", &number2);
    	/*Add the numbers to find their sum.*/
    		total = number1 + number2;
    	/*Display the answer.*/
    	printf("The sum of %d and %d is %d.\n", number1,number2,total);
    }
    
    /*Perform subtraction sequence if the user inputs [2].*/
    if(operation = 2)
    {
    	/*Set total to zero.*/
    		total = 0;
    	/*Assign value to the first number.*/
    	printf("Enter the interger you'd like to subtract from.\n");
    	scanf("%i", &number1);
    	/*Assign value to the second number.*/
    	printf("Now input the interger that you'd like to subtract from %d.\n", number1);
    	scanf("%i", &number2);
    	/*Subtract the second number from the first to find the difference.*/
    		total = number1 - number2;
    	/*Display the answer.*/
    	printf("The difference of %d and %d is %d.\n", number1,number2,total);
    }
    
    /*Perform the multiplication sequence if the user inputs [3].*/
    if(operation = 3)
    {
    	/*Set total to zero.*/
    		total = 0;
    	printf("Enter the interger that you'd like to multiply.\n");
    	scanf("%i", &number1);
    	printf("Now input the interger that you'd like to multiply %d by.\n", number1);
    	scanf("%i", &number2);
    		
    
    }
    
    /*Perform the division sequence if the user inputs [4].*/
    if(operation = 4)
    {
    	/*Set total to zero.*/
    		total = 0;
    	printf("Enter the interger that you'd like to divide.\n");
    	scanf("%i", &number1);
    	printf("Now input the interger that you'd like to divide %d by.\n", number1);
    	scanf("%i", &number2);
    
    }
    
    
    return(0);
    }
    Thanks in advance for any help. I really just want to understand how to do this. Been reading over the textbook for hours but it's just not clicking.
    If you know you need to subtract in a loop to handle division, then where's the subtraction code at?

    Code:
    answer= 0;
    while (numerator > 0)  {
       numerator -= denominator;
       answer++;
    }

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Quote Originally Posted by Adak View Post
    If you know you need to subtract in a loop to handle division, then where's the subtraction code at?

    Code:
    answer= 0;
    while (numerator > 0)  {
       numerator -= denominator;
       answer++;
    }
    I didn't even know where to start, I'm very new to this. :/

    Thank you very much, I see what you've done and I'm kind of irritated that I didn't think of it myself.

    Would multiplication work like this:

    Code:
    answer = 0;
    while (number2 > 0) {
     number1 += number1;
     number2--;
     answer++;
    }
    ?

    edit: er, nvm. That wouldn't work at all. How can I add the first number to itself (number2 amount of times) and get an answer while retaining the value (number1 entered by the user) to find the answer in the first place?
    Last edited by StateofMind; 03-04-2009 at 11:21 PM.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by StateofMind View Post
    How can I add the first number to itself (number2 amount of times) and get an answer while retaining the value (number1 entered by the user) to find the answer in the first place?
    Simple - do not add it to itself - add it to answer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Quote Originally Posted by vart View Post
    Simple - do not add it to itself - add it to answer
    Ah,

    Code:
    	total = 0;
    			while(number2 > 0)
    				{
    					total += number1;
    					number2--;
                                     }
    Thanks so much!

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by StateofMind View Post
    Ah,

    Code:
    	total = 0;
    			while(number2 > 0)
    				{
    					total += number1;
    					number2--;
                                     }
    Thanks so much!
    (Your indentation sucks!) But yeah, that'll work. Though you'll have to take care if number2 is negative, as that loop will basically run forever in that case (There are various ways of fixing that).
    Here is a small alteration to make the loop neater:
    Code:
    int total = 0;
    while (number2--)
    {
        total += number1;
    }
    QuantumPete
    Last edited by QuantumPete; 03-05-2009 at 02:01 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Quote Originally Posted by QuantumPete View Post
    Code:
    int total = 0;
    while (number2--)
    {
        total += number1;
    }
    QuantumPete
    I don't really understand this. The while(number2--) part is what I don't get...

    I see what you're saying about negative numbers though. I do need to fix that for both multiplication and division...
    Last edited by StateofMind; 03-05-2009 at 10:23 AM.

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by QuantumPete View Post
    Though you'll have to take care if number2 is negative, as that loop will basically run forever in that case (There are various ways of fixing that).
    a negative number is not > 0 and the loop won't run.

    Quote Originally Posted by StateofMind View Post
    I don't really understand this. The while(number2--) part is what I don't get...

    I see what you're saying about negative numbers though. I do need to fix that.
    It checks the value of number2 for the while condition (It will run for any non zero value) and then subtracts 1 from number2

    actually, It looks like Pete's suggestion is the one that will always run if number2 is negative

    I may be wrong though
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by StateofMind View Post
    I don't really understand this. The while(number2--) part is what I don't get...
    When you do number2-- it will use the current value of number2, run the loop and then decrement it.
    As you probably know, any number, except 0 is considered "true" in C.
    The last thing is that a number by itself is still considered a statement and the result of this statement is basically number2 (because it's post-decremented). Since while() takes a boolean arguments (i.e. either true or false), number2 is considered as a boolean statement. When number2 is not 0, it returns true. So when number2 == 2, it is true, the loop runs, number2 == 1, still true, the loop runs, number2 == 0, this is considered false, so the loop is no longer run. This is a short-hand way of not having to write (while x != 0).
    Though, as it has been pointed out, this will not work for negative numbers, since -1 is also considered true.

    Quote Originally Posted by ಠ_ಠ View Post
    actually, It looks like Pete's suggestion is the one that will always run if number2 is negative
    Yep, sorry, I was already thinking ahead to my alteration when I wrote that. Only my suggestion will run forever if number2 is -ve. For StateOfMind's code, it won't execute the loop at all as you've correctly pointed out.

    QuantumPete
    Last edited by QuantumPete; 03-05-2009 at 10:57 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Can anyone help me get this to loop correctly?

    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;
    
    while(cont == 1)
    {
    /*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);
    
    /*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;
    }
    /*Ask the user whether or not 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);
    
    } /*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;
    }
    /*Ask the user whether or not 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);
    }
    }
    
    return(0);
    }
    I'm trying to make it to where after an operation is performed, the sequence starts over from the beginning and the user is asked whether or not to start over and then which operation they'd like to perform. I think I've just created a bunch of redundant code trying to do this...

    Also, how on Earth do I fix the negative integer problem with multiplication and division? This stuff is so much harder than I expected, but I love it!
    Last edited by StateofMind; 03-05-2009 at 12:49 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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).

  12. #12
    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...

  13. #13
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by QuantumPete View Post
    Yep, sorry, I was already thinking ahead to my alteration when I wrote that. Only my suggestion will run forever if number2 is -ve. For StateOfMind's code, it won't execute the loop at all as you've correctly pointed out.

    QuantumPete
    lol, your solution to a loop that might not execute was a loop that might execute forever?

    I guess throwing your code into an If statement with it decrementing if its >0 and incrementing if it's < 0 would work

    Code:
      int total = 0;
    
      if(number2 > 0) {
        while (number2--)
        {
           total += number1;
        }
      }else {
        while (number2++)
        {
           total += number1;
        }
      }
    Last edited by ಠ_ಠ; 03-05-2009 at 03:11 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    How do I get my code to go back to the top and evaluate cont after it runs through either set of if statements? It just keeps terminating...

    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);
    
    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);
    }
    Last edited by StateofMind; 03-05-2009 at 06:15 PM.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're right, goto's are strong medicine, and have to be used, very sparingly.

    Here's an example of when to use a goto:

    Code:
    while (variable > 0) 
    {
        if(variable mod 10 == 0)
        {
            for(i = 0; i < j; i++)
           {
                for(j = 10000; j > 0; j--)
                {
                     if(j % 288 == 0 && j < 599)  
                     {
                         goal = 1;
                         goto Done;
                     }
                 }
            }
        }
    }
    Done:
    ;
    Because a simple "break" will not cleanly exit from all the levels of the loops, etc.

    Other than something like this, goto's lead to ........ty code, which quickly degenerates into
    an unmanageable "can of worms".

    I'll take a look at your program, in just a bit.

Page 1 of 2 12 LastLast
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, 02:30 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21