testing characters

Started by
1 comment, last by TheOne1 20 years, 3 months ago
I was wondering how you would test for characters. For example (in pseudocode): if persons name equals Jane, print "Welcome Jane" else print "Access denied." In code, I tried this, but it doesn't work cause I can't convert an int to char?

#include <iostream>

using namespace std;

int main()
{
   char name[50];
   
   cout << "Enter your name: ";
   cin.getline(name, 49);


   if ( name == //what do I put here? )

      cout << "Welcome " << name << "\n";
   else
      cout << "Access denied.\n";

   return 0;
} 
=================================================== Me: So do you know any computer languages? Him: Ummmm.......yeah, I used to know l337 talk. Me: lol, um okkkkkkkkk. Send the noobs over to me:|[LINK REV="Starting a MSVC++6.0 console projectHREF="www.rainent.netfirms.com/rainsoftstartproject.html"|/LINK] [edited by - TheOne1 on January 5, 2004 2:37:51 AM] [edited by - TheOne1 on January 5, 2004 2:38:14 AM]
----Me: So do you know any computer languages?Friend: Ummm....yeah, I used to know l337 talk.Me: ok....
Advertisement
Use a std::string instead of a char[].

#include <iostream>#include <string>using namespace std;int main(){         string name;      cout << "Enter your name: ";      cin >> name;         if ( name == "name" )          cout << "Welcome " << name << "\n";         else          cout << "Access denied.\n";   return 0;} 
You don't have to use the string class, but you can as the post above me says. You can use some functions of the cstring library.

#include <iostream>using namespace std;#include <cstring> //Necessary to use strcmp() functionint main(){     char name[ 50 ];     cout << "Enter your name:";     cin.getline( name, 49 );     if( strcmp( name, "Jane" ) == 0 )          cout << "Welcome " << name << "\n";     else          cout << "Access denied.\n";     return 0;}


[edited by - PoLiSh_Peta on January 5, 2004 3:10:53 AM]
There are two inevitabilities in life: death and failure.-PoLiSh

This topic is closed to new replies.

Advertisement