我得到这些错误与这段代码:
[错误]期望的标识符或'('前字符串常量
[Error] 'ans1'未声明(在此函数中首次使用)
[Error] 'ans2'未声明(在此函数中首次使用)
实际上我想减去两队的胜率。我尝试了很多方法,但我找不到如何将球队名称与teamWinPercentage相等,然后将一队减去第二队。有什么不同的方法,我现在尝试。我在这段代码中得到错误,我认为这段代码不会达到我的主要目标。谢谢. .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int mainMenuChoose, leagueMenuChoose, leagueSubtractMenuChoose, returnMainMenu;
void mainMenu(){
printf("ntChoose");
printf("n_____________________________________");
printf("n1- see league");
printf("n2- Compare Teams");
printf("nnChoose what you want : ");
scanf("%d",&mainMenuChoose);
switch(mainMenuChoose){
case 1:
leagueMenu();
break;
case 2:
leagueSubtractMenu();
break;
default:
printf("nPlease enter correctly!");
}
}
void leagueMenu(){
printf("nntLIG Menu");
printf("n_____________________________________");
printf("n1- See Team");
printf("n2- Back to Main Menu");
printf("nn Choose: ");
scanf("%d",&leagueMenuChoose);
switch(leagueMenuChoose){
case 1:
seeTeams();
break;
case 2:
mainMenu();
break;
default:
printf("nPlease enter correctly!!");
}
}
void leagueSubtractMenu(){
printf("nn League subtract menu");
printf("n_____________________________________");
printf("n1- Enter Teams");
printf("n2- Genel Menuye Donme Islemi");
printf("nnChoose: ");
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 - Wolverhamptonnn");
*/
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] = %sn",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();
}
}
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
下面是strcmp()的示例,在列表
中查找名称#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 listn", 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;
}
我认为二维数组和结构体将是一个更好的解决方案,但下面的解决方案只是使用数组,atof
函数将数组转换为浮点数。
#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 scoren", teams[0][0], team_a);
}
else
{
printf("The best team is %s with a %f scoren", teams[1][0], team_b);
}
return 0;
}