Solved

C++ Locate-like command

Posted on 1998-03-07
12
338 Views
Last Modified: 2012-05-04
When I was programming in basic there was a locate command that let me put a character up on the screen in any location provided I supplied an x and y value.  How is this done in C++?
0
Comment
Question by:Booth882
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1183210
C++ is platform/hardware independant.  The standard C and C++ libraries do not assume that you working on a console, that is a display, with rows and columns of letters.  There may be some libraries  available that will provide this sort of feature for a particular platform/hardware.

What compiler and what platform are you using?
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1183211
Only some examples for nietod's comment.

To achieve what you are looking for in UNIC console apps. you would use CURSES, in DOS Windows you woud use either the GDI OutText() function or a method for a specific window derived class, this method changes from VC++ to BC++, etc.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183212
In windows you would use TextOut() (not OutText() (which prints backwards--just kidding)).  But there are lots of other details in windows!  (These details do not change from VC to BC, howwever, unless you are useing MFC or OWL.)  However, I doubt this is a windows program.  If it is a DOS program or a windows console program, then TextOut() isn't available.
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1183213
Try the function
void gotoxy(int x, int y)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183214
gotoxy() is not part of the standard C++ library.  You might have a library that defines it, but Booth might not.
0
 
LVL 1

Author Comment

by:Booth882
ID: 1183215
I guess I should be more specific.  I am using Microsoft Visual C++ 4.0.  Windows at the moment is befuddling, so I am trying to stick with console applications until I figure out what I am doing.  If I were to use this void gotoxy(int x, int y) what should I include?  And what other things should I try?  Thank you all for your help, by the way.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Booth882
ID: 1183216
I guess I should be more specific.  I am using Microsoft Visual C++ 4.0.  Windows at the moment is befuddling, so I am trying to stick with console applications until I figure out what I am doing.  If I were to use this void gotoxy(int x, int y) what should I include?  And what other things should I try?  Thank you all for your help, by the way.
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1183217
Sorry Booth882, function gotoxy() is not avaiable in
Visual C++.  It 's only avaible in Borland C.  To use
it you need to include <conio.h>
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183218
In a windows console application you can use the SetConsoleCursorPosition() API function.
0
 
LVL 1

Author Comment

by:Booth882
ID: 1183219
how do I use SetConsoleCursorPosition and what do I include?  And what is API?
0
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 1183220
Here's an example of how to write a string to the screen:


#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <wincon.h>

void main(void)
{
      COORD coord;
      TCHAR *hello = "Hello World!";
      DWORD charsWritten;

      // Allocate a console buffer
      HANDLE hBuffer = CreateConsoleScreenBuffer(
            GENERIC_READ | GENERIC_WRITE,
            0,
            NULL,
            CONSOLE_TEXTMODE_BUFFER,
            NULL
      );
      if(hBuffer == INVALID_HANDLE_VALUE){
            fprintf(stderr, "ERROR in CreateConsoleScreenBuffer\n");
            exit(1);
      }

      if(!SetConsoleActiveScreenBuffer(hBuffer)){
            fprintf(stderr, "ERROR in SetConsoleActiveScreenBuffer\n");
            exit(1);
      }

      coord.X = 30;
      coord.Y = 12;
      if(!SetConsoleCursorPosition(hBuffer, coord)){
            fprintf(stderr, "ERROR in SetConsoleCursorPosition\n");
            exit(1);
      }

      if(!WriteConsole(hBuffer, hello, strlen(hello), &charsWritten, NULL)){
            fprintf(stderr, "ERROR in WriteConsole\n");
            exit(1);
      }

}
0
 
LVL 1

Author Comment

by:Booth882
ID: 1183221
You have my undying gratitude, jhance.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now