Please suggest something better than gotoxy function in C++ - c++

I have written a program that consists of multiples and multiples of function each of which are printing several things on the console when control is passed to them. Now I am trying to print everything in the center of the screen rather than on the upper left corner of the screen. For that purpose, the only thing I know is the gotoxy function of Windows.h. Now this would be an extremely hectic job because I would have to place gotoxy above each "cout". Is there a way that I set the cursor to a particular position on the screen and every time anything gets printed, the printing commences from that particular position.

Write a small helper function (e.g. printCentered(std::string) ) that receives the string to be printed. This function moves the cursor to the center and then prints the parameter. Then replace your couts with a call to this function.

Don't use cout or any other stream-based I/O for drawing all over the screen. It doesn't make sense to "position" a stream if it's being redirected to a different device.
Call the Win32 console functions directly, such as this example that draws a status line in a console program.

The following allows easy find-and-replace and takes care of setting the cursor to the center:
#include <iostream>
std::ostream& PrintCentered(){
// comment in the following if you're experiencing
// weird output due to io-buffering like #Ben says in a comment
//std::cout.flush();
gotoxy(your_x, your_y);
return std::cout;
}
Now just find&replace your std::cout calls with the above function where you want it to be centered. Usage after replace should look like this:
PrintCentered() << "your message";

Assuming you are using cout to write to, this hack should do the job:
#define AT_CENTER( stuff ) goto( 100, 100 ); cout << stuff;
Where 100, 100 should be replaced with your specific values. Then in use:
AT_CENTER( "The meaning of life is " << x );

Related

C++: How to return output at the cursor position?

If we take the standard "Hello world" example, I want to send the output to the current cursor position, NOT to the console window. In fact, I would like the console window to disappear.
What I want to do is basically an autocorrect function: cin >> ae; cout << ä;
Grabbing the first part works, but sending the output to the right location proves surprisingly difficult, even after reviewing countless youtube videos...

Using function parameters and arguments in Xcode 8 with C++

I've only recently started learning to code (About 3 days ago, to be exact!) so I really have no idea what I'm doing, to the point that I'm having trouble researching answers to my questions because I don't really know the terminology for anything.
Anyway, I'm learning on learncpp.com, and I'm up to 1.4a – "A first look at function parameters and arguments".
I tried to run this piece of code:
#include <iostream>
void printValues(int x, int y)
{
std::cout << x << std::endl;
std::cout << y << std::endl;
}
int main()
{
printValues(6, 7);
return 0;
}
Apparently it's supposed to spit out:
6
7
in the console window.
However, when I run it, it just says (lldb) in the console window and in the variable window it says:
[A]x = int (6)
[A]y = int (7)
The program also doesn't seem to finish running as it should – it doesn't spit out the return number at the end and when I try to change it and rerun it it asks if I want to terminate the program that's already running.
If it's any help, it highlights this line in green:
std::cout << x << std::endl;
It might also be worth noting that the tutorials on the website use Visual Studios, so I assume there's a difference between two programs that's causing me to have a problem?
Sorry if there is an obvious way to find the answer to this question, I've done some Googling and I tried watching Youtube tutorials etc. but I can't seem to find anything addressing my issue. Maybe I'm not looking in the right place.
If anybody would be able to help me or even direct me to where I might find an answer that would be much appreciated.
Thank you!
Did you set a breakpoint on that line? Usually the debugger will stop on a line if there's an error, or the developer set a breakpoint on that line. If it's highlighted in green, that's a breakpoint. You should see a blue pointer to the left of the line, like this:
If you click on it, it will turn light blue and the breakpoint will be disabled. You can then either type "continue" at the prompt, or press the continue button (it's a right-pointing triangle with a rectangle to the left of it, sort of like the "Play/Pause" button on a music player).
To remove the breakpoint, grab the blue arrow and drag it to the left and then let go of it. It should disappear in a puff of smoke.

How do you output in languages that are read from right to left?

I'm curious how one handles output in languages (e.g. Arabic, Hebrew) that are read from right to left. Almost everything in C/C++ or any language I have seen is premised on outputting from left to right.
Is there a system call or something that will force a terminal, for example, to print right to left? Even so, inside a program strings are coded like string("this is a string"). Wouldn't it have to code something like string("string a is this") or some reverse function be used one everything?
I suspect that in practice, a system that is configured for a right-to-left language will have terminal settings the print from right-to-left, so that the internals of a C/C++ program need not worry about it.
If that doesn't reassure you, here's a hack:
string str = "The look on your face is priceless.";
cout << "\u202e" << str << endl;
At the lowest layers, there are the extended window styles for setting up a window in a "right to left" ordering: WS_EX_LAYOUTRTL, WS_EX_RIGHT, and WS_EX_RTLREADING. Also, the API call, SetProcessDefaultLayout with LAYOUT_RTL. The right combination of these flags and APIs means the (0,0) coordinate is the top right of the window and the x-axis increases linearly.
Here's a link to the classic MSDN article that explains all the fine details.

Is there a way to set the console height(in lines)?

Is it possible to set how many lines the console will print before it starts to erase the top ones? For example, is it possible to set it to 3 and only make the last 3 lines be visible? So:
std::cout<<"line 1!"<<std::endl;
std::cout<<"line 2!"<<std::endl;
std::cout<<"line 3!"<<std::endl;
std::cout<<"line 4!"<<std::endl;
system("pause");
Would output:
line 3!
line 4!
Press any key to continue...
^without creating a scroll bar on the side.
I've been trying to use Console::BufferHeight but I can't seem to get it to work. This is the only thing I've been able to find that seems to be close to what I want to do: http://msdn.microsoft.com/en-us/library/system.console.bufferheight.aspx But It just shows how to read it, not how to set the size. And for some reason typing just std::cout<<System::Console::BufferHeight; gives me scope errors. Any help would be greatly appreciated. Thanks!
I think you want this:
SetConsoleWindowInfo
Example,
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT rect = {0,0, 100, 100};
SetConsoleWindowInfo(hConsole, TRUE, &rect)
Have a look at these as well (experiment with them):
SetConsoleScreenBufferSize()
SetConsoleScreenBufferInfoEx
There is an example on MSDN here.
Im new to programming also, but I tried:
while(!cin.get())
{
}
and it worked,try to place it in the bottom of the code. It will work wonders on your console!
Try changing its position (put it in the middle of the code)
I'd suggest keeping the lines yourself, in a quick class, say that cycles where to put the next line by using an iterator that is always set to point at the next line to be input into.
Next, use FillConsoleOutputCharacter() to print blanks over the lines you previously had printed there.
Then, use SetConsoleCursorPosition() to four(or however many lines you wanted) lines above where you want your input to start, and output each line in you cycle, starting at the one after your iterator. This prints all the lines in order from eldest to youngest.It's been a while, so my knowledge of C++ is kinda hazy, but this should be pretty simple with the standard library and win32 library.

Implementing the clrscr() function to understand its working

I am trying to make the copies of the builtin functions and adding a x to their name so i can understand each functions working.While writing a function for clrscr() i am confused about how it works.Does it use 2 nested loops and print (" ") i.e space all over the screen or it prints("\n") over the screen?Or what?
I tried this:
#include<stdio.h>
#include<conio.h>
void main(void)
{
printf("press any key to make clrscr() work");
getch();
for(int i=0;i<50;i++)
{
printf("\n");
}
// to make the screen come to 1,1
gotoxy(1,1);
getch();
}
clrscr() implementation may depend on the environment your console application runs. Usually it sends the ClearScreen control character (0x0C) to the console driver, that actually clears the screen.
The driver knows about character space to clear as well as all attributes (blink, underline,...) to reset.
If you dont want the driver to handle 0x0C, you can mimic this with 50 times calling printf("\n"). but calling 50x80 calling poutchar(' ') is not similar to calling clrsrc(), since the cursor will be advanced by one what may put it in the next line after scrolling the screen content.
Further you should regard, that the behaviour of the screen depends on the implementation. When the cursor position is in the right column and you output one character the cursor position may stay at the right edge or it may cause a new line. Whe you cursor position is in the lower right corner the next character may cause a new line including scrolling the screen content by one line.
The best way would be to imaging what clrscr() would do and let it make it's job.