Thread: Problem with a string

  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
    28,413
    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"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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,694
    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,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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, 10:29 PM
  4. Replies: 0
    Last Post: 04-05-2003, 09: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