Thread: error

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    50
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main()
    {
        int x1,y1,x2,y2,a[2],c[2],d=1;
        gh:
        printf("insert x1,y1,x2,y2\n");
        scanf("%d\n%d\n%d\n%d\n",&x1,&y1,&x2,&y2);
        a[d]=(y2-y1)/(x2-x1);
        c[d]=y1-a[d]*x1;
        printf("y=%dx+%d\n",a[d],c[d]);
        d++;
        if(d<=2)
        {
         goto gh;
        }
        else
        {
         if (a[1]==a[2])
         {
                       printf("they are parallel");
         }
        }
        getch();
    }
    this is a program for finding if 2 lines are parallel
    when user enters the co-ordinates of 2 points on the line
    it is working well
    but when the user is supposed to enter 4 datas only it asks for a fifth data which seems to have no effect on the program. but why is this happening????
    plz help!!!!!!!!!

    pst: i am using dev c++
    Last edited by Abhas; 04-07-2011 at 10:33 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
        printf("insert x1,y1,x2,y2\n");
        scanf("%d\n%d\n%d\n%d\n",&x1,&y1,&x2,&y2);
    You should not use \n in your formatting string... If you want separation between numbers simply add spaces or comas. It might even be better to split it in two and ask separately for each line.
    Code:
        printf("insert x1,y1,x2,y2\n");
        scanf("%d,%d,%d,%d",&x1,&y1,&x2,&y2);
    OR....
    Code:
        printf("insert x1 y1 x2 y2\n");
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    50
    cool! that worked!
    @tater:thx. but what if i want it to b shown in new line and at the same time accept the values in one command??? is it possible??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%d\n%d\n%d\n%d\n",&x1,&y1,&x2,&y2);
    Any trailing matching characters after the last conversion causes scanf to keep looking until something fails the last match.

    Edit:
    typically, you use fgets() to read a whole line, then use sscanf() to parse it.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    50
    now 1 more question: why is void main() not accepted in dev c++ though i include the c header files??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Abhas
    now 1 more question: why is void main() not accepted in dev c++ though i include the c header files?
    for what you are dealing with, the return type of main is supposed to be int.
    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

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    50
    why??? it is accepted in c.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Abhas
    why??? it is accepted in c.
    There are a few details about whether it is or is not standard C that I do not wish to discuss here, but take my word for it: your information is incorrect, it is not accepted in C.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM