Displaying user input with array

So, what I'm making here is a program where users enter the input, then the output is displayed of what the user entered. As you can see, I am attempting to use array method but I didn't succeed like what I expected. Anything missing beyond my knowledge?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
    int a, b, c;
    cout<<"Enter viewers aged 2>5: "<<endl;
    cin>>a;
    cout<<"Enter viewers aged 7>17: "<<endl;
    cin>>b;
    cout<<"Enter adult viewers: "<<endl;
    cin>>c;
    
    int viewer[3]={a, b, c};
    cout<<"Viewers aged 2>5: "<<endl;
    cout<<viewer[0];
    cout<<"Viewers aged 7>17: "<<endl;
    cout<<viewer[1];
    cout<<"Adult viewers: "<<endl;
    cout<<viewer[2];
    return 0;
    
}

The output displayed:

Enter viewers aged 2>5:
Enter viewers aged 7>17:
Enter adult viewers:
Viewers aged 2>5:
0Viewers aged 7>17:
0Adult viewers:
32766
Last edited on
What I expected was to be like this:

Enter viewers aged 2>5:
24
Enter viewers aged 7>17:
14
Enter adult viewers:
3
Viewers aged 2>5:
24
Viewers aged 7>17:
14
Adult viewers:
3
Your output is in the same order that you specify. Your line feeds were in the wrong place.
Try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main()
{
    int a, b, c;
    cout<<"Enter viewers aged 2>5: "<<endl;
    cin>>a;
    cout<<"Enter viewers aged 7>17: "<<endl;
    cin>>b;
    cout<<"Enter adult viewers: "<<endl;
    cin>>c;
    
    int viewer[3]={a, b, c};
    cout<<"Viewers aged 2>5: ";
    cout<<viewer[0]<<endl;         // MOVE LINE FEED TO AFTER THE OUTPUT
    cout<<"Viewers aged 7>17: ";
    cout<<viewer[1]<<endl;         // MOVE LINE FEED TO AFTER THE OUTPUT
    cout<<"Adult viewers: ";
    cout<<viewer[2]<<endl;         // MOVE LINE FEED TO AFTER THE OUTPUT
    return 0;   // NOT NECESSARY IN main()
}
Alright, I manage to find out the source. But I don't really understand how it works though...
 
int viewer[3]={a, b, c};

So, i change it to this:
 
int viewer[]={a, b, c};
Topic archived. No new replies allowed.