C++ Array problem???

Mar 24, 2017 at 1:53am
I tried this Array entry method, where it asks for a colour then you press return and put in the remaining ones, and then it prints out the full Array. Can anyone explain why it will not do the first slot, "colours[0]", it ignores this and enters the last for strings. What am I missing?

int main() {
string colours[CAPACITY];
string input;

for (int a = 0; a < CAPACITY; a++) {
colours[a] = input;
cout << "Please type in the colours you want:";
cin >> input;

}
for (int j = 0; j < CAPACITY; j++){
//for (int j = 0; j < numberOfElements; j++) {
cout << "Colour #" << " " << colours[j] << endl;
}
return 0;
}
Mar 24, 2017 at 2:20am
The example below is a way to input into an array of X amount of elements.
1
2
for (int count = 0; count < CAPACITY; count++)
    cin >> colours[count];


Here is what I mean (an example)
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
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
    const int CAPACITY{ 4 };
    string colours[CAPACITY]{" "};

    cout << "Please type in the colours you want\n";
    for (int count = 0; count < CAPACITY; count++)
    {
	   cout << "Colour #" << count + 1 << ": ";
	   cin >> colours[count];
    }
   
    cout << "The colours you chose are:\n";
    for (int count = 0; count < CAPACITY; count++)
	   cout << colours[count] << endl;

    return 0;
}
Mar 24, 2017 at 3:06am
Thank you Chicofeo; that is very neat. Nice code!

Topic archived. No new replies allowed.