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:
#include <cstdlib>
#include <iostream>
usingnamespace 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");
}
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!
#include <cstdlib>
#include <iostream>
usingnamespace 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");
}