-1

I'm a beginner in c programming and i'have come to a point (pretty ridiculous) that i'm stuck at an if statement between two strings.

In this program, i want the user to input a lesson of a student saved in a structure, then it should search for students with the same lesson and print them on screen.

My problem is at if statement, for some reason (memory address maybe?) when i type the same characters, it sees them for different strings. The code just "skips" the if part. Although it works when i declare if(record[0].lesson!="maths") for instance.

i have tried lots of things such as strcmp,

if (record[0].lesson==record[1].lesson)

if(record[0].lesson=="maths")... and other.

I would highly appreciate it if you could help me. thank you very much.

# include <stdio.h>
# include <string.h>
typedef struct student{
    char name[10];
    char lesson[10];
}students;

int main()
{
    students record[10];
    char less;
    strcpy(record[0].name,"James");
    strcpy(record[0].lesson,"maths");
    strcpy(record[1].name,"John");
    strcpy(record[1].lesson,"maths");
    strcpy(record[2].name,"Harry");
    strcpy(record[2].lesson,"C/C++");
    printf("Give Lesson\n");
    scanf("%s",less);
    for(int i=0;i<3;i++){
    if(less==record[i].lesson)
    printf("%s\n",record[i].name);}


    return 0;
}
Suryakant Sharma
  • 3,852
  • 1
  • 25
  • 47
  • 3
    With `less==record[i].lesson` you compare a single character (`less`) with a pointer (`record[i].lesson` decays to a pointer to its first element). – Some programmer dude Mar 28 '18 at 07:57
  • 2
    Oh, and `scanf("%s",less);` is *very* wrong. Perhaps you should go find a beginners book and start reading it from the start? – Some programmer dude Mar 28 '18 at 07:58
  • You should use `strcmp`. [SO on strcmp](https://stackoverflow.com/questions/19479232/comparing-two-strings-problems-with-strcmp) – Adder Mar 28 '18 at 07:58
  • 1
    `less` is incorrectly as `char`. It should be `char less[256] = {0};` Also, use `strcmp` for string comparison. – sameerkn Mar 28 '18 at 08:00
  • `char less;`-> `char less[10];` and `if(less==record[i].lesson)` -> `if (strcmp(less, record[i].lesson) == 0)`. Read the chapter dealing with strings in your C text book. Also learn how code should be formatted. The samples in your C text book are formatted correctly. – Jabberwocky Mar 28 '18 at 08:02
  • Thank you very much !!! YOU SAVED ME! – Μιχάλης Μονές Mar 28 '18 at 08:08

1 Answers1

1

You want this:

  • better formatting
  • less declared correctly as char less[10];
  • using strcmp for string comparision instead of ==

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

typedef struct student{
    char name[10];
    char lesson[10];
} students;

int main()
{
    students record[10];
    char less[10];
    strcpy(record[0].name, "James");
    strcpy(record[0].lesson, "maths");
    strcpy(record[1].name, "John");
    strcpy(record[1].lesson, "maths");
    strcpy(record[2].name, "Harry");
    strcpy(record[2].lesson, "C/C++");
    printf("Give Lesson\n");
    scanf("%s", less);

    for (int i = 0; i < 3; i++) {
      if (strcmp(less, record[i].lesson) == 0)
      printf("%s\n",record[i].name);
    }

    return 0;
}

There is still much room for improvement.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115