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?
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
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.
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7