Thread: something wrong with gotoxy

  1. #1
    Shadow12345
    Guest

    something wrong with gotoxy

    I know this topic has been posted a bunch of times, but I honestly don't know what I am doing wrong. It seems that the points displayed aren't what the user entered.

    here is my code
    ------------------------------------------------------------------------------------

    #include <windows.h> //wasn't sure what headers I needed
    #include <stdlib.h> //so I included a bunch!
    #include <stdio.h>
    #include <conio.h>
    #include <iostream>
    using namespace std;

    void gotoxy(int X, int Y) //you need this whenever you use
    { //gotoxy
    COORD coord;
    coord.X = X;
    coord.Y = Y;

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hConsole, coord);
    }

    void draw(int x1, int y1, int x2, int y2) //drawing function
    { //just draws a "." at the
    //points
    gotoxy(x1, y1); cout << ".";
    gotoxy(x2, y2); cout << ".";
    }

    int main()
    {
    int x1, y1, x2, y2;
    cout << "Enter your coordinates" << endl;
    cout << "Point 1" << endl;
    cin >> x1 >> y1;
    cout << "Point2" << endl;
    cin >> x2 >> y2;
    gotoxy(0, 0);
    system("CLS");
    draw(x1, y1, x2, y2);
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";

    return 0;
    }

    ------------------------------------------------------------------------------------

    Like I said this kinda almost sort of works, but the actual output seems to not be what the user enters. Funky.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    63
    I see what the problem is.

    gotoxy(0, 0);
    If the point that is displayed on the screan is 0 by 0 then you should change the gotoxy(0,0); to gotoxy(x1,y1);.

    You were not giving the function the users coords.

    Oops!
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    63
    I suddenly see that there is no point for putting the
    gotoxy(0,0);
    statement where you put it.

    It doesn't need to be there.

    And I just copied and pasted the code and it worked fine for me.

    I just thought that the gotoxy(0,0); would have been the problem.

    Sorry, but there is nothing wrong with the code.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but the actual output seems to not be what the user enters.
    It works fine for me.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    your gotoxy function looks fine...

    try initializing the HANDLE in main()
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  6. #6
    Shadow12345
    Guest
    >try initializing the HANDLE in main()
    How do I do this exactly?

  7. #7
    Shadow12345
    Guest
    ok you guys say that this works for you, but it still doesn't work for me. I mean it compiles, and it DOES draw dots, but the placement of the dots isn't correct.
    When I enter (1,2) for the first coordinate and (5,6) for the second coordinate my output looks like this:
    ----------------------------
    x //This looks more like (1, 6)


    .....x//This looks more like (5,1)
    -----------------------------
    when the output should look something like this:
    ----------------------------
    .....x(5,6)

    x (1,2)

    ----------------------------
    Also note that I get the first output even if I switch the coordinates, meaning (5,6) is point one and (1,2) as the second point. (Yes I know my original program doesn't use x's).
    Last edited by Shadow12345; 05-18-2002 at 03:41 PM.

  8. #8
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    swap your x and y coords until you find the combination which is right, this looks like its right to me
    Code:
    void draw(int x1, int y1, int x2, int y2) //drawing function 
    { //just draws a "." at the 
    //points 
    gotoxy(x1, y2); cout << "."; 
    gotoxy(y1, x2); cout << "."; 
    }
    If not, who knows......
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  9. #9
    Shadow12345
    Guest
    Wow, wow, wow, wow, wow...as if...THAT WORKS...wow...


  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    63
    I don't think that swapping the x and y coords is the problem.

    I think it should stay the same.
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    It works so what is the question? That it doesn't goto the correct coordinates? it does (remember it counts (0,0) as the first coord) so if the user wanted the 1st column 1st row he would need to put in "0 0"
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    63
    True, True
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

  13. #13
    Shadow12345
    Guest
    >It works so what is the question? That it doesn't goto the correct coordinates?
    Exactly. It seems that the coordinates displayed weren't what the user actually entered. This is very weird indeed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use gotoxy()??
    By Beginnerinc in forum C Programming
    Replies: 5
    Last Post: 08-15-2010, 12:23 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM