Write starts corresplondingly to numbers entered

Hello. I have just began to study arrays and I am trying to write a program, which prints same numbers of stars, with numbers of entered. I have an array where I can enter 8 numbers, which should be from 0 to 15 only. and it should print out same number of starts next to the array element. I thought that I had figured it out, however it prints only one * net to each element and Can you please help me and tell me what can I do about it? Thank you :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
#include <math.h>
using namespace std;
int main ()
{
    
    int a[8];
    for (int i=0;i<8;i++){
    
    
        cin>>a[i];
    
    
    }
   
    for (int i=0;i<15;i++){
    
    
    cout<<a[i]<<"*";
    
    }
    
}
I think you meant something like:
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
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;

int main()
{
    int numarray[8];
    int dummy_var = -1;
    
    cout << "Enter numbers: " << endl;
    
    for(int i = 0; i < 8; i++)
    {
        while(dummy_var < 0 || dummy_var > 15)
        {
            cin >> dummy_var;
            if(dummy_var < 0 || dummy_var > 15)
                cout << "Number must be in the range 0 <= x <= 15!" << endl;
        }
        numarray[i] = dummy_var;
        dummy_var = -1;
    }
    
    cout << " " << endl;
    
    for(int j = 0; j < 8; j++)
    {
        cout << numarray[j] << " ";
        for(int k = 1; k <= numarray[j]; k++)
            cout << "*";
            
        cout << "\n";
    }
    
    return 0;
}
Last edited on
hahaha That is exactly what I wanted that you very much I now get what I should have done. Thanks you've been very helpful! :)
Topic archived. No new replies allowed.