I need something that can hold multiple integers.

Oct 8, 2012 at 5:13pm
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. 
Oct 8, 2012 at 5:15pm
Oct 8, 2012 at 6:34pm
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
Oct 8, 2012 at 7:05pm
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 Oct 8, 2012 at 7:06pm
Oct 8, 2012 at 7:12pm
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 Oct 8, 2012 at 7:15pm
Oct 8, 2012 at 7:22pm
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
Oct 8, 2012 at 9:40pm
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];
}
Oct 9, 2012 at 4:21pm
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?
Oct 9, 2012 at 4:44pm
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.)
Oct 9, 2012 at 5:10pm
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..
Oct 9, 2012 at 5:29pm
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
Oct 9, 2012 at 6:33pm
Replace endl with " "
Topic archived. No new replies allowed.