?
Solved

C++ Locate-like command

Posted on 1998-03-07
12
Medium Priority
?
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

800 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