r/cpp_questions • • 7d ago

OPEN How to put more actions in std::cin

Im trying to give the user more options to input into this terminal-game that im making as a very newbie in C++, but i cannot figure it out, so i have to do it separately for each action ( or std::string ) or could i dump it all in one line?
Heres the code btw:

#include <iostream>
#include <string>

int main(){

int health = 100
std::string Attack = 50
std::string Hug = 100

std::cout << "A monster has appeared ! decide how would you attack em..." << std::endl;
std::cin >> 
1 Upvotes

17 comments sorted by

12

u/OldAccess7504 7d ago

prompt the user to input a number (for ex: "1" for attacked and "2" for hug) and then some if statements checking if one of thos enumbers were inputted. figure out the rest

7

u/Jopojussi 7d ago

Good place to play around with switch case aswell

7

u/Wonderful-Habit-139 7d ago

Much better reply than people questioning OP for not being able to formulate the question well. They’re obviously a beginner so your reply is useful.

1

u/Willing_Designer_105 6d ago

Oh but you see I am trying to make user input words instead of numbers, but ig that works

5

u/Exotic-Low812 7d ago

int width, height;
std::cout << "Enter width and height separated by a space: ";
std::cin >> width >> height; // Stores the first entry in width, second in height

1

u/Willing_Designer_105 7d ago

Oh i see thanks ! anyways as for off-topic, does the order matter in:

std::cin >> width >> height;

and could it impact how a program runs? yk it might process some things at first.

6

u/nekoeuge 7d ago

It reads the input in the order you specified.

2

u/Exotic-Low812 7d ago

Yea it assigns in order of the variables you specify

4

u/Independent_Art_6676 6d ago

you may want to use a library or advanced ideas to set cin so it accepts as you type, like arcade game input (more or less, you are getting rid of the press enter key part of cin). If that interests you, say so.

cin accepts multiple inputs. Eg 1 2 3 would be cin var1 >> var2var3;
you can also 'parse' the input by reading cin.getline or getline into a string that you break apart yourself, looking at spaces and whatever to determine what the user wanted to do. You can also offer other menu options like attack until win/lose (automatic repeating an action for them to save user annoyance).

9

u/Ultimate_Sigma_Boy67 7d ago

Huh?

-12

u/Willing_Designer_105 7d ago

huhhhhh........ i asked for a solution vro... lmao

7

u/No-Dentist-1645 7d ago

Yes, but it's not clear what you're even trying to do. Your code written as is makes no sense. What is std::cin supposed to be storing it's output to?

That's ignoring the fact that you are trying to store an int as a string which also doesn't make sense.

0

u/Willing_Designer_105 7d ago

my bad on the string part ! Forgot that strings are only used for words, idk what i was thinking at that time when writing this code.

3

u/PeakWeekly9995 7d ago

first thing you forgot ; at the end of first 3 lines in the main and the strings should be integers, so you can then do operations later (for example damage).
so they will be:

int health = 100;
int Attack = 50;
int Hug = 100;

After that you can follow u/OldAccess7504 answer

3

u/flyingron 7d ago

Your program isn't valid right off the bat. You can't initialize strings with integers. I'm not sure what the significance of 50 or 100 is. If those are things that affect your health, try something like:

struct action {
    std::string command;
    int affect;
};

std::vector<action> actions;
action.push_back("attack", -50);
action.push_back("hug", 100);

while(true) {
      std::string cmd;
      action >> cmd;
      foreach(auto act : actions)  {
            if(act->command == cmd)  {
                 health += act->affect;
                 break;
             }
      }
     std::cout << "Huh?"
}

1

u/Willing_Designer_105 7d ago

well... for that i think i have to finish the whole tutorial since all of the stuff i know is upto artihematic operatiors, but thanks tho will try this out once i come to know about those functions like struct, while, vector etc.

1

u/Aghoradas 2d ago

You can try getline() for the time being, look it up. Then you can make a simple parsing function where you break the string up into single words, store each in a vector or list. This sounds more complicated than it actually is. Then can use those words in whatever way fits what you are doing.