Erase line in C++ console program

  • Context: Comp Sci 
  • Thread starter Thread starter Deathfish
  • Start date Start date
  • Tags Tags
    C++ Line Program
Click For Summary

Discussion Overview

The discussion revolves around how to erase previous lines displayed in a C++ console program using functions like printf and scanf. Participants explore methods for managing console output, particularly in relation to user input and cursor positioning.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant asks how to erase the line "[Press Enter to Start]" after it is displayed, indicating a need for console manipulation techniques.
  • Another participant suggests that the method to erase lines may depend on the operating system, mentioning the use of escape sequences or libraries like curses for cursor positioning.
  • A different participant proposes using an ASCII code to move the cursor up one line and then writing a blank line to effectively erase the previous line.
  • Several participants express confusion about why the code requires pressing Enter three times, with one noting that the operating system typically does not deliver typed characters until Enter is pressed.
  • Another participant clarifies that multiple calls to getch() are causing the issue, suggesting that storing the character from a single call could simplify the code.

Areas of Agreement / Disagreement

Participants generally agree that the behavior of the code is related to how input is handled by the operating system, but there are multiple competing views on the best approach to erase lines and manage user input effectively.

Contextual Notes

There are limitations regarding the assumptions about the operating system's behavior and the specific libraries available for console manipulation. The discussion does not resolve how to implement the proposed solutions or the effectiveness of different methods.

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
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · 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
5K