Need Help

Pages: 12
i need a screen string changer:
cout << "Hello World!";
will result this :
Hello World!
but i need to change the Hello World to I'm Alive(just a example)
how to do that ?
clear the screen and std::cout<<"whatever";

Not 100% sure what you mean, but if you wanted to change output during runtime you can store a name or something in a variable and output its contents like this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include <string>
using namespace std;

int main()
{

	string name;

	cout << "Enter Name:";
	cin >> name;

	cout << "Hello " << name << ", I'm alive!";

	return 0;

}
Enter Name:Michael
Hello Michael, I'm alive!
i think he means print something, then write over the text
Ah right, no bother :)
not that way, i want to change the text in one line no in other, because will be this :
1
2
cout << "Hello World!" << endl;
cout << "I'm Alive";

Hello World! I'm Alive

i want to change with one line.
i tryed this :
1
2
3
4
5
6
7
8
9
string i = "unknown";

while (1<2){
system("cls");
cin >> i;

cout << i << endl;

}

is the WAY but i not make exactly this is more complexy.
i make a Menu, this menu is to change the "Name" of a string pressing Enter, left, right.
but, is flicking, because then write and clean.
how to fix ?
Last edited on
Now you have completely lost me lol :-)

Do you mean when you show "Hello World", you want to show a new cout<<"I am Alive" over the top of it instead of below it?
http://www.binbert.com/blog/wp-content/uploads/2010/09/ping-color.gif
is like this gif.
then write and clear.
my program do this, but very fast, this is flicking.
how to change this ?
ah you mean delayed before next cout...

sleep(1000); // delay 1 second is the easy way from the console.
i can't make the program wait one second, i make a menu and i want, This menu to be very proximity to real time.
and i tryed to make wih 200 not work :(
i think i'll overwrite the text, but how i do that ?
Last edited on
you mean first it will show HELLO, then u click left or right and it will show BYE but on same line and overwrite it?

like a scrolling menu?
Last edited on
It sounds like you need to use the ncurses library.
Well, I think you need to use some Windows API to move the cursor. Here is an example for you:
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
#include <iostream>
#include <windows.h>
#include <stdio.h>
using namespace std;

static HANDLE hout;

void GetReady()
{
	hout= GetStdHandle(STD_OUTPUT_HANDLE);//Get the handle of the standard output
	return;
} 
void gotoxy(unsigned x,unsigned y)//Set cursor Position
{
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(hout,pos);
	return;
}
int main()
{
	GetReady();
	cout<<"Hello, World!";
	system("pause");//Without pause, the program will run too fast for user to see clearly
	gotoxy(0,0);//Return cursor to up-left corner 
//Well, maybe I shoud clean the screen first to make sure no words is uncovered. But here the second sentence is longer than the first, and can cover the first completely.
	cout<<"I'm a Chinese.";
	system("pause");
	return 0;
}


By the way, you turely can use system("cls") instead of my function, but you forgot to use system("pause") , and your program cleaned the screen before you saw it clearly.
What's more, you can use while(1) or while(ture) instead of while(1<2). while(1<2) will make others baffled.
yep is a scroll menu.
ok but there is a problem, i used a while and the "way" of you suggested,
therefore my program is writing all the screen, example :
hello!
hello!
hello!
hello!
...
but now dont flicking but is writing in my entire screen.
EDIT:
i have an idea now, how i can change a text foreground to black ?
if is another color i can use :
SetConsoleTextAttribute(hConsole, FOREGROUND_ ...
but isn't, how can i do this ?
Last edited on
but now dont flicking but is writing in my entire screen.

You mean you want the words to flicking?
how i can change a text foreground to black ?

If you want to show words which both foreground and background is black, then you can just show a space. It looks the same.
i don't want the words flicking !
and, using the example up, is writing im my entire screen, but, if i put a system("cls") back to flick :(

What about this..

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

# include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;

void gotoxy(int x, int y);
int readMenu(int option);

int main() 
{
	
	int option = 0;
	char ch;

	cout << "Use Space bar to select menu option, enter to select" << endl;

	do
	{
		ch = _getch();
		if (ch == ' ')
			option = readMenu(option);

	} while (ch != 13);

}


int readMenu(int option)
{
	option++;
	if (option > 5) option = 1;
	
	switch (option)
	{
	case 1:
		gotoxy(0, 3);
		cout << "Menu Option 1";
		break;
	case 2:
		gotoxy(0, 3);
		cout << "Menu Option 2";
		break;
	case 3:
		gotoxy(0, 3);
		cout << "Menu Option 3";
		break;
	case 4:
		gotoxy(0, 3);
		cout << "Menu Option 4";
		break;
	case 5:
		gotoxy(0, 3);
		cout << "Menu Option 5";
		break;
	}
	return option;
}

void gotoxy(int x, int y)
{
	COORD coord;
	coord.X = x;
	coord.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
whats wrong ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 if( GetAsyncKeyState( VK_NUMPAD0 ) & 0x8000 ){
    int line = 0;
    int stop = 1;
    Imprimir(line, hConsole);
    while (stop == 1){
        if( GetAsyncKeyState( VK_UP ) & 0x8000 ){
            line -= 1;
                if (line < 0){line = 3;}
                Sleep(200);
                Imprimir(line, hConsole);
                Sleep(200);}
            if( GetAsyncKeyState( VK_DOWN ) & 0x8000 ){
                line += 1;
                    if (line > 3){line = 0;}
                    Sleep(200);
                    Imprimir(line, hConsole);
                    Sleep(200);}
i don't want the words flicking !
and, using the example up, is writing im my entire screen, but, if i put a system("cls") back to flick :(

Why it is writing in your entire screen? It only changes the first line.
Well, please describe what you want in detail.
ex. print "Hello, world!" on first line and then change it into "I'm I'm Alive" without flicking after press any key.
On my computer, this code works well. I just don't know what
writing in my entire screen
means.
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
#include <iostream>
#include <windows.h>
#include <stdio.h>
using namespace std;

static HANDLE hout;

void GetReady();//Get the handle of the standard output
void gotoxy(unsigned x,unsigned y);//Set cursor Position
 
int main()
{
	bool wd1=true;
	GetReady();
	
	while(1)
	{
		gotoxy(0,0);
		if(wd1){
			cout<<"Hello, World!"<<endl;
			system("pause");
		}
		else
		{
			cout<<"I'm Alive."<<endl;
			system("pause");
		}
		gotoxy(0,0);
		for(int i=0;i<40;i++){//Clean words that are not covered
			cout<<' ';}
		wd1= !wd1;
	}
	return 0;
}
void GetReady()
{
	hout= GetStdHandle(STD_OUTPUT_HANDLE);//Get the handle of the standard output
	return;
} 
void gotoxy(unsigned x,unsigned y)//Set cursor Position
{
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(hout,pos);
	return;
}

P.S. You want to put the words in other position instead of the first line? Just change gotoxy(0,0) into gotoxy(2,3) etc.
ex.
1
2
3
4
gotoxy(2,3);
cout<<"Hello, World!"<<endl;
gotoxy(2,4);
system("pause");
Last edited on

I am completely lost on what he wants lol.
Pages: 12