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();
}
}
CodePudding user response:
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;
}
CodePudding user response:
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