Problem with functions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Problem with functions

  1. #1
    Join Date
    May 2009
    Posts
    6

    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;
    }
    }
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Problem with functions

    The array indexing appears to be wrong pretty much everywhere actually.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,069

    Re: Problem with functions

    The loops don't make sense either.

    for(int a=0; a<numPlayers[size]; a++)

  5. #5
    Join Date
    May 2009
    Posts
    6

    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?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.

  7. #7
    Join Date
    May 2009
    Posts
    6

    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;
    		}		
    	}
    	}

  8. #8
    Join Date
    May 2009
    Posts
    6

    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.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,069

    Re: Problem with functions

    Quote Originally Posted by jcnewton View Post
    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.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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?

  11. #11
    Join Date
    May 2009
    Posts
    6

    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.

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    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.

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,069

    Re: Problem with functions

    Quote Originally Posted by jcnewton View Post
    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.

  14. #14
    Join Date
    May 2009
    Posts
    6

    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?

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,069

    Re: Problem with functions

    Quote Originally Posted by jcnewton View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center