vector<string> guess(4);
		
	

		cout << "Input a Guess(e.g red blue green yellow): ";
		
        cin >> guess[0] >> guess[1] >> guess[2] >> guess[3];

this is part of my code to let the user input 4 different guesses, is there anyway I can limit the input by 4 so that if the user inputs more or less than 4 it would be invalid?
the input are basically strings

Dani AI

Generated

A simple, robust way to enforce “exactly 4 strings” is to read the whole input line, split it into tokens, then check the token count and validate each token. That avoids the subtlety where operator>> can happily read four words and leave extra text in the stream. ’s looping/validation idea is good — this just shows a safe, single-line variant that’s easy to re-prompt until valid.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>

int main() {
    std::unordered_set<std::string> allowed = {"red","blue","green","yellow"};
    std::string line;
    std::vector<std::string> guess;

    while (true) {
        std::cout << "Enter 4 colors (e.g. red blue green yellow): ";
        if (!std::getline(std::cin, line)) return 0; // handle EOF

        guess.clear();
        std::istringstream iss(line);
        std::string tok;
        while (iss >> tok) {
            std::transform(tok.begin(), tok.end(), tok.begin(), [](unsigned char c){ return std::tolower(c); });
            guess.push_back(tok);
        }

        if (guess.size() != 4) {
            std::cout << "Please type exactly 4 items (you entered " << guess.size() << ").\n";
            continue;
        }

        bool ok = true;
        for (auto &s : guess) if (!allowed.count(s)) { std::cout << "Invalid color: " << s << '\n'; ok = false; }
        if (ok) break; // valid input
    }

    // guess now contains 4 validated strings
}

Notes and tips: if you want to accept multi-word names (e.g. "light blue") use a delimiter (commas) and split with getline(iss, token, ','), or require quotes. To ignore extra tokens instead of rejecting them, take the first four tokens after splitting. Always normalize (lowercase, trim punctuation) before comparing to your allowed set. If you mix cin >> with getline, remember to clear the leftover newline from the input buffer.

Recommended Answers

All 3 Replies

Why don't you use a counter and increment it when you get valid answers?
When it hits 4, drop out of the loop.

You could also just check to see if any of your variables is null (less than 4 condition).

If there are more than 4, could you ignore any additional input?

sorry, as I'm still quite new to C++, could you provide an example?
thanks alot

Something like this:

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

bool isValid(string str)
{
   /*
      do some type of check to make sure str is valid
      ...like comparing it to a know color list
   */
   return true;
}

int main(void)
{
   vector<string> vecGuess;
   char strTemp[128] = {0};
   
   for(int i =0; i < 4; i++)
   {
      cout << "guess #" << (i+1);
      cin >> strTemp;

      if(!isValid(strTemp))
      {
         cout << "not valid" << endl;
         i--;
         continue;
      }

      vecGuess.push_back(strTemp);
   }

   return 0;
}

You will need to validate what the user enters in the isValid() function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.