Thread: Need a help with a simple program printing a certain statement (Beginner)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Need a help with a simple program printing a certain statement (Beginner)

    I need help for a class where I have to make a program that calculates the square of distance between two points in the xy axis (x1,y1) and (x2,y2)

    For the most part the program can calculate out the answer. My only problem is that I need the program to STATE "The square of the distance between (x1,y1) and (x2,y2) is Z, where Z is the answer. Right now when I attempt to compile the program, it tells me the line with [ printf("The square of the distance between (%lf,%lf) and (%lf,%lf) is %lf", x1, y1, x2, y2);] has too few arguments for format. Can someone help me fix the error in the code? This is the first real program i'm writing so I am pretty lost.

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    
     double x1, x2, y1, y2, z;
    
    
       printf("Enter the value of x1: ");
       scanf("%lf", &x1);
    
    
       printf("Enter the value of y1: ");
       scanf("%lf", &y1);
    
    
       printf("Enter the value of x2: ");
       scanf("%lf", &x2);
    
    
       printf("Enter the value of y2: ");
       scanf("%lf", &y2);
    
    
       z = ((x2 - x1) * (x2 -x1))  + ((y2 - y1) * (y2 - y1)) ;
    
     printf("The square of the distance between (%lf,%lf) and (%lf,%lf) is %lf", x1, y1, x2, y2);
    
    
    
    
       return 0;
    
    
    
    
    }
    Last edited by heyitzaustin; 10-14-2012 at 02:20 AM.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    printf("The square of the distance between (%lf,%lf) and (%lf,%lf) is %lf", x1, y1, x2, y2,z);

    look at the end of this statement, bold text is what you're missing!

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    I can't believe I missed something that simple


    I truly am a beginner

    Thank you!!

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    48
    No worries, yeah when I was going to post to offer "help" I realized the only thing to do was just point it out. Haha!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try ramping up the warning level on your compiler.

    Eg.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:28:2: warning: format ‘%lf’ expects a matching ‘double’ argument [-Wformat]
    bar.c:7:25: warning: variable ‘z’ set but not used [-Wunused-but-set-variable]
    Gives you two clues that z isn't going to get printed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 10-31-2011, 11:06 AM
  2. Beginner If Statement Help
    By shukiren in forum C Programming
    Replies: 3
    Last Post: 09-06-2011, 02:05 PM
  3. Replies: 8
    Last Post: 05-02-2010, 12:19 PM
  4. simple program - i am a beginner
    By travelstar in forum C Programming
    Replies: 3
    Last Post: 04-04-2008, 02:35 PM
  5. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 07:23 AM