Code to check if entry already exists in array?!?!

tsj

tsj

Permabanned
Joined
7 May 2004
Posts
1,051
Location
United Kingdom
Code:
int initialise(int numofteams) {
         
        char *yes = "y";
        char *no = "n";
        int tmp_y;
        int tmp_n;                      
        char yesno[1];
                                
        printf("\nDo you want to read data from a file? (y/n)\n>");
        scanf("%s", yesno);             
                                
        tmp_y = (strcmp(yesno, yes)); 
        tmp_n = (strcmp(yesno, no));    
                                
/** if reading from a file... **/
        if (tmp_y == 0) {
        printf("you entered yes");
        read_file();
        }
                                         
/** if not reading from file... **/
        if (tmp_n == 0)
        {
                int i;  
                        for (i = 0; i < numofteams; i++)
                        {
        
                                char teamd[16];
                                printf("\nPlease enter team name:\n>");
                                scanf("%s", teamd);
        
                                char target[16] = "teamd";
                                int j;
        
                                for (j = 0; j < i; ++j)
                                {
                                        int comptname;
                                        comptname = (strcmp(teamd, arrdetails[j].team));
        
                                        if (comptname == 0)
                                        {
                                        printf("Sorry, this team already exists. Please try again!");
                                        i--;
                                        }
                
                                        if (comptname != 0)
                                        {
                                        strcpy(arrdetails[num_teams].team, teamd);
                                        arrdetails[num_teams].played = 0;
                                        arrdetails[num_teams].won = 0; 
                                        arrdetails[num_teams].lost = 0;
                                        arrdetails[num_teams].draw = 0;
                                        arrdetails[num_teams].points = 0;
                                        arrdetails[num_teams].goalsfor = 0;
                                        arrdetails[num_teams].goalsagainst = 0;
                                        arrdetails[num_teams].goalsd = 0;
                                        arrdetails[num_teams].team_id = num_teams;
                                        num_teams++;  
                                        }
        
                                }
                        }
        }
        return 0;
}

The problem code is where it checks if the team is already in the array. it just doesnt function correctly.

Any ideas why its not functioning correctly?
 
tsj said:
Code:
int initialise(int numofteams) {
         
        char *yes = "y";
        char *no = "n";
        int tmp_y;
        int tmp_n;                      
        char yesno[1];
                                
        printf("\nDo you want to read data from a file? (y/n)\n>");
        scanf("%s", yesno);             
                                
        tmp_y = (strcmp(yesno, yes)); 
        tmp_n = (strcmp(yesno, no));    
                                
/** if reading from a file... **/
        if (tmp_y == 0) {
        printf("you entered yes");
        read_file();
        }
                                         
/** if not reading from file... **/
        if (tmp_n == 0)
        {
                int i;  
                        for (i = 0; i < numofteams; i++)
                        {
        
                                char teamd[16];
                                printf("\nPlease enter team name:\n>");
                                scanf("%s", teamd);
        
                                char target[16] = "teamd";
                                int j;
        
                                for (j = 0; j < i; ++j)
                                {
                                        int comptname;
                                        comptname = (strcmp(teamd, arrdetails[j].team));
        
                                        if (comptname == 0)
                                        {
                                        printf("Sorry, this team already exists. Please try again!");
                                        i--;
                                        }
                
                                        if (comptname != 0)
                                        {
                                        strcpy(arrdetails[num_teams].team, teamd);
                                        arrdetails[num_teams].played = 0;
                                        arrdetails[num_teams].won = 0; 
                                        arrdetails[num_teams].lost = 0;
                                        arrdetails[num_teams].draw = 0;
                                        arrdetails[num_teams].points = 0;
                                        arrdetails[num_teams].goalsfor = 0;
                                        arrdetails[num_teams].goalsagainst = 0;
                                        arrdetails[num_teams].goalsd = 0;
                                        arrdetails[num_teams].team_id = num_teams;
                                        num_teams++;  
                                        }
        
                                }
                        }
        }
        return 0;
}

The problem code is where it checks if the team is already in the array. it just doesnt function correctly.

Any ideas why its not functioning correctly?

In what way is it 'not functioning correctly'?
 
Code:
How many teams are in the league?
5

Do you want to read data from a file? (y/n)
>n

Please enter team name:
>manu

Please enter team name:
>liverpool

Please enter team name:
>chelsea
Sorry, this team already exists. Please try again!
Please enter team name:
>manu

Please enter team name:
>liverpool
Sorry, this team already exists. Please try again!
Please enter team name:
>everton

Please enter team name:
>astonvilla
 
tsj said:
Code:
How many teams are in the league?
5

Do you want to read data from a file? (y/n)
>n

Please enter team name:
>manu

Please enter team name:
>liverpool

Please enter team name:
>chelsea
Sorry, this team already exists. Please try again!
Please enter team name:
>manu

Please enter team name:
>liverpool
Sorry, this team already exists. Please try again!
Please enter team name:
>everton

Please enter team name:
>astonvilla

Well, the first thing i'd do is extract the code that checks if a team is already in the array. Call it isTeamInArray(const char * teamName); Then test that code in isolation until you know its working, then integrate it wiuth the rest of the app.

As ever when debugging, divide and conquer. Reduce the problem to the minimal piece of code that still exhibits the error.
 
Visage said:
Well, the first thing i'd do is extract the code that checks if a team is already in the array. Call it isTeamInArray(const char * teamName); Then test that code in isolation until you know its working, then integrate it wiuth the rest of the app.

As ever when debugging, divide and conquer. Reduce the problem to the minimal piece of code that still exhibits the error.


nice one mate gonna go try that :D
 
Back
Top Bottom