I need help with using the rand() function and an array to create 19 random numbers when the user types 'P' instead when I type 'P' I get 19 rows of 19 numbers

``````int main ()
{
char command;
double setNum [19];
int create;

create = 0;

cout << "***************************************************" << endl;
cout << "*                                                 *" << endl;
cout << "*                    MENU                         *" << endl;
cout << "* N/n    : Create A New Set Of Numbers            *" << endl;
cout << "* P/p    : Print Numbers To Screen                *" << endl;
cout << "* S/s    : Calculate & Print Sum Of Numbers       *" << endl;
cout << "* A/a    : Calculate & Print Average Of Numbers   *" << endl;
cout << "* M      : Determine Maximum Value & Location     *" << endl;
cout << "* m      : Determine Minimum Value & Location     *" << endl;
cout << "* Q/q    : Quit                                   *" << endl;
cout << "***************************************************" << endl;

cout << "Command => ";
cin >> command;

srand(time(NULL));
while(create < 19)
{
setNum[create] = rand();
create++;

switch (command)
{
case 'N':
case 'n':
break;
case 'P':
case 'p':cout <<"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19" << endl;
cout <<create;
break;
case 'S':
case 's':
break;
case 'A':
case 'a':
break;
case 'M':
break;
case 'm':
break;
case 'Q':
case 'q':
break;//system ("cls");
}

}

cin.ignore();//flush input buffer

getchar ();

}

}``````

## All 3 Replies

rand() returns integers, not doubles or floats, so you might as well declare setNum as an array of integers. And the code you posted does not even reference that array nor does it ever call rand()

thank you for your response, I intialized the array as an int and also I initialized numbers 0-19

``int setNum [20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}``

line 27 I belive I called rand()

what's the purpose of that switch statement inside the while loop? The while loop should do nothing more than call rand() for each element of the array.

There is no point initializing the array like that when you are going to change all the elements to the value returned by rand().

>>line 27 I belive I called rand()
Oh yes, I missed that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.