Thread: Comparing string (strcpy == char)

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    8

    Comparing string (strcpy == char)

    Here is where I take data from file and put it in structure.

    Code:
    
    struct NameRecord{
            char name[31];
            int year;
            int frequency;
            }records[200000];
    Code:
    fp=fopen("malebabynames.csv","r");
     if (fp == NULL){
            printf("Cannot open the malebabynames.csv file\n");
     }else{
    
    
            while (fgets(line_buffer, sizeof(line_buffer), fp)){
    //                printf("%s\n",line_buffer);
                    str = strtok(line_buffer,",");
                    records[currSize].year = atoi(str);
                    str = strtok(NULL,",");
                    strcpy(records[currSize].name,str);
                    //records[currSize].name = str;
                    str = strtok(NULL,"\n");
                    records[currSize].frequency= atoi(str);
    //                printf ("Years: %d\n", records[currSize].year);
                    currSize++;
            }
            fclose(fp);

    Code:
    void setNameYearTotals (char theName[], struct NameRecord records[], int size, int nameTotal[]){
    int a;
    int b,c;
    int x;
    int year = 1920;
    int total;
    char name[31];
    //char str[31];
    //printf("%s\n",records[size].name);
    
    
    for (a=0 ; a < 18 ; ++a){
      for (b=0; b < 5 ; ++b) {
            total = 0;
            year++;;
            for ( x = 0; records[x].year != -1; x++){
            //printf("%s\n",records[x].name);
             if ( year == records[x].year){
                    //printf("Name: %s, Data name: %s\n",theName, records[x].name);
    
    
    
                    if ( theName == records[x].name){ //THIS IF STATEMENT IS NOT RUNNING
                    total = total + records[x].frequency;
                    printf("Total: %d\n",total);
                    }
             }
            }
    //printf("Test: %s\n",name);
     }
    nameTotal[a] = total;
    //printf("%d\n",yearRangeTotal[a]);
    //printf("year: %d\n",year);
    }
    return;
    }

    I tried to compare a string from user input with a structure but for some reason it not working.

    Code:
                    if ( theName == records[x].name){
                    total = total + records[x].frequency;
                    printf("Total: %d\n",total);
                    }
    For example,
    theName = "ERIC" and records[0].name contain "ERIC". However, the if statement say they are not the same.

    I store records structure using strcpy and the user input is char array.

    I also tried:
    Code:
    char name[31]
    name = records[x].name;
    It gives me error:
    assignment3.c:155:8: error: incompatible types when assigning to type ‘char[31]’ from type ‘char *’
    Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In C, you cannot compare strings with ==. You need to use "strcmp". Take special note of the return value of this function.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you can't compare strings that way in C. you have to use strcmp().
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "strcpy(string,char[int])" doesn't work?
    By JonathanS in forum C Programming
    Replies: 4
    Last Post: 10-17-2011, 10:32 AM
  2. Converting char to char*, strcpy problems
    By Gareth321 in forum C Programming
    Replies: 9
    Last Post: 05-24-2011, 12:23 AM
  3. Problems coping a char to a char using strcpy
    By iKlys++ in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2011, 06:39 PM
  4. Using strcpy from const char*
    By Korhedron in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2006, 11:36 AM
  5. umm comparing string to char
    By kennny2004 in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2006, 09:11 PM

Tags for this Thread