Array size..!!

I am asking the user to input only 2 integers and I want to check if the user did input 2 integers using an array. Here is my code. Thanks for the help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
#include <string>

using namespace std;


int main()
{
  int a(0);
  int SIZE = 2;
  int array[SIZE];
  cout <<"Enter 2 positive integers: ";
  cin >> a;
  if(a > 0)
    {
      if(a.size() >  array[SIZE])
	{
	  cout << "Too many numbers" << endl;
	}
	 else
	   {
	     cout << a;
	   }
     }

    return 0;
}
'a' is an int, it doesn't have a size() function, because it is always only one element.

array[SIZE] isn't your size; SIZE is your size. array[SIZE] is the SIZE'th element of array, which doesn't exist, because array has SIZE elements going from [0, SIZE-1].

On line 13, you're telling the program to read in *one* int. Anything beyond that is ignored (sort of). You can tell it to read exactly two ints quite similarly:

1
2
3
int a;
int b;
cin >> a >> b;


This will read in (the first) two ints and save them into a and b respectively. If you want to read a variable number of ints, you can use a loop.
What if I am only allowed to use one variable aka int a. Do I do it something like this?

1
2
3
4
5
for(int i = 0 ; i < (array size of the integer entered) ; i++)
{

  etc...
}

closed account (3qX21hU5)
What exactly are you trying to do?

I am trying to store as many numbers in the array and check if there is only 2 integers. I know it sounds stupid to use an array but I am just practicing to learn how to use size of array for other different things like odd/even/negative/non-negative..etc..!!
closed account (3qX21hU5)
Well if you want to store the number into a array directly you could use a loop like this

1
2
3
4
5
6
// Use size_t when making a index for a array. Also SIZE is the array's size.
for (size_t i = 0; i != SIZE; ++i)
{
    cin >> myArray[i];
    ++numberOfInputs;
}


That will read the users input directly into the arrays elements. Then if you wanted to keep track of how many times the user inputted numbers you could make a counter that keeps track of it. If that is what you are asking, if not let me know.
Last edited on
That won't work, because it'll stop at SIZE and therefore never know if it's bigger than SIZE.
closed account (3qX21hU5)
Huh how could the array be bigger then SIZE? and it stops 1 before SIZE.

Anyways I am not sure I understand what the OP is trying to do to. I don't really get the part of storing as many number in the array and the checking if there is only 2 integers parts.

Maybe that is why my example wont work? Otherwise it does what I believed he wanted just fine (Grab user input and put that input into the array), though now that I look at it again there is no point for the counter since it will always equal SIZE - 1 since there is no other way to break out of the loop.
I'm not sure that I understand the question. But I would use a string and getline. Then use a stringstream to extract as many integers as possible from that string.
closed account (3qX21hU5)
Lol I think all of us have different ideas on what he is trying to do. Kobe would you elaborate a bit more on what you are trying to do?
i guess you want the input to contain 2 digits only no less or more not 2 ints if that's what you mean here is how:
any number contains 2 digits only should be between the values 10-99 so you can test the input to be larger than 9 and smaller than 100.
I hope this is what you want.
Not sure if you meant 2 individual values or just to check if you entered 2 integers in one value at a time. So I did both.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
main(){
int amount = 0;
int i = 0;
const int SIZE = 2;
int my_array[SIZE];
    while(amount < 2){
        cout << "Enter integer " << (i+1) << ": ";
        (cin >> my_array[i]).get();
        if(!(my_array[i] > 9 && my_array[i] < 100)){
            system("cls");
            cout << "You didn't input 2 integers as a value!\nTry again!\n";
            system("pause");
            system("cls");
            continue;
        }
    i++;
    amount++;
    }
}
Topic archived. No new replies allowed.