Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Suppose we have 3 arrays: One for the players' names,one for the games' names, and one for the games' scores.The size of the arrays is defined by the user.
we're supposed to use a function to add players' scores in a game and then display the names next to their correspondent score ,How can we code it?Need an answer ASAP.

What I have tried:

What I're written so far:
C++
void pscore(char pname[])
    {
    int pcounter;
    int gscore[pcounter];
    for(int i=0;i<pcounter;i++)
        {
        cout<<"enter score for player number"<<i+1<<endl;
        cin>>gscore[i];
        }
    cout<<gscore[pcounter];
    }
int main()
    {
    int p;
    cout<<"How many games"<<endl;
    cin>>p;
    char gname[p],pname[p];
    int gscore[p];
    cout<<"enter names"<<endl;
    for(int i=0;i<p;i++)
        {
        cin.get(pname,550);
        }
    pscore(pname[p]);
    cout<<pname[p]<<"\t"<<gscore[p];
    return 0;
    }

I always end up with an invalid conversion from 'char' to 'char**' [-fpermissive] message and I'm not sure why.
Posted
Updated 9-Apr-18 8:09am
v4
Comments
OriginalGriff 9-Apr-18 11:51am    
Code indented - always intend even trivial code - it makes it a whole load easier to see what is going on...

The first problem is this sequence of code :
void pscore(char pname[])
{
    int pcounter;
    int gscore[pcounter];
    ...
}
That will not compile because the array size needs to be a constant expression. If the count is going to be a variable expression (entered by the user) then you should allocate the arrays dynamically like this :
int * gscore = new int[count];
Then remember to release the memory when you done with it :
delete [] gscore;
There is a similar problem in the main function and it should be handled similarly.

A better option would be to use std::vector if it is allowed for this assignment.
 
Share this answer
 
I indented your code: nobody cares which indentation system you use: K&R, Whitesmiths, even the execrable 1TB is fine as long as you are consistent.

Now look at your code, and assume that the user enters "2" as the number of games.
How big is pname as a result?
C++
char gname[p],pname[p];

Answer: 2 characters.
So when you try to read 550 characters what happens?
C++
cin.get(pname,550);

Answer: It doesn't even slightly fit. So it randomly overwrites memory.
Worse, you do this repeatedly, overwriting the same memory you just overwrote.
And then after the loop, you try to work out the game score for the final name.
But...you have two different gscore arrays, and they aren't the same because they are in different functions.

That whole code shows sure signs of panic: you haven't sat down and thought about your homework, you've just slammed some code together and hoped for the best. That isn't going to work. Sit down, think about the homework task and what you need to do to solve it, and plan your code. Then try it out manually and see if it will work, before you get anywhere near a keyboard.

Hint: your "array of names" needs to be an array of pointer-to-character values, and you need to allocate enough space for each name before you read it - look at malloc
 
Share this answer
 
Your idea misses of a good design. In your tasks the player are a class with members for the name and the score. And a class for games with a player and a name score.

When you use standard library classes it gets more easier. Use std::string and std::vector for an easy control flow. Include the needed header files as documented.
C++
#include <vector>
#include <string>

class Player
{
  std::string name;
  int score;
}

class Game
{
  Player player;
  std::string name;
}

std::vector<Game> data;
Game game;
//fill game data
data.push_pack(game);// a game is stored
You can also do the implementation with pointers, but than you need to rebalance the alloc and free. And I think you have other priorities.

ASAP means that you missed your timelines.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900