Archived

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

TheOne1

testing characters

Recommended Posts

TheOne1    156
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
invective    118
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
PoLiSh_Peta    100
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