简体   繁体   中英

cin: No Operator Found Which Takes A Left-Hand Operand

I'm trying to get two space-separated variables and running into this error, as well as [no operator ">>" matches these operands] and [syntax error: ">>"].

#include <iostream>
#include <string>
#include <vector>
using namespace std;

...

void player(char array[][ROWS_AND_COLUMNS], char letter)
{
    bool element_occupied = false;

    do
    {
        int row = 0;
        int column = 0;
        cout << "Enter player " << letter << ", row and column:";
        cin >> inputValidate(row, 1, 3) >> inputValidate(column, 1, 3);

        if (array[(row - 1)][(column - 1)] == '*')
        {
            array[(row - 1)][(column - 1)] = letter;
            element_occupied = true;
        }
        else
        {
            cout << "Sorry, that spot is taken." << endl;
            element_occupied = false;
        }

    } while (element_occupied == false);
}

inputValidate() takes the input, minimum value, and maximum value, and returns the input value if it is 1, 2, or 3.

EDIT:

int inputValidate(int user_number, int lowest, int highest)
{

    while (!(cin >> user_number) || (user_number < lowest || user_number > highest))
    {
        cout << "Error. Enter a number from " << lowest << " to " << highest << ": ";
        cin.clear();
    }

    return user_number;
}

This is a tic-tac-toe game.

inputValidate() returns a temporary int , an rvalue, which operator>> can't read into. It expects an lvalue instead (ie, a variable with a name). That is why you are getting errors on the cin >> inputValidate(...) statement.

Even if the statement could compile, it simply doesn't make sense to use. inputValidate() does its own reading of cin , so player() shouldn't be trying to pass the return value of inputValidate() to cin >> to begin with. inputValidate() would read in a validated number and return it, then player would be trying to read in a new unvalidated number to overwrite it.

For that matter, there is no point in passing in the user_number parameter at all. You are passing it in by value , just to immediately overwrite it with user input. So the value passed in by player() is irrelevant. If you want to return the user's input then user_number should be a local variable inside of inputValidate() . Otherwise, it should be an output parameter passed by reference instead, and inputValidate() 's return value should be void .

Also, when operator>> fails inside of inputValidate() due to invalid input, you are not discarding the input that caused it to fail. cin.clear() only resets the stream's error state, it does not discard any data. You need to call cin.ignore() for that.

With that said, try something more like this:

#include <iostream>
#include <string>
#include <vector>
#include <limits>
using namespace std;

...

int inputValidate(int lowest, int highest)
{
    int user_number;

    do
    {
        if (!(cin >> user_number))
        {
            cout << "Invalid. Enter a valid number: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        else if (user_number < lowest || user_number > highest)
        {
            cout << "Error. Enter a number from " << lowest << " to " << highest << ": ";
        }
        else break;
    }
    while (true);

    return user_number;
}

void player(char array[][ROWS_AND_COLUMNS], char letter)
{
    int row, column;

    do
    {
        cout << "Enter player " << letter << ", row and column:";
        row = inputValidate(1, 3) - 1;
        column = inputValidate(1, 3) - 1;

        if (array[row][column] == '*')
            break;

        cout << "Sorry, that spot is taken." << endl;
    }
    while (true);

    array[row][column] = letter;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM