0

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.

Mathmath
  • 37
  • 9
  • You can use `scanf` easily for such a task, because it returns the number of inputs read, which can be used to loop until it is exactly equal to `4`. – Ahmed Akhtar Feb 24 '16 at 03:30
  • @AhmedAkhtar I just went onto cplusplus.com and looked for `scanf`. What I saw is that it was just to check if the value was the same. So using `scanf` would I use that to check the count of times it has gone through in the for loop? – Mathmath Feb 24 '16 at 03:38
  • How exactly do you want the user to give input? I mean do you expect them to hit enter after each number, or just enter 4 numbers separated by spaces and then hit enter just once? – Ahmed Akhtar Feb 24 '16 at 03:41
  • Either way would be fine for this. – Mathmath Feb 24 '16 at 03:43
  • But, in the former case where they hit enter after each number, "if the user only inputs 3 numbers" would not be possible because the console would be waiting for them to enter another one (i.e. the 4th one)? – Ahmed Akhtar Feb 24 '16 at 03:48
  • Right sorry. So in that case where the user inputs values one after another separated by only a space, how would I go about using `scanf`? Would it be as I stated in an earlier comment or would it be different? – Mathmath Feb 24 '16 at 03:55
  • scanf() is the recommended way to get formatted input in C, not C++. Have a look at this http://stackoverflow.com/questions/2430303/disadvantages-of-scanf – pcodex Feb 24 '16 at 04:02
  • In `if (input != cnt)` you check if the number input is not equal to the current count. Did you mean `if (cnt != 4)` instead? – Ahmed Akhtar Feb 24 '16 at 04:02
  • For "the user inputs values one after another separated by only a space": see my answer, on how to read the input as a `string` from the user, and then, parse it to find out, as to, how many numbers the user gave. – Ahmed Akhtar Feb 24 '16 at 06:54
  • @AhmedAkhtar yes I put the wrong value for the `if(cnt != 4)`. Thank you a bunch! – Mathmath Feb 26 '16 at 03:25

2 Answers2

1

Here's what you can do to check for exactly 4 inputs.

int cnt = 0;
while(cnt < 4)
{
  cout<<"Type in input :"<<endl;
  cin >> input;
  cnt+=1;
}
pcodex
  • 1,812
  • 15
  • 16
1

Now, you can do this, by taking input from the user as a string, and then parse that string, to see how many numbers were entered by the user. I have used getline to read a string, with spaces, from the user, and also, to later tokenize that string using a stringstream.

Here, isNumber is a helper function to check whether a given string, is a number and getNumber is another one to return that number. Both of these functions are implemented using strtol of stdlib.h.

I would suggest that you store the inputs, in an int array, so that you can access them later, hence, declare int nums; as int nums[4];.

Here is the code:

#include<iostream>
#include<sstream>
#include<stdlib.h>
using namespace std;

bool isNumber(string s)
{
    char* p;
    strtol(s.c_str(), &p, 10);
    return *p == 0;
}

int getNumber(string s)
{
    char* p;
    return strtol(s.c_str(), &p, 10);
}

void askUser()
{
 string strInput;
 int nums[4];
 int cnt;
 do
 {
  cnt = 0;
  cout << "Input 4 values: ";
  getline(cin, strInput, '\n');

  stringstream ss(strInput);
  string s;

  while (getline(ss, s, ' '))
  {
   if(isNumber(s))
   {
    if(cnt < 4)
    {
     nums[cnt] = getNumber(s);
    }
    cnt++;
   }
  }

  if(cnt!=4)
  {
   cout<<"Invalid number of inputs!\n";
  }
 }
 while(cnt!=4);

 int i;
 cout<<"The 4 numbers input by the user are: ";
 for(i = 0; i < 4; i++)
 {
  cout<<nums[i]<<" ";
 }
 cout<<endl;
}

Note: The input must be given as:

the user inputs values one after another separated by only a space.

Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
  • Ah yes! I was trying to see if I could avoid doing what I have to do without using `string` but I guess it is the best way especially since I actually have to check if the value inputted is a number or not. Thanks a lot! – Mathmath Feb 26 '16 at 03:26
  • Glad to help! Actually using a `string` becomes necessary here, because we have to take input once and then count the data in the input, which is not possible while using `cin` to get input for an `int`. – Ahmed Akhtar Feb 26 '16 at 09:50