I have been working on my final project that i am going to do a simple soccer game. Not graphical. I will ask the user to choose or create a team. Teams are saved in the same file as the code as text files. where i am stuck is, i need to create a league of 8 teams randomly and have them match up with each other for the each game. I can't really figure out how to do this.

#define     Arsenal             "arsenal.txt"
#define     Barcelona           "barcelona.txt"
#define     Bayern_Munich       "bayern munich.txt"
#define     Chelsea             "chelsea.txt"
#define     Juventus            "juventus.txt"
#define     Liverpool           "liverpool.txt"
#define     Manchester_United   "manchester united.txt"
#define     Porto               "porto.txt"
#define     Real_Madrid         "real madrid.txt"
...
void choose_team(char teams[9][100],char players[10][100],int team_choice);
void create_league(int team_choice,char teams[9][100],
                       char league[8][100],int drawing[9][100]);
...
void choose_team(char teams[9][100],char players[10][100],int team_choice)
  {
       FILE *fh = NULL;
       int i = 0;
       
       switch(team_choice)
             {
                          case 1:{
                               system("cls");
                               printf("Your team is:Arsenal\n");
                               printf("Your players are:\n");
                               fh = fopen(teams[0],"r");
                               // Repeat until I reach end of file
                                           while (! feof(fh))
                                           {// Read a string from the file
                                                fgets(players[i], 100, fh);
                                                // Get rid of the newline
                                                if(players[i][ strlen(players[i]) - 1 ] == '\n')
                                                   players[i][ strlen(players[i]) - 1 ] = 0;
                                        printf("%s\n", players[i]);         
                                                // Increment the counter
                                                i++;
                                            } // end of while
                                           fclose(fh);
                               break;
                               }
....// choices go like that

void create_league(int team_choice,char teams[9][100],
                       char league[8][100],int drawing[9][100])
{}

Kudos to using code tags correctly for your first post! :)

but i dont understand the goal, here.... i see that you read player's names ... but where's the "random matchup" of teams come into play, and what's that got to do with the players?

I am going to use the names to print events in the game. I have logically solved the creating a league error. But now i get another error."incompatible types in assignment".

void create_league(char temp[10][100],char teams[10][100],int  team_choice)
  {
       int i = 0;
       strcpy(temp[0],teams[team_choice--]);
       for(i=team_choice--; i>0 ;i--)
       {teams[i] = teams[i--];}
       teams[0] = temp[0];

But now i get another error."incompatible types in assignment".

you cant use the assignment operator (=) to copy strings.

use "strcpy()" like you did earlier in the function.

or, better yet, use "strncpy()".

Thanks for the strcpy() advice it seems to be working, but still the function i have given is not working properly. i was planning to move the users choice(team) to the beginning of the array.

# define   Arsenal             "arsenal.txt"
# define   Barcelona           "barcelona.txt"
# define   Bayern_Munich       "bayern munich.txt"
# define   Chelsea             "chelsea.txt"
# define   Juventus            "juventus.txt"
# define   Liverpool           "liverpool.txt"
# define   Manchester_United   "manchester united.txt"
# define   Porto               "porto.txt"
# define   Real_Madrid         "real madrid.txt"
void create_league(char temp[10][100],char teams[10][100],int  team_choice);

...

char teams[10][100] = {Arsenal, Barcelona, Bayern_Munich, Chelsea, Juventus,
   Liverpool, Manchester_United, Porto, Real_Madrid};
...
void create_league(char temp[10][100],char teams[10][100],int  team_choice)
  {
       int i = 0;
       strcpy(temp[1],teams[team_choice--]);
       for(i=team_choice--; i>0 ;i--)
       {
       strcpy(teams[i--], teams[i]);
       }
       strcpy(teams[0], temp[1]);
       printf("The league is:\n");
       for(i=0;i<9;i++)
       printf("%s\n", teams[i]);
       
       
  }

when i try to sort it out like this, team arsenal is always replaced with the next team in the row that user choose.(If i choose juventus, it is replaced with liverpool.). And the rest of the teams are not sorted out. There are 2 liverpools with example i have given above. One in the first place and one in its normal place. I started to freak out.:confused:

your for loop is confusing . instead of that convoluted decrement method you're trying to do, do something like this (pseudo code):

set team[0] <-- teamlist[userchoice]

set  counter  = 1
for loop = 1 to (total # of teams - 1)
     if loop = userchoice, do nothing
     else team[counter++] <-- teamlist[userchoice]

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.