if statements with char variables

This is a discussion on if statements with char variables within the C Programming forums, part of the General Programming Boards category; #include <iostream.h> int main() { char name[100]; cout<<"Please input your name: "; cin>>name; if(name=="bob") { cout<<"You are bob"; } else ...

  1. #1
    Unregistered
    Guest

    if statements with char variables

    #include <iostream.h>
    int main()
    {
    char name[100];
    cout<<"Please input your name: ";
    cin>>name;
    if(name=="bob")
    {
    cout<<"You are bob";
    }
    else if(name=="bobby")
    {
    cout<<"You are bobby";
    }
    else
    {
    cout<<"You are not bob";
    }
    return 0;
    }

    it lets you enter your name but cuts out as soon as oyu hit enter, it never delivers any of the if statement text.
    im abviously a n00b, so thanks for any help you all give.
    bax

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >if(name=="bob")

    //Change this to:

    if(strcmp(name,"bob") == 0)

    You might have to include <string.h> at the top.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
      string name;
      cout<<"Please input your name: ";
      cin>>name;
      if(name=="bob") {
        cout<<"You are bob";
      }
      else if(name=="bobby") {
        cout<<"You are bobby";
      }
      else {
        cout<<"You are not bob";
      }
      return 0;
    }
    And please post C++ questions in the C++ board, not the C board. They are two different languages.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    thanks alot for the help everyone.
    bax

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    if(strcmp(name,"bob") == 0)

    You might have to include <string.h> at the top.
    That's too much work. You should do this:

    if( name[0] && name[0] == 'b' && name[0] && name[0] == 'o' && name[0] && name[0] == 'b' )

    Heh..

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if( name[0] && name[0] == 'b' && name[0] && name[0] == 'o' && name[0] && name[0] == 'b' )
    No no no, you're not really programming unless you use hex and pointers. No sissy array notation for the pros
    Code:
    if ( *(name + 0) != 0x00 && *(name + 0) == 0x62 && 
         *(name + 1) != 0x00 && *(name + 1) == 0x6F &&
         *(name + 2) != 0x00 && *(name + 2) == 0x62 ){
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    hehe, you dudes are just too funny!!! I would try to top you, but don't think I stand a chance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 03:25 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21