after the user inputs a char how can a make i randomly return one of several responses? Does entering more than one char work the same way as one?
Printable View
after the user inputs a char how can a make i randomly return one of several responses? Does entering more than one char work the same way as one?
You are going to have to carefully think about the question you are attempting to ask, and then word it in a manner so that we can understand it.
After wrapping my brain around your corkscrew of a question, I think I sort of understand what you're getting at....
To get more than one character as input, use cin.get
example:
As for randomly doing something depending on the input.....look in to the rand() function, and to make sure it's different every time look in to the srand function.Code:char name[512];
cin.get(name,511,'\n');
cin.ignore(512,'\n');
You'll also need to use if statements. I hope you know how to use them.
Your question is posed in such a way that leads me to believe that english may not be your first language. I will now attempt to respond to your inquery based on my own assumptions.
From what I understand, you want a user to input a character, and then return a random response.
Here is some example code for what it's worth:
Code:#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
char input;
cout "Enter a character ";
cin >> input; //this will only accept a single char
cin.ignore(200,"\n"); //ignore basically everying else in the input buffer
srand((unsigned)time(NULL));
switch(rand()%4) //generate a random number from 0 to 3
{
case 0: cout << "I am saying something";
break;
case 1: cout << "This is a random saying";
break;
case 2: cout << "I am saying something that is random";
break;
case 3: cout << "Random stuff";
break;
}
return 0;
}
To answer the second part of your question:
The use of cin.ignore( ) can be used to disregard any extraneous input that exceeds the first valid char (in this case, for up to 200 characters and/or the enter key)Quote:
Does entering more than one char work the same way as one?