storing input

Jul 7, 2011 at 5:04am
How do I store console input for later use? I want the user to pick a letter from a list given i.e choose a,b or c. How do I get this input stored? I want to be able to quit the program if the user enters c. I am using cin >> variable in my code. Thanks for the help in advance!
Jul 7, 2011 at 5:12am
Well, by reading the input into a variable you already have it stored and you can use it at any time from that point on.

1
2
3
char choice;
cin >> choice;
if (choice=='c')return 0;
Jul 7, 2011 at 5:38am
I realize now I asked the wrong question. How does a loop affect that input? For example, get those choices from the user, store them in choice. But if you do a loop, it says 'c' is not declared, though it is already being stored. Sorry if this is confusingly worded...
Jul 7, 2011 at 5:46am
You can refer to variables inside a loop just fine, there's no difference.
The problem is somewhere else, but you'll need to post your code.
Jul 7, 2011 at 6:01am
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{


srand(time(0));

int humanschoice;
cout << "choose [r,p,s,q] " << endl;
cin >> humanschoice;
cin.ignore (1000, 10);
cout << "Human: " << humanschoice << endl; //humanschoice is the input so it should be stored right?

int z = rand() % 3;
if (z == 0)
cout << "Computer: " << "rock " << endl;
if (z == 1)
cout << "Computer: " <<"paper " << endl;
if (z == 2 )
cout << "Computer: " << "scissors " << endl;

return 0;
}

I am just getting started, so it is really choppy and unformatted. As you can most likely tell, it is for hw so hints only please. Thanks again!
Jul 7, 2011 at 6:05am
Yeah, the value is stored in that variable. So what's the problem?
Edit: wait a second. You're prompting the user to type a character, but your variable is of type int (so it can only store integers). Just change its type to char.
Last edited on Jul 7, 2011 at 6:08am
Jul 7, 2011 at 3:45pm
ok that works a lot better. I am now adding a loop, so I added a while true under srand, and close loop bracket after return. My break statment is if (humanschoice == q) break;.

I want the game to quit if human enters q, although I get q is undeclared. Any ideas on why it does not save the input if I add a loop?
Jul 7, 2011 at 4:14pm
If you write q, this refers to a name of e.g. a variable.
What you mean is the character literal q, which is written as 'q'.
Jul 7, 2011 at 5:04pm
that worked! Alas, it was such a simple solution....I will mark this post solved when I am completely done, incase I have any more questions. Thanks again!
Topic archived. No new replies allowed.