Programming simple user commands

Hi, I am a begginner at c++ programming and have been trying to create a simple texted based game, nothing too fancy. I have made a few other programs such as "guess my number" and an average finder. What I have been trying to do is create simple user-commands in my game, or at least incorporate the ability for the user to use actual words, like "yes" and "no" instead of just y and n. If possible, I'd like to be able to program a way for a player to type "instructions", or, "help", and have text pop up. I have been trying to use strings to do these kinds of things and haven't had any luck. If someone could help me out, I'd really appreciate it. If someone wants, I can post one of my failed code trials.

Thanks,

Ryman-
What game exactly are you trying to make ?

And yes your code trials will be very useful .


If possible, I'd like to be able to program a way for a player to type "instructions", or, "help", and have text pop up. I have been trying to use strings to do these kinds of things and haven't had any luck.


Did you try cin.getline() for this purpose ?
1
2
3
4
5
6
7
8
9
#include <string>
#include <iostream>

int main()
{
   std::string x;
   std::cin >> x;
   std::cout << x;
}
Last edited on
adesh:

I am trying to make a sort of puzzle game, in which the user is asked a series of riddles and has to enter a one word answer for each.

Here is a sample of my code from the beginning of the game:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstring>
using namespace std;

int main() {

char answer1; 
char str1 = 'yes';
char str2 = 'no';
char name[100];

cout << "Welcome to the 'Riddle Game'" << endl;
cout << "Ready to begin?" << endl;
cin >> answer1;

if(answer1 == str1) {                                     //All this compiles
cout << "Great! Let's Go!" << endl; }                      //but gets skipped 
if(answer1 == str2) {                                     //when the program is run.
cout << "Too bad! We're starting anyway." << endl; }    //For instance, if user 
                                                      //enters "yes", none of the 
cout << "So, what's your name?"                         //"if" text is shown, 
cin >> name;                                            //and their name is now "es".
cout << "Okay, " <<  name << ", let's have some fun." << endl;

system("pause");
return 0;
}


Also, I have not tried "cin.getline()" yet, but I will give it a shot.
A character stores a single character.

1
2
char example1 = 'x'; //that is all it can store, you can't store anymore info in example1
char example2 = 'Excelsior!' //example 2 stores ONLY !, the rest of it was too long to fit into the single character 


Where do the rest of the characters go? Not into memory, that's where. You would need to declare it as an array like you did on line 10, or, better yet, use the string class, that was designed to hold strings.
Last edited on
Topic archived. No new replies allowed.