Like Tree2Likes
  • 2 Post By grumpy

Problem with a string

This is a discussion on Problem with a string within the C++ Programming forums, part of the General Programming Boards category; I want to make a program in which when a person type " black " on run time, the output ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Problem with a string

    I want to make a program in which when a person type "black" on run time, the output will be one. Can I use switch or any loop for it, if so how? if not what I do?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    Quote Originally Posted by Danishmehmood28
    when a person type "black" on run time, the output will be one.
    What do you mean? The output will be "one" as opposed to "two", "three", etc? The output will be "1"? The output will be something that is black in colour? What happens if the user enters other input, e.g., "white"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    C++ cannot switch on string types. C# can, but not C++.

    In C++ you should just compare std::strings using ==
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37
    Code:
    std::string someString;
    
    while(someString != "black"){
        while(!(std::cin >> someString)) /*Deal with error*/;
        std::cout << "1";
    }

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by ex-mortis View Post
    Code:
    std::string someString;
    
    while(someString != "black"){
        while(!(std::cin >> someString)) /*Deal with error*/;
        std::cout << "1";
    }
    Maybe you should see iMalc's post again.He said that we do not compare strings by operator == but with the function strcmp.Same story for operator != . strcmp will return zero if the strings are equal and a non-zero value if the strings are not equal

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,259
    Quote Originally Posted by std10093 View Post
    Maybe you should see iMalc's post again.He said that we do not compare strings by operator == but with the function strcmp.Same story for operator != .
    iMalc didn't say that. He said that strings cannot be tested using switch statements, and that one should use (since this is a C++ forum, not C) == operators.

    Your comment about using strcmp() is unnecessary (and often invalid) for C++ code that uses C++'s string type. ex-mortis's example is correct because it used that string type, again bearing in mind that this is a C++ forum.

    What you say is valid in C. But C++ provides alternatives that C does not.
    Elysia and Matticus like this.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Quote Originally Posted by grumpy View Post
    iMalc didn't say that. He said that strings cannot be tested using switch statements, and that one should use (since this is a C++ forum, not C) == operators.

    Your comment about using strcmp() is unnecessary (and often invalid) for C++ code that uses C++'s string type. ex-mortis's example is correct because it used that string type, again bearing in mind that this is a C++ forum.

    What you say is valid in C. But C++ provides alternatives that C does not.
    Oh,i am truly sorry.Of course you are right.Thanks for correcting me.!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-10-2011, 05:25 AM
  2. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  3. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 09:29 PM
  4. Replies: 0
    Last Post: 04-05-2003, 08:33 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM

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