Thread: if () commands with char's

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    7

    if () commands with char's

    Code:
    int main()
    {
    
      char Name;
    
      cout << "Your Name: " << endl;
      cin  >> Name;
    
        if (Name = ChrisJ)
          {
            system ("cls");
            cout << "Your Name: " << Name "." << endl;
          }
    
    }

    I am trying to see if Name = ChrisJ. How do I do it? Whenever I do it like this I get "ChrisJ undeclared (first use this function)".

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    There are a lot of problems with your code. a) Name is a single character. If you wanted to make it an array, you would've done: char Name[100]; b) The = operator is assignment, not testing for equality. You need to use == for equality. c) ChrisJ is undefined. Who knows what it is? If it was supposed to be a string, you sure left out the quotes...

    Ok, we're gonna do it with std::strings because in most cases, they are easier to use than c-strings. Btw, if you cin a string, it will only retrieve the first word. You need to use getline() which you can find in your documentation/google/board if you want to get more than one word from the cin stream.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string Name;
    
        cout << "Your Name: " << endl;
        cin  >> Name;
    
        if (Name == "ChrisJ")
        {
            system ("cls");
            cout << "Your Name: " << Name "." << endl;
        }
    
        return 0;
    }

  3. #3
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    I think you want to do:

    if (Name == "ChrisJ")

    1. The way you have it now, the compiler thinks ChrisJ is the name of a variable, just like Name is a variable. To tell the compiler it's a string, use quote marks.

    2. Notice the double equal sign -- One equal sign would set Name equal to ChrisJ, but two equal signs compares the value of Name with ChrisJ without changing the value of Name.

    Also, right now Name is a char type, so it will only hold one letter. You will need to make a string or a char array. There's probably more info on strings and char arrays in the FAQ and Tutorials on this site.

    EDIT: Looks like this is a popular thread, Speedy5 and Casey were typing at the same time as me! You can see that Speedy used strings and Casey used char arrays, so you can compare the two!
    Last edited by codegirl; 06-27-2003 at 10:08 AM.
    My programs don't have bugs, they just develop random features.

  4. #4
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    Code:
    #include <string.h>
    
    int main()
    {
    
      char Name[100];
    
      cout << "Your Name: " << endl;
      cin  >> Name;
    
        if (strcmp(Name, "ChrisJ") == 0)
          {
            system ("cls");
            cout << "Your Name: " << Name << "." << endl;
          }
    
    }

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    7
    Okay, I did what you said, and it runs fine, until I put in "New Game" it doesn't display the text is just quits. Here is my code:

    EDIT: Okay, fixed.
    Last edited by ChrisJ; 06-27-2003 at 10:39 AM.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You're off to a good start. Now I'm going to quote myself.
    Originally posted by Speedy5
    Btw, if you cin a string, it will only retrieve the first word. You need to use getline() which you can find in your documentation/google/board if you want to get more than one word from the cin stream.
    And it looks like you're going to be needing arrays. Instead of:
    Code:
    string Main_Menu_Command_001;
    string Main_Menu_Command_002;
    string Main_Menu_Command_003;
    You can do this:
    Code:
    string Main_Menu_Command[3];
    Main_Menu_Command[0] = "...";

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    EDIT: Here's a good quick reference to the std::string class. Just ignore the note on the bottom: http://www.bgsu.edu/departments/comp...cs/string.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-27-2007, 12:48 PM
  2. Bluetooth Dial-up network profile with AT commands
    By BrownB in forum Networking/Device Communication
    Replies: 0
    Last Post: 06-29-2005, 02:47 AM
  3. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM