Array doesnt work right.

Hello. I am trying to learn arrays and one of my begginers project is that person enters how much numbers will he enter, then he enters the numbers in one line and then the program will cout those numbers. It doesnt really work though. It pops out 0 0 and then huge random numbers. Heres the code:
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
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int scount;
    int a[]={},b=0;
    cout << "How many numbers do you want to shuffle?\nAmmount of numbers: ";
    cin >> scount;
    while(scount<1)
    {
                   cout << "The value of numbers you want to shuffle has to be greater than 0.\nAmmount of numbers: ";
                   cin >> scount;
    }
    cout << "\nWhat numbers do you want to shuffle?(n1 n2 n3 n4...)\nNumbers: ";
    cin >> a[scount];
    cout << endl;
    while(scount>0)
    {
                   cout << a[b] << " ";
                   b++;
                   scount--;
    }
    cout << endl;
    
    system("PAUSE");
}


Thanks for your help in advance.
When setting up an array you have to start at a size you could treat this as your max size say for example 20 so int a[20]; and cout << "How many numbers do you want to shuffle?(MAX: 20)\nAmmount of numbers: ";. This is one example of implementation, cin >> a[scount]; only fills up one position of the array!

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
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int scount;
    int a[20];
    cout << "How many numbers do you want to shuffle?(MAX 20) \nAmmount of numbers: ";
    cin >> scount;
    while(scount<1 || scount>20)
    {
                   cout << "The value of numbers you want to shuffle has to be greater than 0 and less than 20.\nAmmount of numbers: ";
                   cin >> scount;
    }
    cout << "\nWhat numbers do you want to shuffle?\nNumbers: \n";
    for(int i = 0; i < scount; i++)
		{
		cout << "Number " << i + 1 << ": ";
		cin >> a[i];
		}
	
    cout << endl;
    for(int i = 0; i < scount; i++)
		{
		cout << a[i] << " ";
		}
    cout << endl;
    
    system("PAUSE");
}
Last edited on
Thanks. Works great :)
Topic archived. No new replies allowed.