Strings.......help

This is a discussion on Strings.......help within the C++ Programming forums, part of the General Programming Boards category; I want to have a little thing that prompts the user to enter a frase........like the user enters "I Love ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Strings.......help

    I want to have a little thing that prompts the user to enter a frase........like
    the user enters "I Love You"
    and then have something that prints out something depending on the message printed by the user

    if (message=="I Love You")
    {
    blah blah blah
    }


    how could I do this?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    39

    ascii values

    well,u can use ascii values of the input string and then compare it with any switch case value or using if statement as u did,if u have specific input string choices.
    once a kohatian,always a kohatian!

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You can't compare strings with the == operator, you must use strcmp() or something similar.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Originally posted by Magos
    You can't compare strings with the == operator, you must use strcmp() or something similar.


    Well only works when I enter one word with the == operator, how is the strcmp() used? My book doesn't have it.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Borland C++ 5.0 Programmer's Guide

    Syntax

    #include <string.h>
    int strcmp(const char *s1, const char *s2);

    Description

    Compares one string to another.
    strcmp performs an unsigned comparison of s1 to s2, starting with the first character in each string and continuing with subsequent characters until the corresponding characters differ or until the end of the strings is reached.

    Return Value

    If s1 is... strcmp returns a value that is...

    less than s2 < 0
    the same as s2 == 0
    greater than s2 > 0
    Simple example:
    Code:
    bool CompareStringWithHello(char* String)
    {
       if(strcmp(String, "Hello")==0) return true;
       else return false;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    try this, hope it helps.

    Code:
    #include <iostream.h>
    
    int main(void)
    {
      const int MAX_BUFFER=256, NUM_MSGS=2;
    
      const char Messages[2][20]={{"I Love you"},{"bla bla"}};
    
      char Buffer[MAX_BUFFER];
    
      cout << "Please enter something: ";
      cin.getline(Buffer,MAX_BUFFER);
    
      for(int i=0; i < NUM_MSGS; i++)
      {
        if(strcmp(Buffer,Messages[i])==0)
        {
          cout << "You entered: " << Buffer;
          i=NUM_MSGS; //Exit loop
        }
        else
        {
          if(i==NUM_MSGS-1)
          cout << "No match found.";
        }
      }
    
      getchar();
    }

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Thanks guys for your help, this seems to work
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string>
    
    int main()
    {
    
    
    char s1[80];
    cout<<"Enter something\n";
    cin.get(s1, 80);
    
    
    
    
    //if it's false
    if ( strcmp( s1, "how are you")  )/*this seems to check if it's false for some reason*/
    
    
    {//print this
       cout<<"Wrong\n";
    }
    
    
    
    else
    {
        cout<<"yeah baby\n";
    }
    
    
         
          return 0;
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    oh this is why.....


    RETURN VALUES
    The function strcmp() returns a positive integer if string s1 is lexically greater than string s2; zero if the two strings are identical; and a negative integer if string s1 is lexically less than string s2.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

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, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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