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
'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.
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..!!
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.
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.
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.