Solved

C++ Locate-like command

Posted on 1998-03-07
12
335 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

758 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

24 Experts available now in Live!

Get 1:1 Help Now