Thread: String Problem

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    Red face String Problem

    I have a bit of a problem with this:
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    cout << "\nEnter a number: ";
    char name[50];
    cin.getline(name, 50, '\n');
    strcmp(name, "Dean");
    if (name == "Dean")
    	cout << "\nThat's my name too!";
    else
    	cout << "\nThat's a nice name!";
    return 0;
    }
    The problem is: no matter what I type in, it prints out the else statement.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    You can't compare c-strings using ==
    You need to put your call to strcmp inside the if statement, and check if it returns 0;
    if (strcmp(name, "Dean") == 0)

    OR you could use C++ strings, they'll save you a lot of trouble
    Code:
    #include <iostream>
    #include <string>
    
    //...
    
    std::string name;
    getline(std::cin, name);
    if (name == "Dean)
    // ...
    else
    //...

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    The problem is: no matter what I type in, it prints out the else statement.
    Ok im pretty sure that strcmp returns either 0(true) or 1(false) so try this instead

    Code:
    if (strcmp(name, "Dean") == 0)
        ...
    and wouldnt it be enter a name, not enter a number =\.
    ~fin

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    if (strcmp(name, "Dean") == 0)
    wow i didnt notice he already said that, my bad Eibro
    ~fin

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: String Problem

    They are right, you should use strcmp(), but I was wondering why did you do this:
    Code:
    strcmp(name, "Dean");
    if (name == "Dean")
    I cann't get why.
    none...

  6. #6
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    using strcmp...

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    cout << "\nEnter a number: ";
    char name[50];
    cin.getline(name, 50, '\n');
    if(!strcmp(name, "Dean")
    	cout << "\nThat's my name too!";
    else
    	cout << "\nThat's a nice name!";
    return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM