Thread: Why do I need function for gotoxy and not getche?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    1

    Why do I need function for gotoxy and not getche?

    Using dev c++ 4

    Hello I'm working on a little program for subnetting. Everything works, I just don't understand a particular thing. What I want to do is have the user enter in 15 characters that will be stored in a character array. Now to do this I'm using conio.h both for the gotoxy function and the getche function. I want to stop the input after the 15th character is entered or return is pressed. I'm using gotoxy to place the cursor after the field label such as "IP address: " thats all. This isn't the real program just a test one, but it will illustrate my point.
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    #include "conio.h"
    using namespace std;
    int i=0;
    char ch[3];
    
    void gotoxy(int x, int y) {
    COORD c;
      c.X = x - 1;
      c.Y = y - 1;
      SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
    }
    
    int main()
    {
    
    gotoxy(0,30);
    do {
    ++i;
    ch[i]=getche();
    } while (i <= 2);
    cout << endl;
    cout << ch[0];
    cout << ch[1];
    cout << ch[2];
    
    
          system("PAUSE");
          return 0;
    }
    I spent a good few hours looking how to get input one character at a time without the need to terminate. Any how, getche() and gotoxy() are conio.h function. what I need to know is why do I need to include the function void gotoxy() and not the int getche()? the getche works on its own apparently????

    Rob

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Because the real conio.h is a borland header and the Dev-C++ version doesn't include the gotoxy() function. In any event, conio.h should be in the same directory as your standard headers and as such should be included between angle brackets <conio.h>

    Secondly, you're using the std namespace, but none of your headers are under that namespace. Either declare your standard headers as

    <iostream>
    <cstdlib>
    <cstdio>

    or don't use that namespace. There no reason to have it the way you do.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed