Input/Output questions

Hello!

I'm new to programming, and am trying to do what I believe must be a very simple I/O project. I'm trying to have the user input a word, that correlates with a number.. in more detail:

I need to create a function, that does a little math. Based on what activity the user performs, I want it to act a multiplier, and have the output be that multiplied number. More importantly, instead of the user entering the multiplier itself, I want them to input the name of the activity.

So if they enter swimming, that multiplies the number by 5. If they enter hiking, it multiplies the number by 4. If they enter napping, it multiplies it by 0, etc.

It sounds simple enough, but I can't find any information on how to do it!

Thanks!
Well, you've pretty much written the pseudo-code in your post!

if input is "swimming"
    multiply number by 5

otherwise, if input is "hiking"
    multiply number by 4


What you need now is a system for the input itself, and then the checking of the input.

I would suggest using a std::string, since it makes input much easier.

Something like this:
1
2
3
4
5
6
7
8
9
10
11
int number = 5;

string input;
cin >> input;

if(input == "swimming")
    number *= 5;
else if(input == "hiking")
    number *= 4;

...


There's no way to access a variable or function by input, so this is probably the simplest way of achieving the same effect.
Ah I had that, but I left out the quotes! Dang haha, so much to learn. Thanks!!


Lets say I had 25 activities though, would if statements still be the most efficient?
If-else chains are still viable in your situation, especially since the input is using string.
This might help if you're thinking about more efficient options: http://stackoverflow.com/questions/4772325/best-way-to-compare-stdstrings

There are more complex options that you can implement yourself, but they are more for readability than for efficiency.
Topic archived. No new replies allowed.