ncurses, how to print out a string from input

hi all,

I'm having trouble finding out how to input a string. This is what i've got so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<ncurses.h>
#include<string.h>

int main(){
	initscr();
	printw("enter the secret password: ");

	char password = getstr(password);	//declare password is a string

	if (password == "opensesame")		
		printw("that is the corrent password");
	else if (password != "opensesame")
		printw("incorrect");
	else 
		printw("error");
endwin();
}
}


My compiler is giving me these errors:

1
2
3
4
5
secret_password.cpp: In function ‘int main()’:
secret_password.cpp:8:17: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
/usr/include/ncurses.h:793:28: error:   initialising argument 2 of ‘int wgetnstr(WINDOW*, char*, int)’ [-fpermissive]
secret_password.cpp:9:17: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
secret_password.cpp:11:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]


thanks in advance,
Last edited on
You should read up on the documentation of ncurses first.

Futhermore, please use std::string when comparing strings like that; otherwise strcmp. The else if is pointless, just use else.
And if you want that password to be secure, don't store it plaintext, instead store the hash (sha1 for instance), create the hash for the input, then compare the input hash with the password hash.

I'll leave those open for you to figure out. For sha1, try crypt++. There are some other encryption libraries out there, that provide sha1, md5 and many more.
thanks errcheck, but I am just learning to code. after reading the manual i tried to test out if i could actually write a simple program using what i just read. this is just an example program. what i found was i dont even know how to do something as simple as get a string and print it out with ncurses (with iostream it is cin and cout). so i was wondering if anyone could show me how to write a string into a variable and print it out using ncurses.

i found this: http://www.cplusplus.com/articles/4z18T05o/

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
#include <curses.h>

int main()
  {
  char users_name[ 100 ];

  initscr();
  (void)echo();

  addstr( "What is your name> " );
  refresh();
  getnstr( users_name, sizeof( users_name ) - 1 );

  /* Here is where we clear the screen.                  */
  /* (Remember, when using Curses, no change will appear */
  /* on the screen until <b>refresh</b>() is called.     */
  clear();

  printw( "Greetings and salutations %s!\n", users_name );
  refresh();

  printw( "\n\n\nPress ENTER to quit." );
  refresh();
  getnstr( users_name, sizeof( users_name ) - 1 );

  endwin();
  return 0;
  }  


this is what i wanted to achieve. i got it to run but i dont understand a couple of things:

(void)echo(); // what does this do?

char users_name[ 100 ]; //what does the square braces mean?

getnstr( users_name, sizeof( users_name ) - 1 ); // getnstr is not in the documentation???

i am referring to: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

is this the official documentation?

Last edited on
I realised that there really isn't any good tutorials for learning ncurses except for: Dan Gookin's Programmer's Guide to Ncurses. I highly recommend this book because it has answered my question on how to print strings in ncurses. http://www.amazon.com/Programmers-Guide-NCurses-Dan-Gookin/dp/0470107596

This is where I should've started.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include
<ncurses.h>
int main(void)
{
char first[24];
char last[32];
initscr();

addstr(“What is your first name? “);
refresh();
getstr(first);

addstr(“What is your last name? “);
refresh();
getstr(last);

printw(“Pleased to meet you, %s %s!”,first,last);
refresh();
getch();

endwin();
return 0;
}


I get everthing in this piece of code except for why void is written inside the int main parenthesis.

Thanks anyway errcheck.
Last edited on
Topic archived. No new replies allowed.