'c Array of display wrong output
I am trying to read the input from the terminal into array and do some further processing.
Here is the expected output
Child 1, pid 12345: Group A: ManCity (England) and PSG (France)
Child 1, pid 12345: Group B: Liverpool (England) and Atletico (Spain)
Child 1, pid 12345: Group C: Ajax (Netherlands) and Sporting (Portugal)
Child 1, pid 12345: Group D: RealMadrid (Spain) and InterMilan (Italy)
Child 1, pid 12345: Group E: Bayern (Germany) and Benfica (Portugal)
Child 1, pid 12345: Group F: ManUnited (England) and Villarreal (Spain)
Child 1, pid 12345: Group G: Lille (France) and Salzburg (Austria)
Child 1, pid 12345: Group H: Juventus (Italy) and Chelsea (England)
Child 1, pid 12345: Salzburg (Austria) vs Bayern (Germany)
Child 1, pid 12345: Sporting (Portugal) vs ManCity (England)
Child 1, pid 12345: Benfica (Portugal) vs Ajax (Netherlands)
Child 1, pid 12345: Chelsea (England) vs Lille (France)
Child 1, pid 12345: Atletico (Spain) vs ManUnited (England)
Child 1, pid 12345: Villarreal (Spain) vs Juventus (Italy)
Child 1, pid 12345: InterMilan (Italy) vs Liverpool (England)
Child 1, pid 12345: PSG (France) vs RealMadrid (Spain)
However, instead of the expected output's structure of team name(nation) vs team name(nation), I got this:
Child 1, pid 2775: Salzburg (Lille) vs Bayern (Juventus)
Child 1, pid 2775: Sporting (Juventus) vs ManCity (Ajax)
Child 1, pid 2775: Benfica (Chelsea) vs Ajax (Salzburg)
Child 1, pid 2775: Chelsea (Italy) vs Lille (England)
The interest is the right output if I use the i in for loop(line44 - 56)and I print it successful(line 52).However if I use hard code of 0(line 53). It will change the var.
below are the code of the program .
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
int pid, cid, parentid, myid, numTeam;
char *FCteamA[] = {}, *FCteamB[] = {}, *CountryA[] = {}, *CountryB[] = {}, *TeamA[] = {}, *TeamB[] = {};
char *Group[] = {"A", "B", "C", "D", "E", "F", "G", "H"};
int seed = 2022;
int TeamDetail= 32, MaxData = 52, MaxTeam = 16 , countCompetition = 35 ;
//build method find country in debug mode
char *checkCountry(char *Team){
int i;
for (i = 0; i < 8; i++){
// printf("%s\n",CountryA[i]);
if(strcmp(Team, FCteamA[i]) == 0 ){
return CountryA[i];
}else if(strcmp(Team, FCteamB[i]) == 0){
return CountryB[i];
}
}
}
// debug
void debugMode(int argc, char *argv[])
{
int checkchild = 0 , countDeatil = 3;
pid = fork();
checkchild++;
if (pid < 0)
{ // error occurred
printf("Fork Failed\n");
exit(1);
}
else if (pid == 0)
{ // child process: handle
cid = getpid();
// get teamName and country
for (int i=0; i<(MaxTeam/2);i++)
{
FCteamA[i] = argv[countDeatil];
CountryA[i] = argv[countDeatil + 1];
FCteamB[i] = argv[countDeatil + 2];
CountryB[i] = argv[countDeatil + 3];
printf("First Testing :%s %s %s %s\n",CountryA[i],CountryB[i],FCteamA[i],FCteamB[i]);
printf("Second Testing :%s %s %s %s\n",CountryA[0],CountryB[0],FCteamA[0],FCteamB[0]);
//printf("Child %d, pid %d: Group %s : %s (%s) and %s (%s) \n ", checkchild, getpid(), Group[i], FCteamA[i], CountryA[i], FCteamB[i], CountryB[i]);
countDeatil = countDeatil + 4;
}
for (int p = 0; p < (MaxTeam/2); p++)
{
TeamA[p] = argv[countCompetition];
TeamB[p] = argv[countCompetition + 1];
char *returnCountry = checkCountry(TeamA[p]);
char *returnCountry2 = checkCountry(TeamB[p]);
printf("Child %d, pid %d: %s (%s) vs %s (%s) \n ", checkchild, getpid(), TeamA[p], returnCountry, TeamB[p], returnCountry2);
countCompetition = countCompetition + 2;
}
exit(0);
}
else{ // pid > 0 parent process
printf("Parent: Child %d collected\n", checkchild);
exit(0);
}
}
// estimation
int estimationMode()
{
}
int main(int argc, char *argv[])
{
numTeam = (argc - 3) / 2;
printf("There are %d teams\n", numTeam);
if (strcmp(argv[2], "D") == 0)
{
debugMode(argc, argv);
}
else if (strcmp(argv[2], "E") == 0)
{
estimationMode();
}
else
{
printf("Please input again!");
exit(0);
}
// srand(seed);
}
here is input
fixture D ManCity England PSG France Liverpool England Atletico Spain Ajax Netherlands
Sporting Portugal RealMadrid Spain InterMilan Italy Bayern Germany Benfica Portugal
ManUnited England Villarreal Spain Lille France Salzburg Austria Juventus Italy
Chelsea England Salzburg Bayern Sporting ManCity Benfica Ajax Chelsea Lille Atletico
ManUnited Villarreal Juventus InterMilan Liverpool PSG RealMadrid
The first time asks the question, I am sorry for my bad format and english. Hope guys give me the advice.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
