String input

Is there anyway you could use a string which the user gives, and use that string to determine what function executes?

So like a simple menu would usually use a single char as an option, like:
"A) for A
B) for B
cin >> choice;

Where choice is a char

is there a way to do it so instead of the user entering just 'a' or 'A' he/she would enter "AddA" or "AddB"

cin >> string;
tolower(string);
if (string == 'adda')

??

Yeah. It's a called a switch statement. Syntax:

switch(choice){
case 1: " blah";
break;
//other cases
default: "blah";
};

cases can be
case 'a': //stuff u want to do
break;
Last edited on
Don't think you are understanding my question so please read through it carefully (there is not that much to read)

I am asking for a user to input a STRING not a single char to determine a choice, and for a condition to compare a whole string

so if I use switch

switch(string){
case "AddA":
case "AddB":

but this doesn't seem right
try it. I'm pretty sure it works
@Zarman. Switch-cases do not work with Strings. You can google that.

@Alex067. You can do it just like you would do it with chars or integers, but instead with strings.

1
2
3
4
5
6
7
8
9
10
11
string choice;
cin >> choice; // use std::getline if you want to have spaces between words

if(choice == "Hello")
{
    cout << "from the other siiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiide?" << endl;
}
else if(choice == "I must") // this one can't be true because there is a white space and you're using std::cin
{
    cout << "Have called a thousand tiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiimes" << endl;
}


Last edited on
@Alex067

std::tolower work s with a char, not a string.
So if you can compare them to literals, I would simply have to just convert the whole string to a lower or upper case and compare it

Is there a lower and upper function for strings?

Also a side question:

If instead of a string, can I use char *choice;
Ok, yeah I never really got the point of char pointers because you could access a string a similar way with indexing right?

string choice;
choice[0]
choice[1]

Or am I completely wrong hahaa
Yes you can do that with strings too. Not to mention strings got lots of great and useful functions, it keeps it's own size, and it's a lot safer. If you're programming c++ there is really no need to use char pointer.
Is there a lower and upper function for strings?


One can loop through the string using it's index just like an array.

I found this with my Google foo :+) You should try your Google foo, too :+)

http://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case


If instead of a string, can I use char *choice;


You can but string is better:
http://www.cplusplus.com/forum/beginner/183276/#msg897204

Using strings like you propose is often error prone, mainly from when the user spells the option incorrectly. You could have a menu with numerical options, and use a switch.

I am guessing you want to call different functions, one can do this with a std::map of function pointers, here is an example from Thomas1965:

http://www.cplusplus.com/forum/beginner/183042/#msg896313
Topic archived. No new replies allowed.