Thread: string

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    string

    ok how can i get it so it will ask for a string and the string will be and number of char long.
    ex:
    hello or hi
    and have if check the string to do something like this:
    ok you tyeped hello so the program will say "hello to you too"

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you're familiar with the C++ string class, just use cin >> yourString like you would with an int.

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    string user_input;
    
         cout << "Enter a statement: ";
    
         cin >> user_input;
    
         if ("hello" == user_input)  //overloaded <string> class == boolean operator simulates the <cstring> strcmp( ) function
         {
              do stuff here
         }
    
         else if ("hi" == user_input)
         {
              do stuff here
         }
    
         else
         {
              do stuff here
         }


    [edit] moved constants to the left-hand side of the == based on this tip [/edit]
    Last edited by The Brain; 07-09-2005 at 01:02 AM. Reason: put constants on the left hand side to avoid possible assignment errors
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    47
    thanx that was easyer than i thought

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM