-2

I get these errors with this code: in void teamEnter(){

[Error] expected identifier or '(' before string constant

[Error] 'ans1' undeclared (first use in this function)

[Error] 'ans2' undeclared (first use in this function)

Actually I want to subtract 2 teams winning percentage. I tried many ways but i couldnt find how can i equal team name with teamWinPercentage and then subtract first team to second team. Is there something different way that i tried now. I get errors in this code and i think this code will not reach my main goal. Thanks..


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int mainMenuChoose, leagueMenuChoose, leagueSubtractMenuChoose, returnMainMenu;

void mainMenu(){
printf("\n\tChoose");
printf("\n_____________________________________");
printf("\n1- see league");
printf("\n2- Compare Teams");

printf("\n\nChoose what you want : ");
scanf("%d",&mainMenuChoose);

switch(mainMenuChoose){
    case 1:
        leagueMenu();
        break;
    case 2:
        leagueSubtractMenu();
        break;
    default:
        printf("\nPlease enter correctly!");
 }
}

void leagueMenu(){
printf("\n\n\tLIG Menu");
printf("\n_____________________________________");
printf("\n1- See Team");
printf("\n2- Back to Main Menu");
printf("\n\n Choose: ");
scanf("%d",&leagueMenuChoose);

switch(leagueMenuChoose){
    case 1:
        seeTeams();
        break;
   case 2:
        mainMenu();
        break;
    default:
        printf("\nPlease enter correctly!!");
  }
 }

void leagueSubtractMenu(){
printf("\n\n   League subtract menu");
printf("\n_____________________________________");
printf("\n1- Enter Teams");
printf("\n2- Genel Menuye Donme Islemi");
printf("\n\nChoose: ");
scanf("%d",&leagueSubtractMenuChoose);

switch(leagueSubtractMenuChoose){
    case 1:
        enterTeam();
        break;
    
    case 2:
        mainMenu();
        break;
    default:
        printf("\nPlease enter correctly!");
  }
 }
 void enterTeam(){
    /*
printf("\n 1 - Arsenal");
printf("\n 2 - Aston Villa");
printf("\n 3 - Brentford");
printf("\n 4 - Brighton & Hove Albion");
printf("\n 5 - Burnley");
printf("\n 6 - Chelsea");
printf("\n 7 - Crystal Palace");
printf("\n 8 - Everton");
printf("\n 9 - Leeds United");
printf("\n 10 - Leicester City");
printf("\n 11 - Liverpool");
printf("\n 12 - Manchester City");
printf("\n 13 - Manchester United");
printf("\n 14 - New Castle United");
printf("\n 15 - Norwich City");
printf("\n 16 - Southampton");
printf("\n 17 - Tottenham");
printf("\n 18 - Watford");
printf("\n 19 - West Ham United");
printf("\n 20 - Wolverhampton\n\n");
*/

FILE *pFile;
if (pFile !=NULL){

char *teams[]={"Arsenal","Aston Villa","Brentford","Brighton & Hove Albion","Burnley","Chealsea","Crystal Palace",
    "Everton","Leeds United","Leicester City","Liverpool","Manchester City","Manchester United",
    "New Castle United","Norwich City","Southampton","Tottenham","Watford","West Ham United","Wolverhampton"};
    char ans1[25];char ans2[25];


float teamWinPerc[20]={0.642,0.441,0.430,0.500,0.385,0.685,0.485,0.367,0.371,0.455,0.828,0.842,
    0.567,0.444,0.228,0.430,0.614,0.228,0.541,0.500};

printf("\nCarefull for case type!\n");  
printf("Enter first team: ");
scanf("%s",&ans1);
    if(strcmp(teams,ans1)==0){
        printf("Enter second team: ");
        scanf("%s",&ans2);
        if(strcmp(teams,ans2)==0){
            printf("%s - %s",ans1,ans2);
        }
        else{
        printf("Wrong data!");
            
        printf("\n Back to main menu with 0 or exit with different value: ");
            scanf("%d",&returnMainMenu);

            while(returnMainMenu==0){
            mainMenu();
            }
        }
    }
    else{
        printf("Wrong Data!");
        
        printf("\n Back to main menu with 0 or exit with different value: ");
            scanf("%d",&returnMainMenu);

            while(returnMainMenu==0){
            mainMenu();
            }
    }
    /*
    int i;
    for(i=1;i<21;i++){
        printf("[%d] = %s\n",i,takimAd[i]);
        printf("%d. Takimi giriniz:",i+1);
        
    }*/
        //scanf("%s",takimAd[20]);

    //fscanf(pFile, "%d %s %3.f",&sira,&name,&ky);
    

    //for(sira=1;sira<21;sira++){}
    
    /*while(fgets(name, sizeof(name),pFile)){
        fscanf(pFile, "%d %s %3.f",&sira,&name,&ky);
        printf("1.Takimi Girin: ");
        scanf("%s",&takim1);
            if(strcmp(name,takim1)==0){
            printf("%s",takim1); 
            fscanf(pFile, "%d %s %3.f",&sira,&name,&ky); 
            }
            
        printf("2.Takimi Girin: ");
        scanf("%s",&takim2);
            if(strcmp(name,takim2)==0){
            printf("%s",takim2);
            fscanf(pFile, "%d %s %3.f",&sira,&name,&ky); 
            }
        printf("%3.f - %3.f = %3.f",ky-ky);
    }*/
    
}

else{
    printf("File cant open.\n");
}

//char Arsenal=(float) 0.642;
//char Astonvilla=(float)0.441;
/*chelsea,manchesteru,tottenham,
manchesterc,westham,wolverhampton,brighton,
crystalpalace;*/

/*printf("Karsilastirmak istediginiz takimlari secin:\n");
printf("1. Takimi secin:\n ");
scanf("%c",&Arsenal);
printf("2. Takimi secin:\n ");
scanf("%c",&Astonvilla);
printf("%.3f - %.3f",Arsenal,Astonvilla);
*/
return 0;
}

void seeTeams(){
FILE * fPointer;
fPointer =fopen("takimlar.txt","r+");
char singleLine[200];

while(!feof(fPointer)){
fgets(singleLine,200,fPointer);
puts(singleLine);
}

fclose(fPointer);

printf("\n Back to main menu with 0 or exit with different value:");
scanf("%d",&returnMainMenu);

while(returnMainMenu==0){
    mainMenu();
 }
}
ICED OUT
  • 3
  • 3
  • Can you make a full example out of it? The code isn't even in a function. – mch May 18 '22 at 12:29
  • 1
    Seems that `ans1` and `ans2` are out of scope of the function. Where have you defined them? `teams[]` array definition is missing {} brackets. You must define the strings within {} as you did in `teamWinPerc[]`. – Kozmotronik May 18 '22 at 12:32
  • this code is in a teamEnter(){ function. Sorry i forgot to write. – ICED OUT May 18 '22 at 12:35
  • I've seen ans1 and ans2 but the way of the definition is not correct. Change them like this: `char teams[] = { "Arsenal", "AstonVilla",....., "Wolverhampton" }; // put a new line and then char ans1[25], ans2[25];` – Kozmotronik May 18 '22 at 12:37
  • Besides the missing `{}` it should be an array of pointers: `char *teams[] = { ... }:` – Gerhardh May 18 '22 at 12:47
  • @Gerhardh char *teams[]={"Arsenal,""...",}; char ans1[25];char ans2[25]; i did like you said and i tried to write Arsenal and program said Wrong!. – ICED OUT May 18 '22 at 12:53
  • Show the exact error message (with line number, any contained code fragment etc.) Also please edit to show complete code that can be compiled to reproduce the problem. If that snipped is part of a function, put it in a function. Don't show anything that does not produce the same result. If you don't get compilation errors but unexpected output, also add the input you provide, the output and expected output. Directly in the question, not in comments – Gerhardh May 18 '22 at 12:57
  • You should enable compiler warnings. For GCC use `-Wall -Wextra -pedantic`. `(strcmp(teams,ans2)` can never work because `teams` is not a string but an array of pointers to string. You must compare each string `teams[x]` separately in a loop. – Gerhardh May 18 '22 at 12:59
  • 1
    @ICED OUT, `scanf("%s",&ans1);` will not work to enter a name with spaces like `"Aston Villa"`. Research `fgets()`. – chux - Reinstate Monica May 18 '22 at 13:50
  • 1
    @ICED OUT, save time and enable all warnings. – chux - Reinstate Monica May 18 '22 at 13:51
  • I had tremendous difficulty reading your code. Troubleshooting is easier for everyone (yourself included) when you format your code properly. What constitutes "proper formatting" is up for debate, but consistent indentation is pretty widely acknowledged as key. – Chris May 18 '22 at 14:30

2 Answers2

0
scanf("%s",ans1); // remove & ans1 is address
scanf("%s",ans2); // remove & ans2 is address

// what are you trying with this? I think you want to know
// if ans1 is inside team, you should performs one strcmp() 
// on each teams member element [0]... [19]
if(strcmp(teams,ans1)==0) 

// void function but return integer, you cant do that
void enterTeam()
{
--snip--
return 0; // if it is void don't return integer
}

FILE *pFile; // need to be initialized before to use 
if (pFile !=NULL) // uninitialized

Down below an example of strcmp(), looking for a name in a list

#include <stdio.h>
#include <string.h>

#define NAME_LENGTH 80

char *teams[NAME_LENGTH] =
    {"Arsenal","Aston Villa","Brentford","Brighton & Hove Albion",
    "Burnley","Chealsea","Crystal Palace","Everton","Leeds United",
    "Leicester City","Liverpool","Manchester City","Manchester United",
    "New Castle United","Norwich City","Southampton","Tottenham","Watford",
    "West Ham United","Wolverhampton"};


int is_listed(char *);

int main()
{
    int r = 0;
    char team_name[80];

    printf("Give me a team name, I will verify:  ");
    scanf("%s", team_name);

    r = is_listed(team_name);

    if(r == -1)
    {
        printf("\n Name is not in list!\n");
    }
    else
    {
        printf("\n Name is position %d in the list\n", r);
    }

    return 0;
}

// Return position in the list, -1 if it is not in the list
int is_listed(char *my_name)
{
    int n;
    for(n =0; n<20; n++)
    {
        printf("\n%s ?? %s", teams[n], my_name);
        if(strcmp(teams[n], my_name) == 0)
        {
            return n;
        }
    }
    return -1;
}
-1

I think that a two-dimension array and a struct will be a better solution, but down below a solution just using arrays, atof function convert an array to a float.

#include <stdio.h>
#include <stdlib.h>

#define NAME_LENGTH 80

char teams[][2][NAME_LENGTH] =
  {
    {"Arsenal", "0.642"},
    {"Aston Villa", "0.441"},
    {"Brentford", "0.430"}
};

int main()
{
    float team_a;
    float team_b;

    team_a = atof(teams[0][1]);
    team_b = atof(teams[0][1]);


    if(team_a > team_b)
    {
            printf("The best team is %s with a %f score\n", teams[0][0], team_a);
    }
    else
    {
            printf("The best team is %s with a %f score\n", teams[1][0], team_b);
    }

    return 0;
}
  • Why would you store a float value as a string? Instead a struct holding a string and a `float` would be much easier to use. If you already sugest a struct, showing the worse solution does not seem very useful. – Gerhardh May 18 '22 at 13:38
  • Besides that the main question was about finding the correct teams from the array. You completely skipped that part. – Gerhardh May 18 '22 at 13:40
  • Maybe but that would not make strings holding float values any better and still misses the main question – Gerhardh May 18 '22 at 13:42
  • @DanielSagarra this way i cant choose team and program directly gives the 1 result and dont tell which team is best. – ICED OUT May 18 '22 at 13:51
  • You are right, the first code I saw had some basic mistakes, or I miss understanding the problem, because I try to give a started code to solve the problem, but the actual code needs a new answer. – Daniel H Sagarra May 18 '22 at 13:58
  • @DanielSagarra i know structs but not like advanced level. still thx for the answer – ICED OUT May 18 '22 at 14:11
  • @ICEDOUT you declare leagueSubtractMenu lika varaible int mainMenuChoose, leagueMenuChoose, leagueSubtractMenu, returnMainMenu; after you use it lika a function leagueSubtractMenu(); pleaes check this out. I got a buch of errors and warrings do you have a new version of the code? – Daniel H Sagarra May 18 '22 at 14:16
  • @DanielSagarra sorry i forgot the translate leagueSubtractMenuChoose. I reedited now but warnings not gone. Im new with programming and i cant write codes correct always. sorry my bad. – ICED OUT May 18 '22 at 14:31