gpt4 book ai didi

c - 使用结构时跳过 "IF"函数

转载 作者:行者123 更新时间:2023-11-30 20:00:56 25 4
gpt4 key购买 nike

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

struct res{
int win;
int defeat;
};


struct tenis_player{
char name[20];
char last_name[20];
int pos;
char hand[10];
struct res comp;
}Ten[20];

int main(){
int i, n;
float comparison, br=0;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s",&Ten[i].name);
scanf("%s",&Ten[i].last_name);
scanf("%d",&Ten[i].pos);
scanf("%s",&Ten[i].hand);
scanf("%d/%d",&Ten[i].comp.win,&Ten[i].comp.defeat);
}
printf("Results:\n");
for(i=0; i<n; i++){
comparison = Ten[i].comp.win/Ten[i].comp.defeat;
if(Ten[i].hand == "left" && comparison>3){
printf("%s %s %f",Ten[i].name,Ten[i].last_name,comparison);
br++;
printf("\n");}}
if (br==0)
printf("No tenis players");
return 0;
}

我的代码不断跳过“if(Ten[i].hand == "left"&& Comparison>3)”,无论我输入什么,都只会打印出“No tenis Players”。任何帮助,将不胜感激 ! :)

最佳答案

在 C 中,当你这样做时Ten[i].hand == "left" 您不是在比较字符串,而是在比较指针。在这种情况下,两个指针永远不会(并且不可能)相同。

要比较字符串,您需要使用 strcmp功能:

if(strcmp(Ten[i].hand, "left")==0 && comparison>3){...}

关于c - 使用结构时跳过 "IF"函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38078322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com