Thread: code revision help

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    code revision help

    After running my code it spits out long numbers.Im new to C so im not sure what I
    am doing wrong.

    Code:
    #include <stdio.h>
    int main(void) 
    
    {
    	int x,remainder;
    	float y,sum,product,quotient, expression;
    	
    	
    	printf("Enter an integer> ");   //calling for an integer value
    	scanf("%d", &x);
    	printf("Enter a real number> ");  //calling for a real number
    	scanf("%f",&y);
    	
    	x=x;
    	y=y;
    	sum=x+y;
    	product=x*y;
    	quotient=x/y;
            remainder=x/7
            expression=(2*x)+(8y/7);
    	
    
    	printf("_____________________________\n");
    	
    	printf("x value: %d\n", x);            //prints x value
    			            
    	printf("y value: %f\n", y);            //prints y value
    	                  
            printf("%f=%d+%f\n", sum);        //prints sum
    	        	
    	printf("%d*%f=%f\n", product);    //prints product
    	
    	printf("%d/%f=%f\n", quotient);   //prints quotient
    
            printf("%d/7\n",remainder);            //prints remainder
    
            printf("(2*%d)+(8*%f/7)\n",expression);   //prints expression
    	
    	printf("_____________________________\n");
    	
    	return(0);
    	}
    Last edited by mshel130; 09-05-2011 at 03:13 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    printf("%f=%d+%f\n", sum);
    Don't lie to printf. If your format string says you have three numbers, then you had better have three numbers ready to go behind it.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Quote Originally Posted by mshel130 View Post
    Code:
    {
    	int x,remainder;
    ...
    	printf("x value: %d\n", x);            //prints x value
    	}
    I am by no means an experienced c programmer, maybe a few lines ahead of you...but I have experienced that you get strange results when you tell printf to print a double which you do by '%d' while you are actually printing an integer (x is an integer). It should be '%i'.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %d isn't double, it's an int.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    You are right. It is too early for me to know even the simplests of things.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    Thank you tabstop. I fixed the error by("Don't lie to printf") backing up printf with 3 numbers and achieved the desired results.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by mshel130 View Post
    After running my code it spits out long numbers.Im new to C so im not sure what I
    am doing wrong.
    Code:
    	x=x; 
    	y=y;
    This line is redundant to your scanf's and is not needed.
    Quote Originally Posted by mshel130 View Post
    Code:
                 remainder=x/7
    This doesn't give you the remainder, it performs integer division. If x < 7 you will get zero. Use MOD (%) for remainder.
    Quote Originally Posted by mshel130 View Post
    Code:
                 expression=(2*x)+(8y/7);
    C is not a math parser, you should have gotten an error with this line. 8y means nothing.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-20-2010, 11:25 PM
  2. Best Project Management and Revision Control Software?
    By sarah22 in forum General Discussions
    Replies: 31
    Last Post: 05-10-2010, 08:06 AM
  3. PLease give a revision for pointerarrays!
    By demonus in forum C Programming
    Replies: 5
    Last Post: 07-18-2003, 05:10 PM