Go back a line.

How do you go back a line? (Opposite of \n).
I have Dev C++.
@usandfriends

You could use a function like gotoXY(), as shown in the following program. Just adapt it to your needs.

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
// Replace Line.cpp : main project file.

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

void gotoXY(int, int);

int main()
{
	int line=0;
	gotoXY(5,12);
	cout << "1) This is line one";
	gotoXY(5,14);
	cout << "2) This is line two";
	gotoXY(5,16);
	cout << "3) This is line three";
	
	do
	{
		gotoXY(5,20);
		cout << "Replace which line? ( 1, 2 or 3 )";
		cin >> line;
		if (line<1 || line>3)
		{
			gotoXY(5,22);
			cout << "We only have three lines..";
			Sleep(1500); 
			gotoXY(5,22);
			cout << "                           ";
		}
	} while ( line<1||line>3);

gotoXY(8,10+(line*2));
cout << "This is a new line..";

gotoXY(5,20);
cout << "   Program finished!!             ";

	cin.clear();
	cin.sync();
	cin.get();
	return 0;
}

void gotoXY(int x, int y) 
{ 
CursorPosition.X = x; 
CursorPosition.Y = y; 
SetConsoleCursorPosition(console,CursorPosition); 
}
Just as a heads up, I recently read that Dev C++ is considered obsolete by the devs.

Here's why:

http://www.cplusplus.com/forum/articles/36896/

You should consider using Code Blocks, I love it so far!
That's an old thread, it may contain obsolete information.

Latest version of the article is here: http://www.cplusplus.com/articles/36vU7k9E/
@ whitenite1: Thanks, works well.

@ Marcan & Chervil: Yeah, I read that article. Might switch over soon.
How do you go back a line?

From a file, or from a terminal? From a file is simple, terminal would be weird. Doesn't even make sense. Going back a line would be editing an already-executed command. You could do something like the up-arrow functionality that is present in most terminals and the windows command prompt.
Last edited on
@ ResidentBuscuit: It was to create a background color by changing the font background and then concatenating spaces to fill the console up. Then I need to go back all the way up and run my main program.
@usandfriends

Here is a more elegant way to change the background color of the console and also of the text. This may help..

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
// Replace Line.cpp : main project file.

#include <iostream>
#include <string>
#include <windows.h>

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

#define on , // So I can use the function - void text(text_color on background_color)
                    // To more easily remember which is text color vs background color

void text(int text_color = 7 on int paper_color = 0)
{
	// defaults to light_gray on black if you just call text(); in program
	int color_total = (text_color+(paper_color*16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );
}

void ClearScreen();
void gotoXY(int, int);

int main()
{
	
	text(dark_gray on light_cyan); //text is dark gray on a light cyan screen
	ClearScreen(); // Clear screen and background becomes light cyan, or whatever
	                       // color you had chosen for text background color in above
	int line=0;
	gotoXY(5,12);
	cout << "1) This is line one";
	gotoXY(5,14);
	cout << "2) This is line two";
	gotoXY(5,16);
	cout << "3) This is line three";
	
	do
	{
		text(dark_gray on light_cyan);
		gotoXY(5,20);
		cout << "Replace which line? ( 1, 2 or 3 )";
		cin >> line;
		if (line<1 || line>3)
		{
			text(light_red on light_cyan);
			gotoXY(5,22);
			cout << "We only have three lines..";
			Sleep(1500);
			gotoXY(5,22);
			cout << "                           ";
		}
	} while ( line<1||line>3);

gotoXY(8,10+(line*2));
cout << "This is a new line..";

gotoXY(5,20);
cout << "   Program finished!!             ";

	cin.clear();
	cin.sync();
	cin.get();
	return 0;
}

void ClearScreen()
  {
   DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
  }

void gotoXY(int x, int y) 
{ 
CursorPosition.X = x; 
CursorPosition.Y = y; 
SetConsoleCursorPosition(console,CursorPosition); 
}
@whitenite1: Thanks!
Topic archived. No new replies allowed.