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?
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?
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"?Originally Posted by Danishmehmood28
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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"
Code:std::string someString; while(someString != "black"){ while(!(std::cin >> someString)) /*Deal with error*/; std::cout << "1"; }
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.