-2
#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;
}

My code keeps skipping the "if(Ten[i].hand == "left" && comparison>3)" and just prints out "No tenis players" no matter what I input. Any help would be appreciated ! :)

voluc
  • 1

1 Answers1

4

In C when you do e.g. Ten[i].hand == "left" you are not comparing strings, you are comparing pointers. And in this case the two pointers will (and can) never be the same.

To compare strings you need to use the strcmp function:

if(strcmp(Ten[i].hand, "left")==0 && comparison>3){...}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • that doesn't seem to have done it sadly :s – voluc Jun 28 '16 at 14:19
  • @voluc Then it's time to take out the *debugger* and step through the code line by line to see what it does and what the values of all variables are at each step. – Some programmer dude Jun 28 '16 at 14:53
  • `scanf("%d/%d",&Ten[i].comp.win,&Ten[i].comp.defeat);` Look at this line closely and you will find your problem. – FredK Jun 28 '16 at 15:23
  • im supposed to have " / " in between the numbers since thats what the program thats testing this inputs.. is there another way to make it with the " / " , i have to keep them as int values since i have to divide them later on.. – voluc Jun 29 '16 at 13:16