   comparing character
  I am looking to check users input based on a char, but i am having a bit of trouble comparing the value that the char needs to be vs. the actual input of the user.#include <iostream>using namespace std;struct character{char *name;char charClass;int age;};void main(){character user;char yn;user.name = new char[20];cout << "Please input your characters name:\n " << endl;cin >> user.name;cout << "The name you have chosen is " << user.name << endl;cout << "Are you sure about the character's name?[Y/N]" << endl;cin >> yn;if (yn == 'n' || 'N'){cout <<"hello" << endl;}}in the if statement, it responds to any char input, not N or n. thanks in advance.[Edited by - ForgottenFate on September 11, 2007 4:18:48 PM]
  (yn == 'n' || 'N') should be(yn == 'n' || yn == 'N')   ;  Might I suggest using strings to solve your problem? (I take it you're using C++ and not C)struct character{char *name;char charClass;int age;};becomes...struct character{string name;char charClass;int age;};and to get input for the name, use cin >>, or if the user can enter spaces, use the function getline.cout << "Please input your characters name:\n " << endl;   cin >> user.name;   // OR getline(cin, user.name);cout << "The name you have chosen is " << user.name << endl;To use strings, you'll need to #include <string>. The string class is in the std namespace, so you'll need to preface it with std:: unless you use the using statement like you have.Also, the if statement at the end of your code doesn't do what you think it does. You cannot compare a variable to two values like that. Change it to this.if (yn == 'n' || yn == 'N')   ;  A couple of things that you may want to reconsider here. First of all, judging by the inclusion of iostream I assume you are programming in C++. If this is true, is there any particular reason why you don't use std::string instead of char pointers?You are using the >> operator to stream characters into the user.name character array. You should be using cin.getline( user.name, 20 ); here.Furthermore, the following line of code does not work:if (yn == 'n' || 'N')While it is safe for you (a human) to assume that you're still comparing 'N' to yn, a compiler doesn't think that way. You have to tell it explicitly what you are comparing it against, thus, it would look like: if ( yn == 'n' || yn == 'N' )And last but not least: you are forgetting to deallocate the memory you allocated in this line: user.name = new char[20];. Before exiting main(), you should add: delete [] user.name;The code would then look like:#include <iostream>using namespace std;struct character{char *name;char charClass;int age;};int main( int args, char **argv ){character user;char yn;user.name = new char[20];cout << "Please input your characters name:\n " << endl;cin.getline( user.name, 20 );cout << "The name you have chosen is " << user.name << endl;cout << "Are you sure about the character's name?[Y/N]" << endl;cin >> yn;if ( yn == 'n' || yn == 'N'){cout <<"hello" << endl;}  delete [] user.name;   return 0;};Quote:edit: the code tags dont workTry [ source ], [ /source ] tags (no spaces)   ;  wow, that was fast.i really appreciate the help. I was unaware of the cin.getline function. I happy that it was brought to my attention, and my forgetting to deallocate the memory from the pointer was just a careless mistake. thanks again   ;Quote:...and my forgetting to deallocate the memory from the pointer was just a careless mistake.Sure - but it's a 'careless mistake' that's very easy to make, even for experienced developers. Which, in turn, is one of the reasons why experienced developers tend to prefer std::string over raw character arrays (and RAII containers over manual memory management in general).In short, use std::string for strings in C++ unless you have a specific reason not to.   ;Quote:Original post by jykQuote:...and my forgetting to deallocate the memory from the pointer was just a careless mistake.Sure - but it's a 'careless mistake' that's very easy to make, even for experienced developers. Which, in turn, is one of the reasons why experienced developers tend to prefer std::string over raw character arrays (and RAII containers over manual memory management in general).In short, use std::string for strings in C++ unless you have a specific reason not to.... with std::string, you will want the free function std::getline, instead of the cin.getline member function. This avoids having to worry about a buffer size, of course :)std::string name;std::getline(std::cin, name);