Thread: expected expression before const

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    expected expression before const

    on the line before return(0) i get the error message when i use the printf statement. before that line ive basically used many for loops and if statements to find the maximum scored and assigned the team name to const char* y1 if that team has scored the most. i stopped the code to test it first and i got that error message that i stated in the titls. can someone explain to me what is wrong there? also it gives me warning messages for all of the "y1's" saying unused variable "y1"


    Code:
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAXTEAMS 16
    typedef struct team
    {
        char name[50];
        int points;
        int scored;
    }teams;
    
    
    
    
    teams team1[MAXTEAMS] = {{"a"},{"b"},{"c"},{"d"},{"e"},{"f"},{"g"},{"h"},{"i"},{"j"},{"k"},{"l"},{"m"},{"n"},{"o"},{"p"}};
    
    
    int main()
     {
         team1[0].scored = 0;
         team1[1].scored = 0;
         team1[2].scored = 0;
         team1[3].scored = 0;
         team1[4].scored = 0;
         team1[5].scored = 0;
         team1[6].scored = 0;
         team1[7].scored = 0;
         team1[8].scored = 0;
         team1[9].scored = 0;
         team1[10].scored = 0;
         team1[11].scored = 0;
         team1[12].scored = 0;
         team1[13].scored = 0;
         team1[14].scored = 0;
         team1[15].scored = 0;
        int i;
    
    
                for(i=0;i<MAXTEAMS;i++)
                {
                    printf("enter team:\n");
                    scanf("%s",team1[i].name);
                }
                printf("welcome to the champions league\n\n");
    
    
    
    
                         for(i=1;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[0].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[0].name);
                            scanf("%d",&team1[0].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[0].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                                 for(i=2;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[1].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[1].name);
                            scanf("%d",&team1[1].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[1].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=3;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[2].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[2].name);
                            scanf("%d",&team1[2].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[2].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=4;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[3].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[3].name);
                            scanf("%d",&team1[3].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[3].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=5;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[4].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[4].name);
                            scanf("%d",&team1[4].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[4].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=6;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[5].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[5].name);
                            scanf("%d",&team1[5].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[5].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=7;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[6].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[6].name);
                            scanf("%d",&team1[6].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[6].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=8;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[7].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[7].name);
                            scanf("%d",&team1[7].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[7].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=9;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[8].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[8].name);
                            scanf("%d",&team1[8].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[8].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=10;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[9].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[9].name);
                            scanf("%d",&team1[9].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[9].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=11;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[10].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[10].name);
                            scanf("%d",&team1[10].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[10].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=12;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[11].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[11].name);
                            scanf("%d",&team1[11].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[11].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=13;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[12].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[12].name);
                            scanf("%d",&team1[12].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[12].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=14;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[13].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[13].name);
                            scanf("%d",&team1[13].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[13].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                            printf("%s vs %s\n",team1[14].name,team1[15].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[14].name);
                            scanf("%d",&team1[14].scored);
                            printf("%s's score:",team1[15].name);
                            scanf("%d",&team1[15].scored);
                            team1[14].scored += team1[0].scored;
                            team1[15].scored += team1[i].scored;
    for(i=1;i<MAXTEAMS;i++)
    {
        if(team1[0].scored > team1[i].scored)
        {
            const char* y1 = team1[0].name;
        }
    }
        for(i=2;i<MAXTEAMS;i++)
        {
            if(team1[1].scored > team1[i].scored)
            {
                const char* y1 = team1[1].name;
            }
        }
        for(i=3;i<MAXTEAMS;i++)
        {
            if(team1[2].scored > team1[i].scored)
            {
                const char* y1 = team1[2].name;
            }
        }
        for(i=4;i<MAXTEAMS;i++)
    {
        if(team1[3].scored > team1[i].scored)
        {
            const char* y1 = team1[3].name;
        }
    }
        for(i=5;i<MAXTEAMS;i++)
        {
            if(team1[4].scored > team1[i].scored)
            {
                const char* y1 = team1[4].name;
            }
        }
        for(i=6;i<MAXTEAMS;i++)
        {
            if(team1[5].scored > team1[i].scored)
            {
                const char* y1 = team1[5].name;
            }
        }
        for(i=7;i<MAXTEAMS;i++)
    {
        if(team1[6].scored > team1[i].scored)
        {
            const char* y1 = team1[6].name;
        }
    }
        for(i=8;i<MAXTEAMS;i++)
        {
            if(team1[7].scored > team1[i].scored)
            {
                const char* y1 = team1[7].name;
            }
        }
        for(i=9;i<MAXTEAMS;i++)
        {
            if(team1[8].scored > team1[i].scored)
            {
                const char* y1 = team1[8].name;
            }
        }
        for(i=10;i<MAXTEAMS;i++)
    {
        if(team1[9].scored > team1[i].scored)
        {
            const char* y1 = team1[9].name;
        }
    }
        for(i=11;i<MAXTEAMS;i++)
        {
            if(team1[10].scored > team1[i].scored)
            {
                const char* y1 = team1[10].name;
            }
        }
        for(i=12;i<MAXTEAMS;i++)
        {
            if(team1[11].scored > team1[i].scored)
            {
                const char* y1 = team1[11].name;
            }
        }
        for(i=13;i<MAXTEAMS;i++)
    {
        if(team1[12].scored > team1[i].scored)
        {
            const char* y1 = team1[12].name;
        }
    }
        for(i=14;i<MAXTEAMS;i++)
        {
            if(team1[13].scored > team1[i].scored)
            {
                const char* y1 = team1[13].name;
            }
        }
        if(team1[14].scored > team1[15].scored)
        {
            const char* y1 = team1[14].name;
        }
        else
        {
            const char* y1 = team1[15].name;
        }
    
    
    printf("y1 is %s",const char* y1);
    return(0);
    }
    Last edited by ingeniousreader; 03-06-2012 at 03:17 PM.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Declare your variable BEFORE all of the if/elses, at the beginning of the function. Make all of the if statements not declare anything, just assign, and then get rid of the random useless typecast in the printf. It should look like this:

    Code:
    const char *y1;
    
    if (....) {
       y1 = .....;
    }
    
    ....
    
    printf("y1 is %s", y1);
    Also, you really need to work on getting rid of the repeated code.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    thanks ill try that now and get back to u in a few minutes

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    ok well the warning message unused variable "y1" is gone anyway. HOWEVER i now have an error message saying( 'y1' undeclared first use in this function.) and another error message saying (each undeclared identifier is reported only once for each function it appears in). so i assume that if i fix the first error message it will be fine. but how will i fix the first error message?

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    this is what the refined code looks like
    Code:
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAXTEAMS 16
    typedef struct team
    {
        char name[50];
        int points;
        int scored;
    }teams;
    
    
    
    
    teams team1[MAXTEAMS] = {{"a"},{"b"},{"c"},{"d"},{"e"},{"f"},{"g"},{"h"},{"i"},{"j"},{"k"},{"l"},{"m"},{"n"},{"o"},{"p"}};
    
    
    int main()
     {
         team1[0].scored = 0;
         team1[1].scored = 0;
         team1[2].scored = 0;
         team1[3].scored = 0;
         team1[4].scored = 0;
         team1[5].scored = 0;
         team1[6].scored = 0;
         team1[7].scored = 0;
         team1[8].scored = 0;
         team1[9].scored = 0;
         team1[10].scored = 0;
         team1[11].scored = 0;
         team1[12].scored = 0;
         team1[13].scored = 0;
         team1[14].scored = 0;
         team1[15].scored = 0;
        int i;
    
    
                for(i=0;i<MAXTEAMS;i++)
                {
                    printf("enter team:\n");
                    scanf("%s",team1[i].name);
                }
                printf("welcome to the champions league\n\n");
    
    
    
    
                         for(i=1;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[0].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[0].name);
                            scanf("%d",&team1[0].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[0].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                                 for(i=2;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[1].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[1].name);
                            scanf("%d",&team1[1].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[1].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=3;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[2].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[2].name);
                            scanf("%d",&team1[2].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[2].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=4;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[3].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[3].name);
                            scanf("%d",&team1[3].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[3].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=5;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[4].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[4].name);
                            scanf("%d",&team1[4].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[4].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=6;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[5].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[5].name);
                            scanf("%d",&team1[5].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[5].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=7;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[6].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[6].name);
                            scanf("%d",&team1[6].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[6].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=8;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[7].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[7].name);
                            scanf("%d",&team1[7].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[7].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=9;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[8].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[8].name);
                            scanf("%d",&team1[8].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[8].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=10;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[9].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[9].name);
                            scanf("%d",&team1[9].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[9].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=11;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[10].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[10].name);
                            scanf("%d",&team1[10].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[10].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=12;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[11].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[11].name);
                            scanf("%d",&team1[11].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[11].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=13;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[12].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[12].name);
                            scanf("%d",&team1[12].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[12].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                          for(i=14;i<MAXTEAMS;i++)
                         {
                            printf("%s vs %s\n",team1[13].name,team1[i].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[13].name);
                            scanf("%d",&team1[13].scored);
                            printf("%s's score:",team1[i].name);
                            scanf("%d",&team1[i].scored);
                            team1[13].scored += team1[0].scored;
                            team1[i].scored += team1[i].scored;
                         }
                            printf("%s vs %s\n",team1[14].name,team1[15].name);
                            printf("--------------\n");
                            printf("%s's score:",team1[14].name);
                            scanf("%d",&team1[14].scored);
                            printf("%s's score:",team1[15].name);
                            scanf("%d",&team1[15].scored);
                            team1[14].scored += team1[0].scored;
                            team1[15].scored += team1[i].scored;
    
    
    
    
    for(i=1;i<MAXTEAMS;i++)
    {
        const char* y1;
        if(team1[0].scored > team1[i].scored)
        {
             y1 = team1[0].name;
        }
    }
        for(i=2;i<MAXTEAMS;i++)
        {
            if(team1[1].scored > team1[i].scored)
            {
                 y1 = team1[1].name;
            }
        }
        for(i=3;i<MAXTEAMS;i++)
        {
            if(team1[2].scored > team1[i].scored)
            {
                 y1 = team1[2].name;
            }
        }
        for(i=4;i<MAXTEAMS;i++)
    {
        if(team1[3].scored > team1[i].scored)
        {
             y1 = team1[3].name;
        }
    }
        for(i=5;i<MAXTEAMS;i++)
        {
            if(team1[4].scored > team1[i].scored)
            {
                 y1 = team1[4].name;
            }
        }
        for(i=6;i<MAXTEAMS;i++)
        {
            if(team1[5].scored > team1[i].scored)
            {
                 y1 = team1[5].name;
            }
        }
        for(i=7;i<MAXTEAMS;i++)
    {
        if(team1[6].scored > team1[i].scored)
        {
             y1 = team1[6].name;
        }
    }
        for(i=8;i<MAXTEAMS;i++)
        {
            if(team1[7].scored > team1[i].scored)
            {
                y1 = team1[7].name;
            }
        }
        for(i=9;i<MAXTEAMS;i++)
        {
            if(team1[8].scored > team1[i].scored)
            {
                 y1 = team1[8].name;
            }
        }
        for(i=10;i<MAXTEAMS;i++)
    {
        if(team1[9].scored > team1[i].scored)
        {
             y1 = team1[9].name;
        }
    }
        for(i=11;i<MAXTEAMS;i++)
        {
            if(team1[10].scored > team1[i].scored)
            {
                 y1 = team1[10].name;
            }
        }
        for(i=12;i<MAXTEAMS;i++)
        {
            if(team1[11].scored > team1[i].scored)
            {
                 y1 = team1[11].name;
            }
        }
        for(i=13;i<MAXTEAMS;i++)
    {
        if(team1[12].scored > team1[i].scored)
        {
            y1 = team1[12].name;
        }
    }
        for(i=14;i<MAXTEAMS;i++)
        {
            if(team1[13].scored > team1[i].scored)
            {
                y1 = team1[13].name;
            }
        }
        if(team1[14].scored > team1[15].scored)
        {
          y1 = team1[14].name;
        }
        else
        {
             y1 = team1[15].name;
        }
    
    
    printf("y1 is %s",y1);
    return(0);
    }

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    No! Stop multi/triple posting! So ........ing annoying you do that in all of your threads

    And I said declare it outside and before the loops, use a bit of logic here. If it is treated as undeclared because of scope issues in one loop, it is in all of the loops.

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    oh my goddd it worked thanks

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Any variable you declare inside a set of curly braces can only be seen within that set of curly braces. That means that stuff declared at the top of your function (e.g. int i; on line 36) can be seen anywhere in main*. But the char *y you declare on line 217 can only be seen within that for loop (lines 217-221). That means it's also visible to the one if statement inside that for loop, but nowhere else in the program. From memcpy's post (emphasis mine):
    Quote Originally Posted by memcpy
    Declare your variable BEFORE all of the if/elses, at the beginning of the function


    * I say anywhere in main, but there is a caveat. It's possible to "shadow" a variable by declaring one with the same name in an inner scope (imagine you also had a variable named i declared inside one of your if statements). That means if you can see the one in the inner scope, there is no way to access any variables of the same name in scope outside that one.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > there is no way to access any variables of the same name in scope outside that one.
    And that's the reason why the unused variable warning occurs. As the compiler says, you aren't actually using the outer scoped variable.

    > I say anywhere in main
    Technically, you're not supposed to mix declarations and code, as per C standard. (type -Wall -pedantic and see for yourself)

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    oh nooooo.. for gods sake y1 is supposed to print the name of the team with the highest score but it prints a 'o'

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I can see a lot of reasons and places where you're accessing variables that haven't been initialized (wtf is up with the first loop?) and just more general kludginess in your code.

    It also seems pointless to continue to try to help when you refuse to fix that huge repeated mess. Post back when you're using nested loops.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by memcpy View Post
    Technically, you're not supposed to mix declarations and code, as per C standard. (type -Wall -pedantic and see for yourself)
    Actually, it's permitted in the now 13-year-old C99 standard. GCC isn't fully C99 compliant and doesn't operate in that mode by default. Add a -std=c99 and see for yourself .

    Quote Originally Posted by ingeniousreader View Post
    oh nooooo.. for gods sake y1 is supposed to print the name of the team with the highest score but it prints a 'o'
    Well, you're not actually doing anything close to tracking the maximum score. All your for loops are independent, so the for loop starting at i=2 can override any score comparisons from the first for loop, and so on. The only thing that ends up mattering in your code is the last if/else:
    Code:
    if(team1[14].scored > team1[15].scored)
    {
        y1 = team1[14].name;
    }
    else
    {
        y1 = team1[15].name;
    }
    y1 is only ever going to be team1[14].name or team1[15].name. All the work in the preceding for loops is thrown away. Google "c finding array maximum" or some such. Practice on an array of integers, when you understand how that code works, adapt it to your football league code.

    By the way, team1 is a horrible name for this array. Why not premier_league or all_teams or something that actually describes what it is?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. expected primary-expression before ']'
    By ooopa in forum C++ Programming
    Replies: 4
    Last Post: 11-14-2011, 09:35 AM
  2. error: expected an expression
    By cdmstr in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2011, 02:00 PM
  3. Expected primary expression
    By MarlonDean in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2008, 03:43 PM
  4. Expected primary expression?
    By Beowolf in forum C++ Programming
    Replies: 17
    Last Post: 11-11-2007, 09:57 PM
  5. expected constant expression
    By Brian in forum C Programming
    Replies: 4
    Last Post: 12-03-2003, 03:30 PM