I need something that can hold multiple integers.

I need some sort of variable that I can use to hold multiple integers and can print out basically. e.g
1
2
3
4
something userNumber = {a, b, c, d, e} // something along those lines. 
cout << "Please enter 5 Number: " << endl;
cin >> userNumbers;
cout << "Your chosen numbers" << userNumbers << endl; // prints out the 5 numbers the user has entered. 
I was thinking about using an array, but I've never used them with integers before so I didn't know how to do it. But thanks for reminding me :D
Ok, so I've got this this so far:
1
2
3
4
5
6
7
8
char userNumbers[5];

    cout << "\t Welcome, I will try to guess the minimum and maximum" << endl;
    cout <<  "\t           numbers of your chosen numbers." << endl;

    cout << "\nPlease 5 Enter Your Numbers here: " << endl;
        cin >> userNumbers;
    cout << "Your Chosen Numbers: " << userNumbers;

But I have a problem.
When I enter the 5 numbers. The cout << "Your Chosen Numbers: " << userNumbers; part only outputs the first numbers that is entered, how can I make it so it outputs all 5 numbers?
Last edited on
cin >> userNumbers[5];

This will try to store the input in userNumbers[5].... which does not exist. When you create an array of size 5, like this:
char userNumbers[5];
the following array elements are created:
1
2
3
4
5
userNumbers[0];
userNumbers[1];
userNumbers[2];
userNumbers[3];
userNumbers[4];




I note that you made an array of type char. Each element can hold one char object. Did you mean to make an array of type int?



 
cout << "Your Chosen Numbers: " << userNumbers;
This only works by chance because it's a char array. When you make it an int array, you will have to output each element individually. You need to go and read about arrays.
Last edited on
I note that you made an array of type char. Each element can hold one char object. Did you mean to make an array of type int?
Well when I make the array of the type int I get a bunch of errors, one of which says
No match for 'operator>>' in 'std::cin >> userNumbers;'
and some othera which when I click takes me to a new tab called istream
Well when I make the array of the type int I get a bunch of errors, one of which says
No match for 'operator>>' in 'std::cin >> userNumbers;
This is because you are trying to input the entire array at once.

You want

1
2
3
4
for (int i=0; i < ARRAY_SIZE; ++i)
{
   cin >> userNumbers[i];
}
Well I've done this
1
2
3
4
for(int i = 0; i < userNumbers; ++i)
        {
            cin >> userNumbers[i];
        }

But then I get this error saying
ISO C++ forbids comparison between pointer and integer

Which I can't figure because I haven't even used a pointer, or have I even learnt them yet.
Btw way, with you putting ARRAY_SIZE; did you want me to put my char array userNumbers?
Which I can't figure because I haven't even used a pointer, or have I even learnt them yet.


But you did. An array name without a subscript serves as a pointer to the first element in most contexts.

Btw way, with you putting ARRAY_SIZE; did you want me to put my char array userNumbers?


No. He wanted you to put the size of the array. (Restated: He wanted you to replace ARRAY_SIZE with the number of elements in the array.)
Thanks Cire :D I've now managed to nearly finish my program. But this code that I added to print out the chosen numbers.
1
2
3
4
for(int i = 0; i < 5; ++i)
        {
        cout << "Your Chosen Numbers: " << userNumbers[i] << endl;
        }

Prints out like
Your chosen numbers: x
Your chosen numbers: y
etc..
Is there any other code that I can write to make print out the numbers on one line? i.e Your Chosen Numbers: x, y, etc..
Try to figure it out :)
What you want is to print "Your chosen numbers: " once, then cycle through the elements in the array and print each one of them (without printing a newline of course), then print a newline at the end of everything
Replace endl with " "
Topic archived. No new replies allowed.