Like Tree1Likes

C++ Video Game Player Program

This is a discussion on C++ Video Game Player Program within the C++ Programming forums, part of the General Programming Boards category; ok so I have a program that is supposed to use arrays and this is the week we start learning ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    39

    C++ Video Game Player Program

    ok so I have a program that is supposed to use arrays and this is the week we start learning arrays. The program is supposed to allow the user to input scores for a number of players up to 100 players and then it will post all scores as well as calculate averages and show who has a below average score. My issue is that for some reason I am not getting the multiple players to display nor will any of there scores and the calculateAverage function doesnt seem to be working. Here is the code I have:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    const int ARRAY_SIZE = 100;
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef);
    void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers);
    double CalculateAverageScore(const int scoreAr[], int numPlayers);
    void DisplayBelowAverage(const string playerNameAr[], const int scoreAr[], int numPlayers, double averageScore);
    
    void main()
    {
    	string playerNameAr[ARRAY_SIZE];
    	int scoreAr[ARRAY_SIZE];
    	int numPlayers = 0;
    	int numPlayersRef = 0;
    	double averageScore = 0;
    
    	cout << fixed << showpoint << setprecision(2);
    	InputData(playerNameAr, scoreAr, numPlayersRef);
    	DisplayPlayerData(playerNameAr, scoreAr, numPlayers);
    	CalculateAverageScore(scoreAr, numPlayers);
    	DisplayBelowAverage(playerNameAr, scoreAr, numPlayers, averageScore);
    	system("PAUSE");
    }
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef)
    {
    	while(numPlayersRef < ARRAY_SIZE)
    	{
    		cout << "Please enter player's name (press Q to quit):  ";
    		getline(cin, playerNameAr[numPlayersRef], '\n');
    		cout << endl;
    		if ((playerNameAr[numPlayersRef] == "Q") || (playerNameAr[numPlayersRef] == "q"))
    			break;
    		cout << "Please enter " << playerNameAr[numPlayersRef] << "'s score:  ";
    		cin >> scoreAr[numPlayersRef];
    		cout << endl;
    		cin.ignore();
    		numPlayersRef++;
    	}
    }
    
    void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers)
    {
    	cout << setw(10) << left << "\n Name"
    		<< setw(5) << right << "Score" << endl;
    	for (int i = 0; i < numPlayers; i++)
    	{ 
    		cout << setw(10) << left << playerNameAr[numPlayers] 
    		<< setw(5) << right << scoreAr[numPlayers] << endl;
    	}
    }
    
    double CalculateAverageScore(const int scoreAr[], int numPlayers)
    {
    	int i;
    	double averageScore = 0, totalScore;
    	for (i = 0, totalScore = 0; i < numPlayers; i++)
    	{
    		totalScore += scoreAr[i];
    	}
    	averageScore = totalScore/i;
    	cout << fixed << showpoint << setprecision(2);
    	cout << averageScore << endl << endl;
    	return averageScore;
    }
    void DisplayBelowAverage(const string playerNameAr[], const int scoreAr[], int numPlayers, double averageScore)
    {
    	cout << "Players who scored below average\n";
    	cout << setw (10) << left << "Name"
    		<< setw(5) << right << "Score" << endl;
    	for (int i = 0; i < numPlayers; i++)
    		if(scoreAr[i] < averageScore)
    			cout << setw (10) << left << playerNameAr[i]
    		<< setw(5) << right << scoreAr[i] << endl;
    }

  2. #2
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef)

    Needs to be:

    void InputData(string& playerNameAr[], int& scoreAr[], int &numPlayersRef)

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,622
    Quote Originally Posted by Alexandre View Post
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef)

    Needs to be:

    void InputData(string& playerNameAr[], int& scoreAr[], int &numPlayersRef)
    Why?!
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Quote Originally Posted by Alexandre View Post
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef)

    Needs to be:

    void InputData(string& playerNameAr[], int& scoreAr[], int &numPlayersRef)
    thats not allowed with arrays.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Are you allowed (by your instructor) to use vectors then ?

    @GReaper: He is handing over a copy of these objects and expecting modifications to happen. Obviously the object returns unchanged and DisplayPlayerData() has nothing to display.

    Also I'm guessing you have not seen them in class yet but I would feel bad if I didn't mention that obviously you should use a struct/class to hold player data.

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Alexandre View Post
    @GReaper: He is handing over a copy of these objects and expecting modifications to happen. Obviously the object returns unchanged and DisplayPlayerData() has nothing to display.
    That is not applicable to ordinary arrays. They are always passed by address.


    The OP's problem, (along *many* other things) is that the input buffer is not empty when the loop repeats.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Tells you how often I use arrays :/ I normally almost exclusively use STL containers :\

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    1. There is no such thing as void main. Read How to define main()-FAQ
    2. You should make an array of structures here, with each structure representing a player: Lesson 7:Structs
    3. You are didn't declare numPlayersRef to be a reference, you declared it to be an int. Hence your display loop never executes.
    4. Same problem with you average loop.


    So for numPlayersRef to actually be a reference to numPlayers you would:
    Code:
    int numPlayers = 0;
    int &numPlayerRef = numPlayers;
    However that isn't needed, your add players function should just be:
    Code:
    int main()
    {
    	string playerNameAr[ARRAY_SIZE];
    	int scoreAr[ARRAY_SIZE];
    	int numPlayers = 0;
    	double averageScore = 0;
    
    	cout << fixed << showpoint << setprecision(2);
    	InputData(playerNameAr, scoreAr, numPlayers);
    	DisplayPlayerData(playerNameAr, scoreAr, numPlayers);
    	CalculateAverageScore(scoreAr, numPlayers);
    	DisplayBelowAverage(playerNameAr, scoreAr, numPlayers, averageScore);
    	
                 system("PAUSE");
                 return(0);
    }
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayersRef)
    {
                   ....
    Note: You need to change your prototype too.
    Last edited by AndrewHunter; 09-29-2011 at 08:23 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by AndrewHunter
    You are didn't declare numPlayersRef to be a reference, you declared it to be an int. Hence your display loop never executes
    That isn't necessary.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by manasij7479 View Post
    That isn't necessary.
    Look at the OPs original code before you go around pulling out single statements from my response. He has two values declared in main:
    int numPlayers;
    int numPlayersRef;

    Guess which one he passes to his input function and guess which one he passes to his print / average functions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by AndrewHunter View Post
    Look at the OPs original code before you go around pulling out single statements from my response. He has two values declared in main:
    int numPlayers;
    int numPlayersRef;

    Guess which one he passes to his input function and guess which one he passes to his print / average functions.
    Sorry, missed that. I don't know why there are two values though.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by manasij7479 View Post
    Sorry, missed that. I don't know why there are two values though.
    I came across too harsh, and I am sorry; too early in the morning (haven't finished my cup of coffee ). He doesn't need it as I pointed out in my post.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Quote Originally Posted by AndrewHunter View Post
    I came across too harsh, and I am sorry; too early in the morning (haven't finished my cup of coffee ). He doesn't need it as I pointed out in my post.
    I think I understand my issue with the numplayersref

    I just need to make all of my code numPlayers and take out the "ref"
    correct?

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    mana I am not sure I understand what you mean by the input buffer not being cleared out. I though the cin.ignore was supposed to free that all up before the loop restarted.

  15. #15
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    and andrew hunter,

    We have not yet learned about these structs that you talked about nor is it anything that I could use in my code so I am oblivious to what you are telling me to do with it.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Video player API etc.
    By g4j31a5 in forum C++ Programming
    Replies: 7
    Last Post: 09-19-2008, 11:53 AM
  2. Replies: 9
    Last Post: 11-11-2007, 01:31 PM
  3. Is it a watch? Is it an MP3 player? Is it a video player?
    By twomers in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-15-2006, 10:54 PM
  4. linux video player
    By colmd in forum Linux Programming
    Replies: 2
    Last Post: 06-28-2004, 04:27 PM
  5. Do most video game programmers program with C?
    By Tride in forum Game Programming
    Replies: 4
    Last Post: 05-12-2003, 05:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21