user input range into array help

Mar 16, 2016 at 7:47am
Hi all, I am trying to give a range to the user inputs, between 1 to 10 as it is being inserted into an array of size 3.

I think I have totally messed up and did it wrongly as there is an error that says 'error: no match for ‘operator>’ in ‘std::cin > 10’'

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    int array[3];
    
    for (int i=0; i<3;i++)
    {
        cout << "Enter in value for Number " << i + 1 << " : "  ;
        while (cin < 0 || cin > 10)
        {
            cin >> array[i];
        }
    }
}
Mar 16, 2016 at 8:22am
That's not how cin works. If i were you I would do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
#include <iostream>
using namespace std;
int main() {
    int array[3];
    int input;
    for(int i = 0; i < 3; i++) {
        //cout your thing
       cin>>input;
       while(! (input > 0 && input< 10) ){
          cin>>input;
      }
      array[i] = input;
    } 
    return 0;
} 

This will keep asking for input if its not in range 0..10. Sorry about the ugly syntax but im from a phone
EDIT:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
#include <iostream>
using namespace std;
int main() {
    int array[3];
    int input;
    for(int i = 0; i < 3; i++) {
        //cout your thing
      do{
          cin>>input;
      }while(input < 0 || input >10);
      array[i] = input;
    } 
    return 0;
} 

Thats a better way!
EDIT 2:
You can even put thw cout inside the do loop so it asks everytime.
Last edited on Mar 16, 2016 at 8:32am
Mar 16, 2016 at 9:00am
Hi there, thanks for the input.

Just curious though, what is the differences between the two codes you posted other than the while and do-while loops that you have used?
Topic archived. No new replies allowed.