-
May 12th, 2009, 12:54 PM
#1
Problem with functions
I made this program it works fine.
I just need to separte it into two other functions one called user_input(), and the other one user_output().
Any help will be greatly appreciatted.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
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;
}
}
}
-
May 12th, 2009, 12:57 PM
#2
Re: Problem with functions
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.
-
May 12th, 2009, 01:03 PM
#3
Re: Problem with functions
The array indexing appears to be wrong pretty much everywhere actually.
-
May 12th, 2009, 01:32 PM
#4
Re: Problem with functions
The loops don't make sense either.
for(int a=0; a<numPlayers[size]; a++)
-
May 12th, 2009, 01:51 PM
#5
Re: Problem with functions
****. i'm just starting to learn C++. Appreciate the feedback. any pointers that could help me out with the function part of the code?
-
May 12th, 2009, 01:57 PM
#6
Re: Problem with functions
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.
-
May 12th, 2009, 01:58 PM
#7
Re: Problem with functions
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;
}
}
}
-
May 12th, 2009, 02:02 PM
#8
Re: Problem with functions
Lindley?
how do i cut the parts into functions?
I tried and it just wont compile right.
Can i call the functions from within the functions? when i do this it seems to override that command.
-
May 12th, 2009, 02:12 PM
#9
Re: Problem with functions
Originally Posted by jcnewton
I think i fixed it a little bit?
Code:
cin>>numPlayers[20];
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.
-
May 12th, 2009, 02:12 PM
#10
Re: Problem with functions
What do you expect this to do:
Code:
cin>>numPlayers[20];
because what it's actually doing is reading a single int into slot 21 of a 20-element array (invalid). Are you trying to read in 20 different values?
-
May 12th, 2009, 02:16 PM
#11
Re: Problem with functions
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.
-
May 12th, 2009, 02:21 PM
#12
Re: Problem with functions
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.
-
May 12th, 2009, 02:22 PM
#13
Re: Problem with functions
Originally Posted by jcnewton
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.
-
May 12th, 2009, 02:33 PM
#14
Re: Problem with functions
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?
-
May 12th, 2009, 02:44 PM
#15
Re: Problem with functions
Originally Posted by jcnewton
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|