hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : help needed limiting the input in c++
Reply

Forum Jump

help needed limiting the input in c++

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 11-18-2005, 04:47 PM
mat1983 mat1983 is offline
New 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;
Is as far as i've got at the minute. I i know i can limit the stored value by setting input as an array, but it will still allow more than one character to be entered in the console, it just doesnt store the rest.

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.


Sponsored Links
  #2  
Old 11-18-2005, 05:03 PM
Adam-AEC Adam-AEC is offline
Web Hosting Master
 
Join Date: Feb 2003
Location: Canada
Posts: 945
cin.get(variable, length);

  #3  
Old 11-18-2005, 05:30 PM
mat1983 mat1983 is offline
New Member
 
Join Date: Nov 2005
Posts: 3
Quote:
Originally Posted by Adam-AEC
cin.get(variable, length);
ty for the suggestion but i tried using it there now, then displaying the variable aftwards but the output appears to be blank
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;

Sponsored Links
  #4  
Old 11-18-2005, 09:54 PM
tamasrepus tamasrepus is offline
Web 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

  #5  
Old 11-18-2005, 10:44 PM
mat1983 mat1983 is offline
New 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

  #6  
Old 11-18-2005, 10:57 PM
Adam-AEC Adam-AEC is offline
Web Hosting Master
 
Join Date: Feb 2003
Location: Canada
Posts: 945
Quote:
Originally Posted by tamasrepus
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;
  }
}
That's another good suggestion. I never thought of that.

Cheers.

Reply

Related posts from TheWhir.com
Title Type Date Posted
EU Body Seeks Input on How to Make Europe a Trusted Region for Cloud Computing Web Hosting News 2014-03-24 09:44:32
OnApp Releases New WHMCS Module for Service Providers Web Hosting News 2014-01-21 14:04:13
Enterprises Move Off Public Cloud in Favor of Hybrid Cloud: Study Web Hosting News 2013-08-16 10:47:00
White House Plans Cybersecurity Incentives for US Businesses Web Hosting News 2013-08-07 13:08:53
Rackspace Launches OpenStack-Powered Cloud Block Storage Solution Web Hosting News 2012-10-23 10:16:40


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:
WHT Membership
WHT Membership



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?