PDA

View Full Version : C++ ncurses question




toothyXdip
Apr 16, 2009, 03:48 PM
Ok I would have no problem doing this using cout <<, and cin >>, but I am having trouble doing it with ncurses.


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

int row, col;
char mesg[] = "Whats your name?\n";
char name[50];

void userinput()
{
initscr();
printw("Hey! I know you.");
refresh();
getch();
endwin();
}

int main()
{
initscr();
getmaxyx(stdscr,row,col);
mvprintw(1, (col-strlen(mesg))/2, mesg);
getstr(name);
if (name == "Bob")
{
userinput();
}
refresh();
getch();
endwin();

return 0;
}


It compiles but when I type in the name "Bob" nothing happens. I have a feeling that its not even considering the if statement because it reacts as if it wasn't even there.

Also, when I try to make name a string variable I get the errors:

cpp:7: error: ‘string’ does not name a type
cpp: In function ‘int main()’:
cpp:23: error: ‘name’ was not declared in this scope



kainjow
Apr 16, 2009, 04:17 PM
== here is comparing the pointer values. Use strcmp() which returns 0 if the two strings are equal.

kpua
Apr 16, 2009, 04:38 PM
Also, the string class isn't in the top-level namespace. To switch to using C++ strings, you need to either use "std::string" or "using namespace std;"

Of course, you can't pass a std::string to getstr(). You'd need to pass a buffer (like you do now), then create a std::string with the buffer. If you do that, then you can use ==.

Also, watch out for buffer overflows ;-)

toothyXdip
Apr 16, 2009, 05:55 PM
Sorry for this but I just started learning c++ yesterday so I am just going on online tuts. I'm probably going to order a book so I might put a thread up for recommendations.

I have no problem with someone just pointing me in the direction of a website/online tutorial that will explain the problems I'm having. I did google both errors and things involved but didn't find much. These are most likely very simple problems to solve, so again sorry.

@kainjow
When I change the if statement to:

if (strcmp(name, "Bob") = 0)
{
userinput();
}


I get the error:

invalid lvalue in assignment

and im guessing its because name isn't a string, but I cant make it a string without using a buffer? And if you continue to read you will see I really have no clue on how to use a buffer.

@kpua
I didn't really know what a buffer was, so I Googled it. What I learned was a buffer is a resource which can hold a number of tokens. And even after looking at examples and looking up more one them, I still wasnt sure how to use them with my problem. I don't want to be spoon fed this, so if you could tell me where to start, or ways buffers are used, or link/write an example using buffers I would be very appreciative.

Thanks

kpua
Apr 16, 2009, 06:17 PM
Use ==, not = in your if statement.

The term "buffer" in practice essentially means an array (usually statically allocated). We usually call an array a "buffer" when it's the destination of some data we're receiving. (In this example, we're receiving data from getstr())

The code you posted original had a buffer called "name". If the buffer contains a sequence of characters followed by a null terminator, it can also be called a "C string".

So, when searching tutorials/documentation, understand that "buffers" and "C strings" are often interchangeable.

A task for you (since you don't want to be spoon fed): Find out how to create a C++ string from a C string (or in other words, the 'name' array in your code).