Computing.Net > Forums > Programming > C program for creating player list

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C program for creating player list

Reply to Message Icon

Name: Swapnil
Date: September 4, 2003 at 21:25:49 Pacific
OS: DOS, WINDOWS
CPU/Ram: 400
Comment:

I have to submit an assignment in college.
the program is:
Create a player list containing player name, team name, batting average. For that first create a structure 'Cricket'. then create an array player[50]& store information in that. Then print the information team wise.
e.g. all indian palyers shold be printed one after one, then australian players.

Please help me!!



Sponsored Link
Ads by Google

Response Number 1
Name: Rolos
Date: September 4, 2003 at 21:44:56 Pacific
Reply:

Since you have no code, none of us can help you in that area. I will make no effort in developing that part since you have nothing to show. I can however, suggest that you begin writing a pseudocode to get your program started.

Your structure should look like this:

struct Cricket
{
// members go in here.
};

Do you know how to access these members in your program? If so then developing the rest of the code should be quite straight forward for you.

1) Declare your variables.
2) Prompt the user to enter names.
3) Get the names and store them in an array.
4) Print the team names to the console.
5) End of program.

I am not sure what method C programmers use to get input from the user, in C++ it is "cin >>". Let me know if this is helpful to you.
Also state your experience with programming.

- Rolos


0

Response Number 2
Name: Infinite Recursion
Date: September 4, 2003 at 23:00:48 Pacific
Reply:

Indians play cricket? That's interesting.
Thanks for listing your partial assignment, do you have an actual question?

Us doing your homework for you will not help you get your degree, even if it did, you would not be productive in the workforce and couldn't hold a job.

Perhaps, you can read your book... attempt the assignment and "call" back when you have a problem that you have actually tried to resolve on your own.

IR


0

Response Number 3
Name: Swapnil
Date: September 5, 2003 at 00:36:07 Pacific
Reply:

Dear Rolos,

Thanks for yor respond.
But my actual query is how to sort players by their team names??
i.e.
if i enter 5 player information,
3 of them are Indians.
2 of them are australians.

Then how to show output as:
Indian team:
player1
player2
player3

Australian team:
player1
player2

Tha actual problem is sorting palyers team wise.
I tried with strcmp() and if(p.team_name=='India')
But it didn't work.

Don't give me full code but atleast please tell me how to sort players by team names..


0

Response Number 4
Name: Swapnil
Date: September 5, 2003 at 00:37:01 Pacific
Reply:


Dear Infinite Recursion,

Thanks for your reply.
Indians are born cricket players!!
As you asked my actual question is as:


Define a structure called Cricket that will describe the following info:
1) Player Name
2) Team name
3) batting average

Using this info. declare an array 'Player' with 10 elements and write a program to read the information about all players. Create team wise list contaiing name of the player with their batting average.


As you said i tried different books asked friends and seniors but no one was able to give solution.
Tha actual problem is sorting names team wise.
I tried with strcmp() and if(p.team_name=='India')
But it didn't work.

Don't give me full code but atleast please tell me how to sort players by team names..


0

Response Number 5
Name: Rolos
Date: September 5, 2003 at 04:41:37 Pacific
Reply:

Swapnil: Hmmm. It appears that you are knowledgeable of your string functions. You could approach this problem either the dirty way by using dynamic memory management (pointers), or getting away with brute force coding. Either way you will have to deal with a few things that you may or may not like. But it seems that the bottom line is getting this assignment done.

I think your problem would lie in comparing strings that have variations in them. Such as if a user were to enter 'India' then 'INDIA' or 'InDiA'. Since your program will be doing many comparisons, this will be an obstacle. You will have to develop an algorithm that converts the entire string to either lowercase or uppercase. The toupper() and tolower() functions with array positions or characters as it's parameter. strlen() which returns the length of the string with the string name as it's parameter, would also be of great value to you as well.

Secondly, after doing conversions to lower or upper case you should do some string copying of course. Store the team name inside of a variable that you will keep for future comparisons team names. Be sure that you are not copying the same string twice though. I do not know if you have knowledge of pointers, otherwise you could use that as well if you are good at it. Another thing to keep in mind is that you need to make sure that you take the necessary precautions to prevent 'out of bounds' errors as these will occur if user enters strings that are too long. Again, use strcmp() to compare two strings AFTER your conversions to lower OR upper case. If it returns '0' then it means that they are the same, else they are not. It should work if you use the tolower() or toupper() functions on your strings FIRST.

Thirdly, I would suggest the use of 2 dimensional arrays. If you have not used 2 dimensional arrays before, they are declared such as array[50][20]. You will have 50 blocks of memory for each player to store 20 characters designated for the team name per each block. So in other words I have 50 players and I can write their team names up to 20 characters for each player.

If you have text books on data structures, use them. They have many snippets of implementations of the string class and many other classes that you can change around and implement (sine you are only using structures in this case). But I think that the mainstay of your code will be coming from the use of mostly string functions. I did not like these kinds of assignments because there are so many ways to approach it. Experimenting is the best way to find out how things work. Research all you can on string functions and I am very sure that you will kick some serious ass in this assignment.

IR: *phew* I think I just wrote another tutorial there. People will disagree in many areas with what I have just advised, but it's the most basic explanation that my mind can conceive. If anyone were to step into my mind for a look, it would be like walking right into 12 category 4 hurricanes. And that's only when I'm sleeping 8)

- Rolos


0

Related Posts

See More



Response Number 6
Name: Swapnil
Date: September 5, 2003 at 07:41:05 Pacific
Reply:

Dear rolos,

thanks for ur responce.
As you said if user entered INDIA or InDiA or india it will not matter in this assignment. Well i can use string conversions but basically while submitting this assignment i will use only lower case characters as 'india'.

i am giving the code which i am trying to work out:

/*Program to create player information*/

#include
void main()
{
int i,j,n;
char *p[50];
struct cricket
{
int avg;
char player_name[50];
char team_name[50];

}
player[50];

printf("Enter the no. of players to be entered: ");
scanf("%d",&n);
for(i=0;in;i++)
for(j=0;jn-1;j++)
{
printf("Enter the player name: ");
scanf("%s",player[i].player_name);
printf("Enter the team name:" );
scanf("%s",player[j].team_name);
printf("Enter the batting average: ");
scanf("%d",&player[j].avg);
*p=player[i].team_name;
strcmp(india,*p);
}

}

but it gives an error that 'undefined symbol 'india'.
when i tried to use
if(*p=='india')
it gave me an erroe that 'character constant too long'

i have one idea in mind but don't know how to proceed with that:
when user will enter team india i will check if string is 'india' or 'australia' every time in for loop.
and if it is india then i will call the function india() that contains an array that will store player 1 info.
if user takes australia then control will go to aus() function defined outside for loop.

But the problem will ariese that it will print as follows:

india:
player1
player2

australi:
player3

india:
player4


i have a book called 'Data Structure in C' by Tennebaum but no solution found for that.

Please help me.



0

Response Number 7
Name: Infinite Recursion
Date: September 5, 2003 at 11:56:33 Pacific
Reply:

Swapnil,

You mention: "The actual problem is sorting palyers team wise. I tried with strcmp() and i p.team_name=='India') But it didn't work."

My suggestion would be to use "India" and not 'india' in your strcmp(). That is the reason 'india' is an undefined symbol, because it know that you want it to be a string. So you need to use "india".

Also, Rolos has the right idea when suggesting that you use the tolower(), it will account for all user inputs InDia, INDIA, india, etc.

Just a thought ...

p.team_name=='India')
should read...
if (tolower(p.team_name) =="india"))
{
// your code
}

I don't have sufficient time to test the idea, but I am sure it could be the culprit of your problem.

IR


0

Response Number 8
Name: Rolos
Date: September 5, 2003 at 12:38:54 Pacific
Reply:

Swapnil: If the team names (such as india or australia) are constants, you should store those strings inside of string variables. Then do your comparisons. Try avoiding structure pointers if you need to get this done right away because it will get messy and confusing.

Use strcmp() to compare the current entry to either team names. Then you could probably approach your method much more easily in your for loop by providing an if statement that checks. So for example:

// declarations.
char india[20] = "india";
char australia[20] = "australia";
char string[50][20];
int members;

cout << "Enter number of players:"
cin >> members;
cout << endl;

// loop
for (int i = 0; i > string[i]; // get string.
tolower(string); // convert string.

if (strcmp(india,string) == 0)
// call function.

else if (strcmp(australia,string == 0)
// call function.

else
// go burn in hell for mispelling.
}

Now of course you would have to call them from your structure, but I am providing an example and trying to keep away from doing the actual assignment. I think this should work well without actually using pointers, if it gets too messy then just go brute force.

- Rolos


0

Response Number 9
Name: Rolos
Date: September 5, 2003 at 12:40:37 Pacific
Reply:

Oops, I guess I missed the closing parenthesis in

if (strcmp(australia,string) == 0)
{
// code.
}


0

Response Number 10
Name: Swapnil
Date: September 6, 2003 at 05:52:03 Pacific
Reply:

Dear Rolos,

I have given up almost!!
I am not getting it, i think i will not able to solve this problem!!

Thanks anyway for your help!!


0

Response Number 11
Name: Swapnil
Date: September 6, 2003 at 05:55:33 Pacific
Reply:

Dear IR,

As u said
"Us doing your homework for you will not help you get your degree, even if it did, you would not be productive in the workforce and couldn't hold a job. "

I think u were right!!

Hmmm!! I think i will not able to be a good programmer!!


0

Response Number 12
Name: Rolos
Date: September 6, 2003 at 14:36:48 Pacific
Reply:

Swapnil: There really is no sense in giving up. Even though this assignment might be due soon, work on it some more. It will only make you a more proficient and better programmer and it will reflect in your future assignments and even when you get out into the industry.

Doing it yourself doesn't always work. Most computing assignments or projects are done in teams because it is most likely that someone else knows more in some area than you do. Try talking to some of your classmates because I am very sure that they have some good insight on your assignment as well, even though this is an individual assignment. I'm sure others will be more than happy to provide some help.

Try using this for loop to convert your entire string to lower case:

char string[20];
char converted_string[20];

// get input for string here.

for (int i = 0; i = 20; i++)
converted_string[i] = tolower(string[i];

// do printf() here to see results.

What this for loop does is that it takes each position, converts the character, and sticks it into the new array. This method should convert the entire input string in lower characters and store them into the converted_string array. Good luck.

- Rolos


0

Response Number 13
Name: Infinite Recursion
Date: September 7, 2003 at 04:02:58 Pacific
Reply:

Swapnil,

You will make a good programmer as long as you keep trying and never give up until the task is complete. The moment you give up, is when you need to choose another profession. Programming is not easy by any means... and being new to some concepts, I could see where this would be confusing. The reason that I made the comment that you quoted is because I used to work with "programmers" who did this. Being that most industrial assignments are team based, it shows who knows what... and the people that did not contributed are normally the people who cheated... yet they get credit for the completed project. It kinda infuriates me a bit.

At any rate, I have some code below for you (written in C++ on purpose, it is not hard to translate over to C, just a matter of IO, etc.) It stores the information, prints, and compares... it works for me, hopefully you can incorporate this solution into your assignment. If you have any questions let us know. Don't get burned out and give up now, you have plenty of time to thing about that your senior year (like me)... the trick is, don't give in, because you can do anything you put your mind to.

Regards,

Infinite Recursion


--hopefully it posts correctly and readable--

#include <iostream.h>

void main (void)
{
     struct team
     {
          int player_avg[50];
          char player_name[50];
          char team_name[50]; 
     };

     team India;
     //team Australia; 
     /* Not using team Australia in this example for simplicity.
     However, to compare the team names, you can see below. 
Also,
     having the structure based off the actual team, will 
elminate
     the need to compare them... as you can just start 
outputting
     with which ever team structure you choose, followed 
by the
     second one. */


     /* Assigning just two players for team India
     and one to team Australia. To enter the rest,
     just utilize a while or for loop */

     // GETTING TWO INDIAN PLAYERS
     int i = 0; // setting counter to element (player) 
0 (first player).
     for (i = 0; i < 2; i++)          
     {
          cout << "Enter the batting average: ";
          cin >> India.player_avg[i];
          cout << "Enter the player's name: ";
          cin >> India.player_name[i];
          cout << "Enter the team's name: ";
          cin >> India.team_name[i];
     }

     // PRINTING INDIAN PLAYERS
     for (i = 0; i < 2; i++)          
     {
          cout << "Player's Name: " << India.player_name[i] 
<< endl;
          cout << "Player's Team: " << India.team_name[i] << 
endl;
          cout << "Player's Average: " << India.player_avg[i] 
<< endl;
          cout << endl;
     }

     // EXAMPLE OF COMPARING TEAM NAME
     if (strcmp(tolower(India.team_name[1]), "india")
     {
          cout << "Team for " << India.player_name[1] << " 
is INDIA!";
          else
               cout << "Team for " << India.player_name[1] << " 
is AUSTRALIA!";
     }
}


0

Response Number 14
Name: Infinite Recursion
Date: September 7, 2003 at 04:16:10 Pacific
Reply:

Pick and choose which headers you may or may not need from:

#include <iostream.h>
#include <string.h>
#include <ctype.h>

Also, you will run into difficulties with my strcmp code above... this error was intentional... take a peek at http://www.cprogramming.com/fod/strcmp.html
to derive a solution for this.

As I mentioned earlier having a team oriented structure, will eliminate the need for comparison anyway. Just declare your teams of type 'team', and print out each team until the last player from each team is printed. One team after the other...

Regards,

Infinite Recursion


0

Sponsored Link
Ads by Google
Reply to Message Icon

newb vb 6 user Loader Script



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C program for creating player list

c++ programming for novell www.computing.net/answers/programming/c-programming-for-novell/2525.html

begining with C programing www.computing.net/answers/programming/begining-with-c-programing/11065.html

HElp me with this c++ program plez. www.computing.net/answers/programming/help-me-with-this-c-program-plez/9940.html