Thread: using strings

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    using strings

    I made a program where you input a string
    then I made and if statement which will display a certain output depending on what was entered in the string but there is something wrong with the if statement can someone please tell me how to use and if statement to read a string I am not sure if I have to pass the whole string and if I do how exactly would I do that
    I use visualC++ 6.0
    thanx for any help

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    So you are evaluating a string that has been entered by the user?

    But you have not said what you are evaluating......length?.....comparing to another string?.....


    The if statement is dependant on the evaluation needed.

    For instance if you wanted to check one string against another you would use strcmp(), to see how big the string was you could use strlen()................

    It depends on what you are trying to do.....

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb Here...

    I think this is what you mean:

    char name[200]; // name of string

    cin >> name;

    cout << "\n\n";

    if(name=="Hi")
    {
    cout << "Hello to you, too!";
    }
    else if(name=="Bye")
    {
    cout << "See ya later!";
    }

    Is that what you mean?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Woops, you cant compare strings like that.....

    Code:
    char name[200]; // name of string 
    
    cin >> name; 
    
    cout << "\n\n"; 
    
    if(!strcmp("Hi",name)) 
    { 
    cout << "Hello to you, too!"; 
    } 
    else if(!strcmp("Bye",name)) 
    { 
    cout << "See ya later!"; 
    }

  5. #5
    Both programs do the same thing

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    No, Fordy is right, with character arrays you have to use the functions from string.h. Your cannot compare them with the == operator.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Thanx

    Yea I needed to compare the string to the condition in the if statement
    thanx for your help

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    also wut does strcmp do exactly

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    strcmp....

    compares 2 strings and returns 0 if they are identical.

    Also returns < 0 if string 1 is less than string 2, and > 0 if otherwise....

  10. #10
    Unregistered
    Guest

    Question Strings

    I use gnu C++ in Emacs, and they allow use of standard operators with string variables. I don't use define strings as char arrays, but I use the string library to define the variable type. I know it works. Is it a question of propriety? I am in a c++ class, and the teahcer tols us to use the standard operators. Is this incorrect? Is it a library issue, or syntax?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM