View Full Version : C++ Beginner Question
Glass_Onion
06-28-2006, 01:47 AM
I've just started teaching myself C++ and have decided to test what I've learnt by writing a couple of pointless little programs. However I've ended up putting something in that I can't figure out how too do. I've tried Google-ing it up and can't find anything that can help me.
Basically what I've got is something along the lines of;
string mystring;
cin >> my string;
if (mystring == "a")
something;
else if (mystring == "b")
something else;
else
yet another thing;
The reason I've not typed the actual code is because I think I'll learn it better if it's done in a method like this than it would if someone just corrected my code. :) I've tried several variations of what's there like = instead of == as well.
rpgfan3233
06-28-2006, 03:59 AM
What is the actual problem? The only error I see in your pseudocode is where you typed
cin >> my■string;
whereas you typed
if (mystring == "b")
everywhere else.
oracleguy
06-28-2006, 05:07 PM
Yeah you didn't actually said what needed to be explained.
sage45
07-04-2006, 05:50 AM
string mystring;
cin >> my string;
if (mystring == "a")
something;
else if (mystring == "b")
something else;
else
yet another thing;
When dealing with code such as this, we can only guess what you are trying to do... To me it looks as if you are trying to see if a user has entered an a or a b or something else...
So you would probably what something like:#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myString
cout << "Please instert a or b to continue: ";
cin >> myString
transform(myString.begin(), myString.end(), myString.begin(), (int(*)(int))toupper);
if (myString == "A")
cout << "You have entered " << myString << endl;
else if (myString == "B")
cout << "You have entered " << myString << endl;
else
cout << "You did not enter A or B, instead you entered " << myString << endl;
return 0;
}
Personally I would use a switch instead of an if...then statement
-saige-
Melon00
07-05-2006, 02:19 PM
Personally I would use a switch instead of an if...then statement
-saige-
How do you do that in C++?
ghell
07-06-2006, 10:51 PM
http://www.google.com/search?q=c%2B%2B+switch&start=0&ie=utf-8&oe=utf-8
:) google is your friend
rpgfan3233
07-06-2006, 11:27 PM
I think Melon00 was referring to something like:
std::string x = "Some random text";
switch (x) {
case "Some random text":
// whatever here
break;
}
You can only use switch with integers and characters AFAIK (no pointer/array variables and no std::string variables):
std::string x = "Some random text";
switch (x.c_str()[0]) {
case 'S':
case 's':
// whatever here
break;
}
It is easier to check for 'S' and 's' than for "SoMe rAnDoM TeXt" and "Some random text" and "sOME RANDOM TEXT" and "some random text" and "SOME RANDOM TEXT" (and other variants, not to mention typos and string length variance), which is probably why that is not implemented.
ghell
07-07-2006, 12:00 AM
you can also use tolower() and check just 's' or whatever :) I don't know if switch internally uses == or anything though as that is overloaded for std::string
i was just pointing out how easy it is to find out how to do a switch-case in c++ without having to ask on a forum ;)
Melon00
07-10-2006, 06:34 PM
I think Melon00 was referring to something like:
std::string x = "Some random text";
switch (x) {
case "Some random text":
// whatever here
break;
}
I was.
I guess I should have been more explicit with my rhetorical question. I personally found it disheartening that a moderator suggested a solution that is not possible. I am assuming it was a mistake.
Also, suggesting someone search Google is insulting to one's intelligence...
ghell
07-10-2006, 07:36 PM
It is perfectly possible in a switch-case but requires a different way of doing it, you may not be able to do it with std::string but characters for example work fine as they are ordinal.
I wasnt insulting your intelligence you asked a simple question which could be found on google, you didnt say you cant do a switch with strings in C++, someone suggested using switch-case instead of an if-else if with strings and you said how do you do it, i pointed out how easy it is to find how you do it in C++. In the examples the strings are all one char long so if the datatype of mystring was char then a switch case would have worked fine. Gees, people post all the time google searches on here but when I do it i get shouted at!
You assume the moderator made a mistake when it could have just been a different way of solving the problem, as the example was generic a switch-case may have been useful in the specific thing they were doing.
rpgfan3233
07-10-2006, 07:54 PM
The code declared "string mystring", with a "using namespace std" outside of main(). This refers to the std::string class and only integers may be used with switch. A char, wchar, int, long int, short int, long long int, tchar, and their variants such as size_t, uint64_t, etc. can be used. floats, doubles, structures, arrays, classes, etc. cannot be used as an argument for switch (<insert type or struct or whatever here> x).
sage45
07-11-2006, 06:10 AM
True... However, in the case that I would have used a switch I would have made the mystring variable a char variant instead of a string variant... I was just posting the code as the original poster asked but then by his own use of the single characters as conditions in his if...then statements, I just said that I would have made it into a switch... Progmattically I never said nor intended to insinuate that a string should be used in a switch statement...
-saige-
Melon00
07-11-2006, 04:10 PM
So you would probably what something like:#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myString
cout << "Please instert a or b to continue: ";
cin >> myString
transform(myString.begin(), myString.end(), myString.begin(), (int(*)(int))toupper);
if (myString == "A")
cout << "You have entered " << myString << endl;
else if (myString == "B")
cout << "You have entered " << myString << endl;
else
cout << "You did not enter A or B, instead you entered " << myString << endl;
return 0;
}
Personally I would use a switch instead of an if...then statement
-saige-
Considering the title of the thread, I would say this suggestion is deceiving. Especially the transform() function. Notice you declared strings, then used them in an else if tree, then made your suggestion to use a switch statement, which cannot be done in this case.
Not a big deal to the seasoned C++ programmer, but might cause some "hair-pulling" to a newbie attempting to resolve compiler errors.
mentalhorse
07-27-2006, 11:19 PM
And you also missed some ";'s".
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.