Need help...

What is the equivalent statement for LOCATE (QBASIC) in c++ and what's the syntax of it?
Is that the one that places the cursor at a specific position on the screen? C++ doesn't have any functions like that, but some third party libraries provide them.

Windows only:
http://msdn.microsoft.com/en-us/library/ms682073%28v=vs.85%29.aspx
Cross platform:
http://pdcurses.sourceforge.net/
http://www.gnu.org/s/ncurses/

Not something a very inexperienced beginner would want to try, I think.
Last edited on
ok.. thanks.. how about a CLEARSCREEN statement in c++?
C++ has no built-in concept of a console, so it doesn't have anything that would require assuming that a console exists. Clearing the screen, moving the cursor around, getting the size of the console, all of it requires assuming that a console exists.
@beginnercode

Here is a small program that writes a lot of X's to the screen, uses a gotoXY routine to print text at a specific location on the screen, waits for you to press a key, clears the screen and then waits again for a key press. Play around with till you feel comfortable with it, and then use the routines in your next project

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// gotoXY and Clearing The Screen : main project file.

#include <stdafx.h>
#include <stdio.h>
#include <iostream>
#include <windows.h>

using namespace std;
using namespace System;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

void gotoXY(int, int);
void cls( HANDLE hConsole );
CHAR WaitKey(VOID);

int main( void )
{
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

	for (int y = 0;y<1520;y++)
		printf( "X" );

	gotoXY(20,21);
	cout << "Press any key to continue..";

	WaitKey();
		
    cls(console);

	gotoXY(26,13);
	cout << "Press any key to end..";
	WaitKey();
    return 0;
}

void gotoXY(int x, int y) 
{ 
CursorPosition.X = x; 
CursorPosition.Y = y; 
SetConsoleCursorPosition(console,CursorPosition); 
}

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor 
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; 
   DWORD dwConSize;

// Get the number of character cells in the current buffer. 

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   // Fill the entire screen with blanks.

   if( !FillConsoleOutputCharacter( hConsole,        // Handle to console screen buffer 
                                    (TCHAR) ' ',     // Character to write to the buffer
                                    dwConSize,       // Number of cells to write 
                                    coordScreen,     // Coordinates of first cell 
                                    &cCharsWritten ))// Receive number of characters written
   {
      return;
   }

   // Get the current text attribute.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
   {
      return;
   }

   // Set the buffer's attributes accordingly.

   if( !FillConsoleOutputAttribute( hConsole,         // Handle to console screen buffer 
                                    csbi.wAttributes, // Character attributes to use
                                    dwConSize,        // Number of cells to set attribute 
                                    coordScreen,      // Coordinates of first cell 
                                    &cCharsWritten )) // Receive number of characters written
   {
      return;
   }

   // Put the cursor at its home coordinates.

   SetConsoleCursorPosition( hConsole, coordScreen );
}

CHAR WaitKey (VOID)
{
  HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
  INPUT_RECORD irInputRecord;
  DWORD dwEventsRead;
  CHAR cChar;

  while(ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
    if (irInputRecord.EventType == KEY_EVENT
	&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
	&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU
	&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL)
    {
      cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
	ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
	return cChar;
    }
  return EOF;
}


EDIT on 09.06.11 : Removed conio.h and replaced old WaitKey routine with a new one that doesn't use the conio.h header.
Last edited on
@whitenite1 ...
Can I have a copy of that code in Dev C++?????
#include "conio.h"
*Sigh* You know... It's 2011.
helios what did you mean by 2011 :D?
seriously what would u included instead?
what did you mean by 2011
conio.h is an old, old console I/O library. Old.

seriously what would u included instead?
I've answered that, already.
oh right, sorry i am too sleepy atm
@beginnercode

Sorry, I don't use Dev C++, just Microsoft Visual C++ 2008 Express

[ Edited above program so as to not use conio.h, as suggested by helios. ]
Last edited on
Topic archived. No new replies allowed.