Archived

This topic is now archived and is closed to further replies.

testing characters

This topic is 5093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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() function


int 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]

Share this post


Link to post
Share on other sites