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; Originally Posted by zonen I think I understand my issue with the numplayersref I just need to make all of ...

  1. #16
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by zonen View 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?
    Yes, just like I showed you in my post. If you can't use structs don't use them than. My point 1 still needs to be addressed though. (no such thing as void main).
    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.

  2. #17
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by zonen View Post
    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.
    You need a cin.ignore() after each input statement based on how you are doing things.
    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.

  3. #18
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Is there any reason for this?

    Code:
    double averageScore = 0, totalScore;
    	for (i = 0, totalScore = 0; i < numPlayers; i++)
    It seems there for no good reason other than to confuse yourself, just initialise the totalScore right where you do the averageScore, silly constructs like that are totally out of context IMO !
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #19
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by zonen View Post
    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.
    You need a cin.ignore() after each input statement based on how you are doing things.
    vanilla cin.ignore() is not sufficient here ... AFAIK .
    You need
    Code:
     cin.ignore(numeric_limits<streamsize>::max(),'\n');
    To clear out all the trailing characters.
    btw...you'd need to include <limits> for that.
    AndrewHunter likes this.
    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 !



  5. #20
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Quote Originally Posted by manasij7479 View Post
    vanilla cin.ignore() is not sufficient here ... AFAIK .
    You need
    Code:
     cin.ignore(numeric_limits<streamsize>::max(),'\n');
    To clear out all the trailing characters.
    btw...you'd need to include <limits> for that.
    mana is there any tutorials on how that statement you posted works because I haven't seen anything on limits<streamsize>::max() before. Is this going to have to go after all of my input statements?

    I will try to research the <limits> and see if I can research it better then find out if I can use it in this assignment.

  6. #21
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by zonen View Post
    mana is there any tutorials on how that statement you posted works because I haven't seen anything on limits<streamsize>::max() before. Is this going to have to go after all of my input statements?

    I will try to research the <limits> and see if I can research it better then find out if I can use it in this assignment.
    Not always, but when the situation is somewhat like; you expect a single character y/n answer and get words like Yes, No, Nope ..and the program needs to use the input for other reasons after that.

    numeric_limits can be used to determine the max or min size/range of type passed (and many other safety tricks !) in the <> brackets.
    numeric_limits - C++ Reference

    Then, the you can easily find out how it works, by examining the prototype of istream::ignore - C++ Reference .
    Last edited by manasij7479; 09-29-2011 at 11:36 AM.
    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. #22
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Quote Originally Posted by manasij7479 View Post
    Not always, but when the situation is somewhat like; you expect a single character y/n answer and get words like Yes, No, Nope ..and the program needs to use the input for other reasons after that.

    numeric_limits can be used to determine the max or min size/range of type passed (and many other safety tricks !) in the <> brackets.
    numeric_limits - C++ Reference

    Then, the you can easily find out how it works, by examining the prototype of istream::ignore - C++ Reference .
    so is this statement required after all of my input statements?

  8. #23
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    ok so my average is working now but the names are still not displaying from the arrays for me and the belowaverage function is not bringing anything up. Any ideas on what I am doing wrong?

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    const int ARRAY_SIZE = 100;
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayers);
    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);
    
    int 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, numPlayers);
    	DisplayPlayerData(playerNameAr, scoreAr, numPlayers);
    	CalculateAverageScore(scoreAr, numPlayers);
    	DisplayBelowAverage(playerNameAr, scoreAr, numPlayers, averageScore);
    	system("PAUSE");
    }
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayers)
    {
    	while(numPlayers < 100 & playerNameAr[numPlayers]!= "Q")
    	{
    		cout << "Please enter player's name (press Q to quit):  ";
    		getline(cin, playerNameAr[numPlayers], '\n');
    		cout << endl;		
    		if ((playerNameAr[numPlayers] == "Q") || (playerNameAr[numPlayers] == "q"))
    			break;
    		cout << "Please enter " << playerNameAr[numPlayers] << "'s score:  ";
    		cin >> scoreAr[numPlayers];
    		cout << endl;
    		cin.ignore(numeric_limits<streamsize>::max(),'\n');
    		numPlayers++;
    	}
    }
    
    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;
    }

  9. #24
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by zonen View Post
    ok so my average is working now but the names are still not displaying from the arrays for me
    Well, let's see:
    Code:
    //Logical AND is && not &
    //Also use your constant ARRAY_SIZE here vice your magic number 100
    while(numPlayers < 100 & playerNameAr[numPlayers]!= "Q")
    Quote Originally Posted by zonen View Post
    and the belowaverage function is not bringing anything up. Any ideas on what I am doing wrong?
    That is because you never store the result of your CalculateAverageScore function. In main you would need to have:
    Code:
    averageScore = CalculateAverageScore(scoreAr, numPlayers);
    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.

  10. #25
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Quote Originally Posted by AndrewHunter View Post
    Well, let's see:
    Code:
    //Logical AND is && not &
    //Also use your constant ARRAY_SIZE here vice your magic number 100
    while(numPlayers < 100 & playerNameAr[numPlayers]!= "Q")

    That is because you never store the result of your CalculateAverageScore function. In main you would need to have:
    Code:
    averageScore = CalculateAverageScore(scoreAr, numPlayers);

    ya that makes perfect sense I completely overlooked the '&' I guess I didn't stop and think enough about how my calculate average funtion was really returning the average. I will give that a shot tonight and see if it starts running everything smoothly.

  11. #26
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    still no luck even with those changes from hunter. Issue still is with displaying the names from the arrays and there scores correctly. The average score function works perfectly but It will not print out the names or scores and the below average function is also not putting any names and scores in at the bottom.

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    Here is one problem:
    Code:
    void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers)
    ...
        for (int i = 0; i < numPlayers; i++)
        {
            cout << setw(10) << left << playerNameAr[numPlayers]
            << setw(5) << right << scoreAr[numPlayers] << endl;
        }
    ...
    Presumably you meant numPlayers to be i.

    Applying my fix with previously mentioned fixes makes the output correct for me. If it does not for you, then provide up-to-date code and steps to get incorrect output.
    Last edited by Elysia; 09-30-2011 at 01:19 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Quote Originally Posted by Elysia View Post
    Here is one problem:
    Code:
    void DisplayPlayerData(const string playerNameAr[], const int scoreAr[], int numPlayers)
    ...
        for (int i = 0; i < numPlayers; i++)
        {
            cout << setw(10) << left << playerNameAr[numPlayers]
            << setw(5) << right << scoreAr[numPlayers] << endl;
        }
    ...
    Presumably you meant numPlayers to be i.

    Applying my fix with previously mentioned fixes makes the output correct for me. If it does not for you, then provide up-to-date code and steps to get incorrect output.
    ya making that fix to the scores worked but how did you get the players names to come out correctly? for some reason I am not getting them to display at all.

  14. #29
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    Quote Originally Posted by zonen View Post
    ya making that fix to the scores worked but how did you get the players names to come out correctly? for some reason I am not getting them to display at all.
    this is my code now which still is not displaying the players names when I enter them and the belowAverage function is still not displaying either.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    const int ARRAY_SIZE = 100;
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayers);
    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);
    
    int main()
    {
    	string playerNameAr[ARRAY_SIZE];
    	int scoreAr[ARRAY_SIZE];
    	int numPlayers = 0;
    	int numPlayersRef = 0;
    	double averageScore = 0;
    	averageScore = CalculateAverageScore(scoreAr, numPlayers);
    
    	cout << fixed << showpoint << setprecision(2);
    	InputData(playerNameAr, scoreAr, numPlayers);
    	DisplayPlayerData(playerNameAr, scoreAr, numPlayers);
    	CalculateAverageScore(scoreAr, numPlayers);
    	DisplayBelowAverage(playerNameAr, scoreAr, numPlayers, averageScore);
    	system("PAUSE");
    }
    void InputData(string playerNameAr[], int scoreAr[], int &numPlayers)
    {
    	while(numPlayers < ARRAY_SIZE && playerNameAr[numPlayers]!= "Q")
    	{
    		cout << "Please enter player's name (press Q to quit):  ";
    		getline(cin, playerNameAr[numPlayers], '\n');
    		cout << endl;		
    		if ((playerNameAr[numPlayers] == "Q") || (playerNameAr[numPlayers] == "q"))
    			break;
    		cout << "Please enter " << playerNameAr[numPlayers] << "'s score:  ";
    		cin >> scoreAr[numPlayers];
    		cout << endl;
    		cin.ignore(numeric_limits<streamsize>::max(),'\n');
    		numPlayers++;
    	}
    }
    
    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[i] << 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;
    }

  15. #30
    Registered User
    Join Date
    Sep 2011
    Posts
    39
    scratch that I got the names to post properly but the below average function is not working.

Page 2 of 3 FirstFirst 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