Erase line in C++ console program

In summary, the user wants to know how to erase previous lines displayed from the printf function using scanf and other functions. They also ask how to alter their code to function by pressing Enter only once. The solution involves using escape sequences or libraries to position the cursor, and to only call getch() once and store the character in a variable to use multiple times.
  • #1
Deathfish
86
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
  • #2
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);
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 

Related to Erase line in C++ console program

1. How do I erase a line in a C++ console program?

To erase a line in a C++ console program, you can use the standard library function system("cls") to clear the entire console screen. Alternatively, you can use the cout and endl statements to print a series of backspace characters (\b) to move the cursor back and overwrite the existing line.

2. Can I erase a specific line in a C++ console program?

Yes, you can erase a specific line in a C++ console program by using the gotoxy() function from the conio.h library. This function allows you to move the cursor to a specific coordinate on the console screen and print over the existing line.

3. Is it possible to erase only part of a line in a C++ console program?

Yes, you can erase only part of a line in a C++ console program by using the setw() manipulator from the iomanip library. This allows you to specify the width of the output and print over a specific number of characters on a line.

4. How can I remove a line break in a C++ console program?

In order to remove a line break in a C++ console program, you can use the getline() function instead of the cin statement. This will allow you to read in a string of characters with spaces and preserve the entire line without breaking it up into separate lines.

5. Can I undo an erase in a C++ console program?

Unfortunately, there is no built-in function to undo an erase in a C++ console program. However, you can save the original line in a variable before erasing it and then use that variable to print the line again if needed.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
926
  • Engineering and Comp Sci Homework Help
Replies
3
Views
705
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top