Thread: Use result in a new statement

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    Use result in a new statement

    Hello,

    I am in the process of learning C and I'm stuck on this code...

    The object is for the user to give 2 integers which will ultimately be multiplied, subtracted and added together. The point which I get stuck at, is how to put the result of one statement into that of another:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a /*first input*/;
    	int b /*second input*/;
    	int c /*result of a + b*/;
    	int d /*result of a - b*/;
    	int e /*result of a * b*/;
    		
    		printf("Enter the first number:");
    		scanf("%d", &a);
    	
    		printf("Enter the second number:");
    		scanf("%d", &b);
    		
    			c = a + b;
    			d = a - b;
    			e = a * b;
    	
    		printf("\na+b=%d\n", c);
    		printf("a-b=%d\n", d);
    		printf("a*b=%d\n", e);
    	
    	return 0;
    }
    If the first input is 78 and the second is 83, this should be the result:

    Enter the first number:78
    Enter the second number:83

    78+83=161
    78-83=-5
    78*83=6474

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    12
    For a and b, you do the same thing you did with c.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Could you be more specific?

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Could you be more specific?
    I think you need to be more specific - your question really doesn't make sense with your code. If I understand you correctly, you're asking how to take the ouput of some expression and use it as the input for another expression. You're pretty much doing that here. Obviously once you have said "c = a + b", c holds a value just like a and b do. So you could say "d = a + c", or you could use c as the parameter to a function - whatever. It's a variable just like a and b.

    scanf("%d", &b);

    c = a + b;
    d = a - b;
    e = a * b;

    printf("\na+b=%d\n", c);

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Code:
    		printf("\na+b=%d\n", c);
    		printf("a-b=%d\n", d);
    		printf("a*b=%d\n", e);
    If I were to execute the program with the code as is, then the final result will display as follows:

    a+b=161
    a-b=-5
    a*b=6474

    What I really want is it to display:

    78+83=161
    78-83=-5
    78*83=6474

    To use another example. If the first input was 12 and the second 25, then the program should display:

    12+25=37
    12-25=-13
    12*25=300

    Instead of:

    a+b=37
    a-b=-13
    a*b=300

    Hope that helps..!

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    printf("\n%d+%d=%d\n", a,b,c);
    Kurt

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Thanks!! This is the result of the final code...

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a /*first input*/;
    	int b /*second input*/;
    	int c /*result of a + b*/;
    	int d /*result of a - b*/;
    	int e /*result of a * b*/;
    		
    		printf("Enter the first number:");
    		scanf("%d", &a);
    	
    		printf("Enter the second number:");
    		scanf("%d", &b);
    		
    			c = a + b;
    			d = a - b;
    			e = a * b;
    	
    		printf("\n%d+%d=%d\n", a,b,c);
    		printf("%d-%d=%d\n", a,b,d);
    		printf("%d*%d=%d\n", a,b,e);
    
    		return 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,007
    Looks good, but I suggest that you make your indentation more consistent. You could also use names that are more descriptive, e.g.,
    Code:
    #include <stdio.h>
    
    int main()
    {
        int input1;
        int input2;
        int result_add;
        int result_sub;
        int result_mul;
    
        printf("Enter the first number:");
        scanf("%d", &input1);
    
        printf("Enter the second number:");
        scanf("%d", &input2);
    
        result_add = input1 + input2;
        result_sub = input1 - input2;
        result_mul = input1 * input2;
    
        printf("\n%d+%d=%d\n", input1, input2, result_add);
        printf("%d-%d=%d\n", input1, input2, result_sub);
        printf("%d*%d=%d\n", input1, input2, result_mul);
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Sounds logical, thanks for the advice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Postfix (reverse polish notation) calculator
    By ottomated in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 05:32 PM
  3. Can someone help with my seg fault?
    By John_L in forum C++ Programming
    Replies: 23
    Last Post: 03-01-2008, 04:04 PM
  4. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  5. Logical error in Switch Statement..
    By gardenair in forum C Programming
    Replies: 4
    Last Post: 05-19-2003, 10:15 PM