Thread: C program with arrays - showing no compile errors but no output

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    C program with arrays - showing no compile errors but no output

    My program has a few functions and arrays, however I cannt work out why, even when there are no compile errors, there is no output. i.e. the cmd.exe is just blank with a blinking line as if i just opened it...
    My code is below:
    Code:
    #include "stdafx.h"
    #include "string.h"
    
    #define NUMBER_OF_TEAMS 20    /*The number of teams in the EPL*/
    #define NAME_LENGTH 12        /*Maximum length for any team name*/
    #define WIN 3                /*Points revieced when a games is won*/
    #define DRAW 1                /*Points revieced when a game is drawn*/
    #define PLAY 1                /*Add to games played*/
    
    
    typedef struct{
        char name[NAME_LENGTH];        /*Team name*/
        int games_played;            /*Games played*/
        int goals_scored;            /*Goals scored*/
        int goals_conceded;            /*Goals conceded*/
        int total_points;            /*Point on table*/
        int goal_diff;                /*Goal difference = (goals_scored - goals_conceded)*/ 
    }team_x;
    
    
    void get_team_names(team_x *team);
    void get_game_results(team_x *team);
    void goal_difference(team_x *team);
    void teams_a_to_z(team_x *team);
    void sort_points(team_x *team);
    void sort_goal_diff(team_x *team);
    void sort_goals(team_x *team);
    void standings_table(team_x *team);
    
    
    int 
    main(void)
    {    
        team_x team[NUMBER_OF_TEAMS]={0,0,0,0,0};    /*Array team of type team_x NUMBER_OF_TEAMS big*/ 
        get_team_names(team);                        /*Function to retrieve the team names i.e. data from input2a.dat*/
        get_game_results(team);                        /*Function to retrieve data from input2b.dat (results) and store these results into team[]*/
        goal_difference(team);                        /*Calculated goal difference for each team*/
        teams_a_to_z(team);                            /*Outputs a table of teams in alphabetical order*/
        sort_points(team);                            /*Sorts teams in order of best performing to worst performing by points*/
        sort_goal_diff(team);                        /*Sorts teams in order of best performing to worst performing by goal difference*/
        sort_goals(team);                            /*Sorts teams in order of best performing to worst performing by which team scored the most goals*/
        standings_table(team);                        /*Outputs a standings table where the team with the most points is on top*/
     
    return 0;
    }
    
    void get_team_names(team_x team[])
    {
        FILE *inp1; /*Data file containing team names*/
        int i;        /*Variable for loop*/
     
        /*Opens the data file*/
        inp1 = fopen("input2a.dat", "r");
    
        /*Creates message if no data is found*/
        if (inp1 == NULL){
            printf("Error reading 'input2a.dat'\n");
        }
        /*If data is found, stores in array team[].name*/
        else{
            for(i=0; i<NUMBER_OF_TEAMS; i+=1){
                fscanf(inp1, "%12s", team[i].name);
            }
        }
    
        /*Close data file*/
        fclose(inp1);
    }
    
    void get_game_results(team_x team[])
    {
        FILE *inp2;                                        /*Data file containing results of games*/
        int i, k, j;                                    /*Variables for loop*/
        int input_status=0;                                /*Status value to test if data file is valid*/
        char team1[NAME_LENGTH], team2[NAME_LENGTH];    /*Temporary places to store teams*/
        int score1, score2;                                /*Temporary places to store score results*/
     
        /*Opens the data file*/
        inp2 = fopen("input2b.dat", "r");
    
        /*Reads in first dataset*/
        input_status = fscanf(inp2, "%12s %3d %12s %3d", &team1, &score1, &team2, &score2);
     
        /*Test whether dataset is found*/
        if (input_status == NULL){
            printf("Error reading 'input2b.dat'\n");
        }
        /*If data is found, run loop until EOF*/
        else{
            while(input_status != EOF){
                /*If name inputted is not an EPL team name from the first dataset then prints error message*/
                for(k=0; k<NUMBER_OF_TEAMS; k+=1){
                    if((strcmp(team[k].name, team1) != 0) || (strcmp(team[k].name, team2) != 0)){
                    }
                    else if((strcmp(team[k].name, team1) == 0) || (strcmp(team[k].name, team2) != 0)){
                    }
                    else{
                        printf("ERROR in input2b.dat - Team name entered incorrectly\n");
                        printf("Inputted values were:\n %12s %3d %12s %3d\n", team1, score1, team2, score2);
                    }                    
                }
            }
     
            /*Checks to see if negative goals were inputted*/
            if(score1<0 || score2<0){
                printf("\nERROR in input2b.dat - Negative goals entered\n");
                printf("Inputted values were:\n %12s %3d %12s %3d\n", team1, score1, team2, score2);
            }
            /*Stores data in correct place in array*/
            else{
                for(i=0; i<NUMBER_OF_TEAMS; i+=1){
                    if(strcmp(team[i].name, team1)==0){
                        team[i].games_played=team[i].games_played+PLAY;            /*Adds 1 to games played for team1*/
                        team[i].goals_scored=team[i].goals_scored+score1;        /*Adds goals scored for team1*/
                        team[i].goals_conceded=team[i].goals_conceded+score2;    /*Adds goals conceded for team1*/
                        if(score1>score2){
                            team[i].total_points=team[i].total_points+WIN;            /*Adds 3 points to team1 if they won*/
                        }
                        if(score1==score2){
                            team[i].total_points=team[i].total_points+DRAW;            /*Adds 1 points to team1 if they drew*/
                        } /*Doesn't add anything if team1 lost*/
                    }
                    else if(strcmp(team[i].name, team2)==0){
                        team[i].games_played=team[i].games_played+PLAY;            /*Adds 1 to games played for team2*/
                        team[i].goals_scored=team[i].goals_scored+score2;        /*Adds goals scored for team2*/
                        team[i].goals_conceded=team[i].goals_conceded+score1;    /*Adds goals conceded for team2*/
                        if(score2>score1){
                            team[i].total_points=team[i].total_points+WIN;            /*Adds 3 points to team2 if they won*/
                        }
                        if(score2==score1){
                            team[i].total_points=team[i].total_points+DRAW;            /*Adds 1 points to team1 if they drew*/
                        } /*Doesn't add anything if team2 lost*/
                    }
                }
            }
    
            /*Scans in dataset for next loop or does nothing if reached EOF*/
            input_status = fscanf(inp2, "%12s %3d %12s %3d", &team1, &score1, &team2, &score2);
        }
                
        /*Close data file*/
        fclose(inp2);
    }
    
    void goal_difference(team_x team[])
    {
        int i; /*Variable for loop*/
    
        for(i=0; i<NUMBER_OF_TEAMS; i+=1){
            team[i].goal_diff = team[i].goals_scored - team[i].goals_conceded; /*calculates goal difference and saves into team[].goal_diff*/
        }
    }
    
    void teams_a_to_z(team_x team[])
    {
        int i; /*Variable for loop*/
     
        printf("\nTeam Results - Alphabetical Order\n"); /*Table heading*/
    
        printf("      Name        Played      Points    Goal-Diff    Goals-Scored\n"); /*Prints headings for table columns*/
    
        printf("-----------------------------------------------------------------\n"); /*Table lines*/
     
        for(i=0; i<NUMBER_OF_TEAMS; i+=1){
            /*Prints data in alphabetical order*/
            printf("%12s       %3d        %3d        %5d        %5d\n", team[i].name, team[i].games_played, team[i].total_points, team[i].goal_diff, team[i].goals_scored);
        }
    
        printf("-----------------------------------------------------------------\n"); /*Table lines*/
    }
    
    void sort_points(team_x team[])
    {
        int i,k; /*Variable for loop*/
        team_x temp; /*Temporary value*/
    
        /*Loop to sort EPL teams based on total points*/
        for(k=0; k<NUMBER_OF_TEAMS; k+=1){
            for(i=0; i<NUMBER_OF_TEAMS-1; i+=1){
                if (team[i+1].total_points > team[i].total_points){
                    temp = team[i];
                    team[i] = team[i+1];
                    team[i+1] = temp;
                }
            }
        }
    }
    
    void sort_goal_diff(team_x team[])
    {
        int i,k; /*Variable for loop*/
        team_x temp; /*Temporary value*/
    
        /*Loop to sort EPL teams based on goal difference*/
        for(k=0; k<NUMBER_OF_TEAMS; k+=1){
            for(i=0; i<NUMBER_OF_TEAMS-1; i+=1){
                if (team[i+1].goal_diff > team[i].goal_diff && team[i+1].total_points==team[i].total_points){
                    temp = team[i];
                    team[i] = team[i+1];
                    team[i+1] = temp;
                }
            }
        }
    }
    
    void sort_goals(team_x team[])
    {
        int i,k; /*Variable for loop*/
        team_x temp; /*Temporary value used so variables can swap positions*/
    
        /*Loop to sort EPL teams based on goals scored*/
        for(k=0; k<NUMBER_OF_TEAMS; k+=1){
            for(i=0; i<NUMBER_OF_TEAMS-1; i+=1){
                if (team[i+1].goals_scored > team[i].goals_scored && team[i+1].goal_diff == team[i].goal_diff && team[i+1].total_points==team[i].total_points){
                    temp = team[i];
                    team[i] = team[i+1];
                    team[i+1] = temp;
                }
            }
        }
    }
    
    void standings_table(team_x team[])
    {
        int i; /*Variable for loop*/
     
        printf("\n\nTeam Results - Performance Ranking\n"); /*Table heading*/
        printf("      Name        Played      Points    Goal-Diff    Goals-Scored\n"); /*Table column headings*/
        printf("-----------------------------------------------------------------\n"); /*Table lines*/
     
        for(i=0; i<NUMBER_OF_TEAMS; i+=1){
            /*Prints data as a standings (performance) table*/
            printf("%12s       %3d        %3d        %5d        %5d\n", team[i].name, team[i].games_played, team[i].total_points, team[i].goal_diff, team[i].goals_scored);
        }
        printf("-----------------------------------------------------------------\n"); /*Table lines*/
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Hello,i would use the debugger or just printf's among the lines of main.
    e.g.
    Code:
    int main(void)
    {
         printf("here --------1\n");
         team_x team[NUMBER_OF_TEAMS]={0,0,0,0,0};    /*Array team of type team_x NUMBER_OF_TEAMS big*/
         printf("here --------2\n");
         get_team_names(team);
         printf("here --------3\n");  
         .....
         return 0;
    }
    Then you will see the last printf that was executed,thus you will know which was the last line of main that was executed,or which function was entered and traped inside(maybe in a infinite loop or something).
    In the example if get_team_names function has an infinite loop for example,then the output will be
    Code:
    here-----1
    here-----2
    So you know that something after here----2 printf and before printf here-----3 cause the program to stack.
    I refer to infinite loops as an example.I do not know if they are relevant to your code (chrome does it very difficult to read large pieces of code :/ )

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First, you should make sure you have your warnings turned all the way up. When I do that, I get a huge list, telling me that it's missing declarations for FILE, EOF, fopen, printf, etc. You should #include <stdio.h> if you want to use those functions (EDIT: this may be taken care of by the non-standard header file "stdafx.h", but I don't know because I don't have MSVC++ or whatever compiler you're using that provides that header). Also, it's conventional to use angle brackets for standard header files, so unless string.h is a file in your local directory, I would suggest #include <string.h>.

    Now, once I resolve the header file issues, I still get several warnings:
    Code:
    $ make foo
    gcc -Wall -Wunreachable-code -g -std=c99 -pedantic  -lm -lpthread  foo.c   -o foo
    foo.c: In function ‘main’:
    foo.c:34: warning: missing braces around initializer
    foo.c:34: warning: (near initialization for ‘team[0]’)
    foo.c: In function ‘get_game_results’:
    foo.c:82: warning: format ‘%12s’ expects type ‘char *’, but argument 3 has type ‘char (*)[12]’
    foo.c:82: warning: format ‘%12s’ expects type ‘char *’, but argument 5 has type ‘char (*)[12]’
    foo.c:85: warning: comparison between pointer and integer
    foo.c:138: warning: format ‘%12s’ expects type ‘char *’, but argument 3 has type ‘char (*)[12]’
    foo.c:138: warning: format ‘%12s’ expects type ‘char *’, but argument 5 has type ‘char (*)[12]’
    foo.c:73: warning: unused variable ‘j’
    Line 34: You have a set of curly braces for the array, but you need a set for the struct too, and a third set for the array in the struct ('name'). If you want everything to be 0, just do
    Code:
    team_x team[NUMBER_OF_TEAMS] = {{{0}}};
    Lines 82 & 138: You don't need an & when passing char arrays to scanf. The name of the array without any [brackets] is already a pointer (to the first element).
    Line 85: input_status is of type int, NULL is of type "pointer to void". Don't compare them. If you want to check if input_status is 0, do if (input_status == 0) or just if (!input_status).
    Line 73: Remove the declaration of j if you're not using it.

    Fixing all that, I compile and run your program and get the following:
    Code:
    $ ./foo
    Error reading 'input2a.dat'
    Segmentation fault
    You shouldn't try to close a file if you didn't actually open it. Move the fclose(inp1); statement into the else clause in get_team_names.

    Also, you will need to provide a sample data file if you want us to help further.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    I tried the printf suggestion by std and it showed me that the problem was inside the function 'get_game_results(team_x team[])'.

    I also rectified all the issues that andruil pointed out and im still getting no output.

    To help you further the data files are as follows (which need to be converted from .txt to .dat

    input2a.txt:
    Arsenal AstonVilla Chelsea Everton Fulham Liverpool ManCity ManUtd Newcastle Norwich QPR Reading Southampton Stoke Sunderland Swansea Tottenham WestBrom WestHam Wigan

    input2b.txt:
    WestHam 1 AstonVilla 0
    WestBrom 3 Liverpool 0
    Reading 1 Stoke 1
    QPR 0 Swansea 5
    Fulham 5 Norwich 0
    Arsenal 0 Sunderland 0
    Newcastle 2 Tottenham 1
    Wigan 0 Chelsea 2
    ManCity 3 Southampton 2
    Everton 1 ManUtd 0
    Chelsea 4 Reading 2
    Swansea 3 WestHam 0
    Tottenham 1 WestBrom 1
    Southampton 0 Wigan 2
    Norwich 1 QPR 1
    ManUtd 3 Fulham 2
    AstonVilla 1 Everton 3
    Chelsea 2 Newcastle 0
    Stoke 0 Arsenal 0
    Liverpool 2 ManCity 2
    WestHam 3 Fulham 0
    Wigan 2 Stoke 2
    WestBrom 2 Everton 0
    Tottenham 1 Norwich 1
    Swansea 2 Sunderland 2
    ManCity 3 QPR 1
    Liverpool 0 Arsenal 2
    Southampton 2 ManUtd 3
    Newcastle 1 AstonVilla 1
    Norwich 0 WestHam 0
    Stoke 1 ManCity 1
    QPR 0 Chelsea 0
    ManUtd 4 Wigan 0
    Fulham 3 WestBrom 0
    AstonVilla 2 Swansea 0
    Arsenal 6 Southampton 1
    Sunderland 1 Liverpool 1
    Reading 1 Tottenham 3
    Everton 2 Newcastle 2
    WestHam 1 Sunderland 1
    Chelsea 1 Stoke 0
    WestBrom 1 Reading 0
    Swansea 0 Everton 3
    Southampton 4 AstonVilla 1
    Wigan 1 Fulham 2
    Tottenham 2 QPR 1
    Newcastle 1 Norwich 0
    ManCity 1 Arsenal 1
    Liverpool 1 ManUtd 2
    Fulham 1 ManCity 2
    Everton 3 Southampton 1
    Sunderland 1 Wigan 0
    Stoke 2 Swansea 0
    Arsenal 1 Chelsea 2
    Reading 2 Newcastle 2
    Norwich 2 Liverpool 5
    ManUtd 2 Tottenham 3
    AstonVilla 1 WestBrom 1
    QPR 1 WestHam 2

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Could you copy and paste your latest version of the code into a post here, so I know I'm working with the correct version?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Actually, never mind. Here's at least one issue:
    Code:
            while(input_status != EOF){
                /*If name inputted is not an EPL team name from the first dataset then prints error message*/
                for(k=0; k<NUMBER_OF_TEAMS; k+=1){
                    if((strcmp(team[k].name, team1) != 0) || (strcmp(team[k].name, team2) != 0)){
                    }
                    else if((strcmp(team[k].name, team1) == 0) || (strcmp(team[k].name, team2) != 0)){
                    }
                    else{
                        printf("ERROR in input2b.dat - Team name entered incorrectly\n");
                        printf("Inputted values were:\n %12s %3d %12s %3d\n", team1, score1, team2, score2);
                    }
                }
            }
    That's a while loop. It keeps going so long as input status is not EOF. But input_status is never changed inside that loop, so once it starts, it never stops. It's an infinite loop.

    I think that while loop is supposed to include all the code down to line 138 (the fscanf that gets new values for input_status).

    Note, you don't want to check against just EOF in your loop. fscanf returns the number of things successfully scanned, which should be 4 if you got all your variables (two team names and two scores). Thus, your loop should be:
    Code:
    while (input_status == 4) {
        ...
    }
    Or, to avoid having two fscanf calls, you can remove the code on lines 81-89 (and the closing curly bracket on line 139 too, of course) and replace it with
    Code:
    while (fscanf(inp2, "%12s %3d %12s %3d", team1, &score1, team2, &score2) != 4) {
        ...
        // no need for an fscanf call here
    }
    // check if we encountered an error
    if (ferror(inp2)) {
        // perror is a function that will print a more useful error message than just "error"
        perror("error reading input2b.dat");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-27-2011, 09:46 PM
  2. please compile my C program and help with my errors
    By howru in forum C Programming
    Replies: 4
    Last Post: 04-22-2008, 03:28 AM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  4. compile errors with date program
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 08-07-2005, 07:17 AM
  5. mega compile errors for small program
    By s_UNI_ in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2005, 12:00 PM

Tags for this Thread