const int size = 20;
int numPlayers[size];
int quickNum[size];
int length[size];
int main ()
{
cout<<"How many players will be playing?"<<endl;
cin>>numPlayers[size];
for(int a=0; a<numPlayers[size]; a++){
cout<<"What is your name player # "<<a+1<<"?"<<endl;
string name;
cin>>name;
cout<<"Hello "<<name<<" how many quick picks will u be playing today?"<<endl;
cin>>quickNum[size];
for(int b=0; b<quickNum[size]; b++){
cout<<"What should the length of quick pick # "<<b+1<<" be"<<endl;
cin>>length[size];
cout<<"Your numbers are: ";
srand((unsigned)time(0));
int random_integer;
for(int index=0; index<length[size]; index++){
random_integer = (rand()%99)+1;
cout << random_integer<<" ";
}
cout<<endl;
}
}
}
It is a little surprising that your program "works fine", since it appears to contain several bugs. For example, you read into numPlayers[size], which does not exist, since numPlayers has size number of elements.
By the way, please post your code in [code][/code] bbcode tags.
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
Since all your variables are global----not a good habit, by the way----there's no need to concern yourself too much with parameter passing or return values. It's just a matter of cutting/pasting the relevant portions of the code into function bodies and calling them from main.
I think i fixed it a little bit? is this better? still how do i separate it into functions?
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int numPlayers[20];
int quickNum[20];
int length[20];
int main ()
{
cout<<"How many players will be playing?"<<endl;
cin>>numPlayers[20];
for(int a=0; a<numPlayers[20]; a++){
cout<<"What is your name player # "<<a+1<<"?"<<endl;
string name;
cin>>name;
cout<<"Hello "<<name<<" how many quick picks will u be playing today?"<<endl;
cin>>quickNum[20];
for(int b=0; b<quickNum[20]; b++){
cout<<"What should the length of quick pick # "<<b+1<<" be"<<endl;
cin>>length[20];
cout<<"Your numbers are: ";
srand((unsigned)time(0));
int random_integer;
for(int index=0; index<length[20]; index++){
random_integer = (rand()%99)+1;
cout << random_integer<<" ";
}
cout<<endl;
}
}
}
No. You're not understanding arrays. Declaring an array such as
int numPlayers[20];
means you have 20 integers. You generally access them one at a time through an index. Valid indexes are 0 through 19. numPlayers[20] is invalid memory. I'm not even sure why you're making numPlayers an array as it appears you only need that number once.
I know you're all gung ho on functions, but you're not there yet. Go reread the chapter on arrays. The program as written would most likely crash.
The point is, you've only got one "numPlayers". There's no need for numPlayers to be an array at all. numPlayers defines the useful size of other arrays, but there's no need for it to be an array itself.
Also, when accessing an array depending on a loop index.....use the loop variable to index the array. That's kinda obvious, but a lot of people don't see it right away for some reason.
its asks the user how many players are going to be playing. I dont know how to use vectors that well yet.
MY instructor told me to just make an array with 20 spaces and if i dont use all of them its ok. it'll just loop till the number the user selected.
You're missing the point. You don't need that number to be an array. Even if you did, valid indexes are 0 through 19. 20 is invalid memory and your program will crash.
when i make numPlayers a regular int and take out its array
and the user types in how many user will play. it wont do as the user wants?
why does it only work as an array then?
when i make numPlayers a regular int and take out its array
and the user types in how many user will play. it wont do as the user wants?
why does it only work as an array then?
You're not listening. The program as written couldn't possibly work. If you've changed your code, post it again.
Bookmarks