I cant move this triangle using keyboard.also a triangle shape is not comming

#include<iostream>
#include<ncurses.h>
//#include<iostream>
using namespace std;
void triangle();
int main()
{
int ch;
int x;
x=20;
initscr();
keypad(stdscr,TRUE);
noecho();

move(5,x);

triangle();
ch =getch();
while(ch !='q')
{
if(ch==KEY_LEFT)
x=x-1;
else if(ch==KEY_RIGHT)
x=x+1;

move(5,x);
triangle();
ch=getch();
}
endwin();
return 0;
}

void triangle()
{
for(int i=0;i<4;i++)
{
for(int j=0;j<i;j++)
cout<<"*";
cout<<endl;
}
}
if you're using curses, you can't use cout to write to the screen. you have to use some thing like printw
The code doesn't even compile on my Linux machine:

1
2
3
4
5
6
7
8
9
10
11
12
13
/tmp/ccGKDZUT.o: In function `main':
t.cpp:(.text+0x10): undefined reference to `initscr'
t.cpp:(.text+0x17): undefined reference to `stdscr'
t.cpp:(.text+0x24): undefined reference to `keypad'
t.cpp:(.text+0x29): undefined reference to `noecho'
t.cpp:(.text+0x38): undefined reference to `move'
t.cpp:(.text+0x44): undefined reference to `stdscr'
t.cpp:(.text+0x4c): undefined reference to `wgetch'
t.cpp:(.text+0x7c): undefined reference to `move'
t.cpp:(.text+0x88): undefined reference to `stdscr'
t.cpp:(.text+0x90): undefined reference to `wgetch'
t.cpp:(.text+0xa3): undefined reference to `endwin'
collect2: ld returned 1 as End-Status 


try

cout << yourvariable << flush;

If that doesn't work change the prinitng routine
ARMinus did you have ncurses installed when you compiled that?
guys bt i called iostream object library.so i can use cout statements...
Even with #include <iosteam> you shouldn't use cout to write to the screen when using curses.
Last edited on
Anyway here is a working example of a triangle shape
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
#include<ncurses.h>
//#include<iostream>
using namespace std;
void triangle();
int x=10;
int y=5;
int main()
{
int ch;
initscr();
keypad(stdscr,TRUE);
noecho();

move(y,x);

triangle();
ch =getch();
while(ch !='q')
{
	switch(ch){
	case KEY_LEFT:
	x--;
	break;
	case KEY_RIGHT:
	x++;
	break;
	case KEY_UP:
	y--;
	break;
	case KEY_DOWN:
	y++;
	break;
	}
//move(y,x);
clear();
triangle();
refresh();
ch=getch();
}
endwin();
return 0;
}

void triangle()
{
	for(int i=0;i<4;i++){
		for(int j=0-i;j<i;j++){
			mvaddch(i+y,x+j,'*');
		}
	}
}
thank u very much.i got it
Topic archived. No new replies allowed.