Newb help with basic If Statement

This is a discussion on Newb help with basic If Statement within the C++ Programming forums, part of the General Programming Boards category; Okay well i am just experimenting with C++. I am writing a DOS based program and I am having difficulties ...

  1. #1
    diZz™
    Guest

    Newb help with basic If Statement

    Okay well i am just experimenting with C++. I am writing a DOS based program and I am having difficulties getting this if statement to work.

    Code:
    #include <iostream.h>
    
    int main()
    
    {
    char name[30];
    char bob[30]="bob";
    cout<<"Please enter your name: ";
    cin>>name;  
    if (name == bob)  
    {
    cout<<"U Suck"<<endl;
    }
    else
    {
    cout<<"You Rule"<<name<<endl;
    }
    return 0;
    }
    Okay what I want to happen is that when you enter bob it will say "u suck" and if you type in anything else it will say "u rule" and the name typed in.
    Im sure this isnt a hard thing to do so any help is greatly appreciated.

  2. #2
    diZz™
    Guest
    I also forgot to add when i run the program no matter what i type it always says U rule and the name.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    you forgot quotation marks around the "bob"
    i don't think you can compare strings with the equal sign. try strcmp()
    if you want to mess with something easier, research string classes and the stl in general. it provides a string class which you can manipulate much more easily.

  4. #4
    diZz™
    Guest
    can u point me in the dirrection of strcmp()?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    C strings cannot be tested for equality with the == operator, you must use one of the string comparison functions in string.h or write one of your own. Here is your code modified to use strcmp. The array with "bob" was useless since you can check for the string literal directly, so I removed it as well.
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
      char name[30];
      cout<<"Please enter your name: ";
      cin>>name;  
      if ( strcmp ( name, "bob" ) == 0 )  
      {
        cout<<"U Suck"<<endl;
      }
      else
      {
        cout<<"You Rule "<<name<<endl;
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  6. #6
    diZz™
    Guest
    thanx you very much Prelude
    is there any tuts on this strcmp()?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is there any tuts on this strcmp()?
    There is no need for a tutorial, strcmp takes the two strings to compare (they need not be modifiable), and returns an integer greater than, equal to, or less than zero, accordingly as the first string is greater than, equal to, or less than the second.

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

  8. #8
    diZz™
    Guest
    How would i use the strcmp() to compare 2 variables that are strings?

    such as name1 and name2?

  9. #9
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Dangit Prelude, you beat me to it. Now I am gonna do something not nice with that pic of you that I printed out.

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This is a better way:

    Code:
    #include <string>
    using namespace std;
    
    //....
    
    string name,bob = "bob";
    cin >> name;
    if (name == bob)
    {}
    else
    {}
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    Originally posted by Sang-drax
    This is a better way:

    Code:
    #include <string>
    using namespace std;
    
    //....
    
    string name,bob = "bob";
    cin >> name;
    if (name == bob)
    {}
    else
    {}
    Well even though ur way is much straight forward but thats not what he asked for ..... Has he started strings ????..
    Last edited by datainjector; 09-06-2002 at 06:34 AM.
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Well, since char* is C and std::string is C++ why not learn C++ from the beginnning?

    I think one should start by learning std::string and later, when learning pointers, take a look at char*.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
      char word1[80];
      char word2[80];
      char flag = 'y';
    
      while(flag == 'y')
      {
        cout << "enter the first word" << endl;
        cin >> word1;
    
        cout << "enter another word" << endl;
        cin >> word2;
    
        int result = strcmp(word1, word2);
        if(result == 0)
        {
           cout << "the words are identical" << endl;
        }
        else if(result > 0)
        {
           cout << "the first word is "bigger" than the second" << endl;
        }
        else
        { 
           cout << "the first word is "smaller" than the second" << endl;
         }
         cout << "do it again? y/n" << endl;
         cin >> flag;
      }
       return 0;
    }

  14. #14
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    easier, but not as impressive.

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    char * and char[] and STL string class are all C++ (at least the string class is if your compiler is STL compliant, and a lot of compilers being used today aren't, although those being sold are). In fact manipulating char[] and char* are intrinsic to understanding what is happening under the hood with the string class. If all you want to do is drive the car, use the string class. If you want to know what makes a car work, learn how to manipulate char* and char[].

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  3. Extremely basic encryption program
    By CrackerJack in forum C Programming
    Replies: 15
    Last Post: 11-30-2003, 11:45 AM
  4. having trouble understanding this statement
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 10-03-2003, 09:00 AM
  5. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM

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