Results 1 to 6 of 6
-
11-18-2005, 04:47 PM #1New Member
- Join Date
- Nov 2005
- Posts
- 3
help needed limiting the input in c++
Im doing a course in at the minute and have to write a game (rock paper scissors) in c++ using a console, but im having a small problem.
Basically i want to limit the ammount of characters that can be entered
Code:cout <<"Please enter your selection : "<<endl<<endl; cout <<"1 = Rock"<<endl; cout <<"2 = Paper"<<endl; cout <<"3 = Scissors"<<endl<<endl; cin>>input;
I know stopping someone from typing more than one character is more than likely out of the question, but i'd be more than happy to be able to display an error message if more than one character was entered. Can the characters entered be counted?
Anyone got any sugestions at all?Last edited by mat1983; 11-18-2005 at 04:51 PM.
-
11-18-2005, 05:03 PM #2Web Hosting Master
- Join Date
- Feb 2003
- Location
- Canada
- Posts
- 1,010
cin.get(variable, length);
-
11-18-2005, 05:30 PM #3New Member
- Join Date
- Nov 2005
- Posts
- 3
Originally Posted by Adam-AEC
Code:cout <<"Please enter your selection : "<<endl<<endl; cout <<"1 = Rock"<<endl; cout <<"2 = Paper"<<endl; cout <<"3 = Scissors"<<endl<<endl; cin.get(input, 1); cout <<input<<endl;
-
11-18-2005, 09:54 PM #4Web Hosting Master
- Join Date
- Dec 2004
- Location
- New York City, NY, USA
- Posts
- 735
I'm only inferring what you want to do, but if you're only wanting them to be able to enter one 1, 2, or 3, instead of any one character or digit, consider using something like:
Code:bool rpsSelected = false; while (!rpsSelected) { cout << "Please enter your selection :\n\n1 = Rock\n2 = Paper\n3 = Scissors\n" << endl; cin >> input; switch (input) { case 1: cout << "Rock" << endl; rpsSelected = true; break; case 2: cout << "Paper" << endl; rpsSelected = true; break; case 3: cout << "Scissors" << endl; rpsSelected = true; break; default: cout << "Invalid input" << endl; break; } }
Samat Jain | Rhombic Networks, LLC - Partner, CTO
-
11-18-2005, 10:44 PM #5New Member
- Join Date
- Nov 2005
- Posts
- 3
thanks, thats exactally what i was trying to do.
Still no joy though, i can only get "invalid inputs" using that. But its given me what i need. Thanks
-
11-18-2005, 10:57 PM #6Web Hosting Master
- Join Date
- Feb 2003
- Location
- Canada
- Posts
- 1,010
Originally Posted by tamasrepus
Cheers.