Results 1 to 6 of 6
  1. #1

    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.

  2. #2
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    951
    cin.get(variable, length);

  3. #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;

  4. #4
    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;
      }
    }

  5. #5
    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. #6
    Join Date
    Feb 2003
    Location
    Canada
    Posts
    951
    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.

Related Posts from theWHIR.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •