作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#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/
我是一名优秀的程序员,十分优秀!