Comp Sci Erase line in C++ console program

  • Thread starter Thread starter Deathfish
  • Start date Start date
  • Tags Tags
    C++ Line Program
Click For Summary
To erase previous lines in a C++ console program, you can use the Windows API to set the cursor position and overwrite lines with blank spaces. The code provided has multiple calls to getch(), causing it to require three Enter presses instead of one; this can be fixed by storing the result of getch() in a variable and using it instead of calling it multiple times. The operating system typically waits for an Enter key press before sending input to the program, so immediate keypress responses require specific handling. Using functions like SetConsoleCursorPosition allows for precise control over the console output. Properly managing input and output can streamline user interaction in console applications.
Deathfish
Messages
80
Reaction score
0

Homework Statement


How do I erase previous lines displayed from printf function? (use printf, scanf etc)

Welcome to Sample Program
[Press Enter to Start]

When I press Enter, the [Press Enter to Start] should disappear.


Homework Equations


- nil -


The Attempt at a Solution



unsigned char mainstart=0;
printf("SAMPLE PROGRAM");
printf("\n==============================");
while(mainstart!=1)
{
printf("\n[Press Enter to start]");
getch();
while(getch() != 0x0d);

if(getch() == 0x0d)
{
mainstart=1;
}
}

Also, why does my code only function when I press Enter three times? How do I alter the program to function by pressing Enter only once?
 
Physics news on Phys.org
Depends on the OS. Some allow escape sequences and/or provide libs that let you position the cursor exactly (like curses). On Windows, use something along these lines:

HANDLE ConHandle = GetStdHandle(STD_OUTPUT_HANDLE);

COORD Pos;
Pos.X=3;
Pos.Y=3;

SetConsoleCursorPosition(ConHandle,Pos);
 
I think there's also an ASCII code ("tab up" maybe?) that DOS (the "command line") will recognize as meaning "go up one line" --- then you have to write a line of blanks, NOT followed by a new-line so as to stay on the line you just erased.
 
Deathfish said:
Also, why does my code only function when I press Enter three times? How do I alter the program to function by pressing Enter only once?
The operating system typically doesn't deliver any typed characters to your program until you hit enter. If you want to respond immediately to keypresses, you have to do something special for your operating system.
 
Deathfish said:
Also, why does my code only function when I press Enter three times? How do I alter the program to function by pressing Enter only once?

You called getch() three times, and each call reads another character.

If you want to use the same character value several times, call getch() once and store the character in a variable.

But in your code, only one of the getch() calls is doing anything useful, You can delete the other two calls completely, and you don't need to store the character that you read.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
9
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K