I am having a simple problem I cannot figure out by myself for some reason. I am trying to have the user input 4 different numbers at one time. Now the problem I am having is I want to check if they have inputted 4 numbers or not. So if the user only inputs 3 numbers (say 1,2,3) I want to output an error message and return them back to inputting 4 numbers. Same if they input say 5 numbers (say 1,2,3,4,5), output error message and return to input screen.
I have an idea as to how to do this but not too sure anymore. I think it would be something along the lines of:
void askUser()
{
int nums;
cin >> nums;
while (int i = 4) // have a for loop here to check if it
{ // has 4 numbers inputted
for (?) // this is where I am having a problem
{
//output numbers or output error message
cout << nums << endl;
else
cout << "Wrong amount of input! Please input 4 values" << endl;
return *back to input screen*
}
}
I saw another question on stack overflow that is a bit similar to this except that in the other question they are dealing with words and I am not to sure how to go about it without trying what they were doing. The other question I saw that is closest to this titled "C++ check for specific number of input". Thank you and hopefully this is asked properly.
* EDIT * Okay so for now I am trying out this:
int input;
int cnt = 0;
cout << "Input 4 values" << endl;
while(cnt < 4)
{
cin >> input;
cnt+=1;
if (input != cnt)
cout << "Error please input exactly 4 values" << endl;
}
return 0;
This seems to work although I must say I am using ideone.com to test out the code as I am on a laptop that doesn't have something such as eclipse or VS. Is there anything that can be done to make it more efficient or use less code? Thanks.