Thread: Username[50] == "Test" QUESTION

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    145

    Username[50] == "Test" QUESTION

    Lets say I had just created a
    Code:
    char Username[50];
    And then the user was asked for their name, which I put Username[50] in.
    Code:
    getline(Username, 50);
    Based on what their Username is, different things will show up.
    Code:
    if (Username[50] == "Test")
    {
    // do something
    }
    The problem I have having, and would like to get fixed, is the if part. What do I do there? Do I do what I did, with the [50] by Username, or do I just do
    Code:
    Username == "Test"
    Because right now it doesn't register what the name entered is.
    Thanks alot for any help. Please, please help me out here.
    "Um...well..."
    -Kyoto Oshiro

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    	char Name[]="Traveller";
    	char mychar[50];
    
    	cout<<"Enter a name:";
    	cin.getline(mychar,50);
    	if(!strcmp(Name,mychar)){
    		cout<<"equal";}
    	else
    		cout<<"not equal";
    	cin.get();
    	return(0);
    }

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    145

    Talking Got it

    Oh, alright, so I just create another character array and the compare those? I understand. Thanks for the help
    "Um...well..."
    -Kyoto Oshiro

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    	char Name[]="Traveller";
    	char mychar[50];
    
    	cout<<"Enter a name:";
    	cin.getline(mychar,50);
    	if(!strcmp("Traveller",mychar)){
    		cout<<"equal";}
    	else
    		cout<<"not equal";
    	cin.get();
    	return(0);
    }
    This will work too, the key is using the strcmp function as opposed to ==.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    145

    Very useful

    Oh, I see. Thanks for telling me that, by not having to create a bunch of arrays, I can just put the name it is supposed to be in "". Thanks again
    "Um...well..."
    -Kyoto Oshiro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe!
    By Sinensis in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 04:40 PM
  2. Tic Tac Toe Program Question
    By Unknowntoyou000 in forum C++ Programming
    Replies: 24
    Last Post: 04-10-2008, 08:55 PM
  3. Tic Tac Toe program...
    By Kross7 in forum C++ Programming
    Replies: 12
    Last Post: 04-12-2007, 03:25 PM
  4. Need an extra pair of eyes ....
    By Whoputthatthere in forum C Programming
    Replies: 5
    Last Post: 06-02-2006, 12:19 PM
  5. help with tictactoe
    By theman29 in forum C Programming
    Replies: 11
    Last Post: 04-27-2006, 05:22 PM