Thread: trun string input into if output

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    155

    trun string input into if output

    Hi, I need help on some colde, when I type anything in it just prints out "hi" when the input wasnt "hi" ~> so on.
    Code:
    #include "tt.h"
    
    using namespace std;
    
    int main()
    {
    string say;
    
    cout << "Naby-> Hi, my name is Naby." <<endl;
    getline(cin, say);
        if(say == "HI"||"Hi"||"hi"||"Hello"||"hello"||"HOLA"||"Hola"||"hola")
            cout<<"Naby-> Hi"<<endl;
        else if(say == "Play music for me")
            mciSendString ( "Play CL.mp3", 0, 0, 0 ); 
        else
            cout<<"Naby-> Finde don't talk with me"<<endl;
            getchar();
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    if(say == "HI"||"Hi"||"hi"||"Hello"||"hello"||"HOLA"||"Hola"||"hola")
    "Hola"||"hola" etc is always true. Compare say with each individually.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    So I should show them individually?
    Code:
    else if(say = bla bla)
    Last edited by adr; 01-27-2006 at 07:21 PM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    He means like this:

    Code:
    if(say == "Hi" || say == "Hola")
    Just the word "Hola" is as good as saying not zero... which is true.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    Oh ok, I get it now, Thanks all.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you may want to look into creating something to capitalize your input to help you out...
    Code:
    #include <iostream>
    #include <cstring>
    
    char*capitalize(char*in);	//warning: this changes the input string
    
    int main()
    {
    	char*buffer=new char[1024];
    
    	std::cout<<"Enter a word: ";
    	std::cin.getline(buffer,1024,'\n');
    	
    	if(!strcmp(capitalize(buffer),"HELLO"))
    	{
    		std::cout<<"Why Hello There";
    	}
    	else
    	{
    		std::cout<<"No Hello first?";
    	}
    	
    	std::cout<<std::endl;
    	delete[]buffer;
    	return 0;
    }
    
    char*capitalize(char*in)
    {
    	short int len=strlen(in);
    	for(register short int i=0;i<len;i++)
    	{
    		in[i]=toupper(in[i]);
    	}
    	return in;
    }
    This is using char*'s, but it'd be a quick easy change to convert it to stl strings.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    toupper() is in <cctype>, not <cstring>.

    And adr, getchar() is a C function, in <cstdio>. I don't know if you've included this or not since you didn't post tt.h.

    [edit]
    Code:
    short int len=strlen(in);
    And strlen() returns a size_t. I think size_t is typedefed in <cstdio> or <cstddef>.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, strlen is from <cstring>, and if it returns a type, that type must have been defined, so therefore if you've included strlen(), you must have indirectly included size_t as well
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, but the fact remains that you tried to stuff a size_t into a short int.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 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. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  4. Replies: 4
    Last Post: 04-03-2007, 05:57 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM