Solved

C++ Locate-like command

Posted on 1998-03-07
12
339 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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.

777 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