简体   繁体   中英

comparing two string using a pointer and strcmp

i'm trying to solve this problem but it's not working i think the statement inside 'if' is wrong and i don't know if i can put a pointer inside strcmp like this!!

#include <string.h>
#include <studio.h>

struct PersonCar {
  char pname[20];
  char cModel[20];
  float price;
};
struct PersonCar pc[4];

float calculatePrice(struct PersonCar *p, char *name) {
  p = malloc(sizeof(pc));
  float s = 0;
  for (int i = 0; i < 4; i++) {
    if ((strcmp((p[i].pname), name)) == 0)  //(p+i)->pname
      s += (p + i)->price;                  //(p+i)->price; }
  return s;
}

int main() {
  // entering the element of the array from the user
  char A[20];
  printf("Enter a name : ");
  fgets(A, sizeof(A), stdin);
  printf("the total price of the registered cars for %s =%f\n", A,
         calculatePrice(&pc, A));
}
  • First you need some data (I have included some statically initialized data in my version)

  • Second, you need to eliminate the malloc() statement at the beginning of the function, that modifies the passed pointer to the data and makes it to point to an uninitialized data, that is very unprobable that finds any register that matches.

  • You h ad better to know the size of the data array, and pass the number of entries on it.

  • You need to eliminate the last '\n' from the array read by fgets() . If you don't, it is comparing "smith" against "smith\n" , which will never be equal. I suggest one way below, but you have to be careful and read the man page of strtok , as it modifies the source string, this can be not what you want (while in this case is preciselly what we want)

To illustrate, I have written this sample code (but a full program you can compile and execute) to see the logic.

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

struct PersonCar {
  char *pname;
  char *cModel;
  float price;
};

/* initialized data */
struct PersonCar pc[] = {
    { .pname = "smith",
      .cModel = "foo",
      .price = 10.50F,
    }, {
      .pname = "montgomery",
      .cModel = "bar",
      .price = 20.50F,
    }, {
      .pname = "mckormack",
      .cModel = "blah",
      .price = 35.50F,
    }, {
      .pname = "smith",
      .cModel = "pong",
      .price = 55.50F,
    }, {
      .pname = "phelps",
      .cModel = "ping",
      .price = 75.50F,
    },
};

size_t pc_count = sizeof pc / sizeof pc[0];

float calculatePrice(
        struct PersonCar *p,
        size_t p_count,
        char *name)
{
  float total = 0.0;
  for (int i = 0; i < p_count; i++) {
    if (strcmp(p[i].pname, name) == 0)
        total += p[i].price; /* found */
  }
  /* not found */
  return total;
}

int main()
{
  // entering the element of the array from the user
  char A[80];

  printf("Enter a name : ");
  fgets(A, sizeof(A), stdin);
  char *p = strtok(A, "\n"); /* chop the last \n if present */
  printf("the total price of the registered cars for %s =%f\n",
         p, calculatePrice(pc, pc_count, p));
}

I think you're looking for something like:

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

struct PersonCar {
    char pname[20];
    char cModel[20];
    float price;
};

struct PersonCar pc[4] = {
    {"abc", "", 1.0},
    {"def", "", 2.0},
    {"abc", "", 3.0},
    {"jkl", "", 4.0},
};

float
calculatePrice(struct PersonCar *p, char *name)
{
    float s = 0;
    for( struct PersonCar *e = p + 4; p < e; p++ ){
        if( strcmp((p->pname), name) == 0 ){
            s += p->price;
        }
    }
    return s;
}

int
main(void)
{
    char A[20];
    printf("Enter a name : ");
    fgets(A, sizeof(A), stdin);
    printf("the total price of the registered cars for %s =%f\n", A,
        calculatePrice(pc, A));
}

One glaring issue here is that you're not dealing with the newline in the input, but since I don't know how you're actually initializing the data it's not clear how you want to deal with it.

thank you for your response! but i was entering the data from the use wrong!! i wasn't taking \n in one of them so strcmp wasn't working !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM