Home > front end >  How can I search for a string value and print the result in linked-list?
How can I search for a string value and print the result in linked-list?

Time:06-17

I was doing a mini-project system for my course and had a problem searching the username from what I inserted into the system. Every time I search for the username it returns no username is found. Is the structure that I have created correctly to store the user input because I'm the first time creating this complex structure. Where is the problem and how can I fix it?

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

#define MAX 100
#define TRUE 1
#define FALSE 0

struct fitnessTracking {
    char activity[50], unit[50];
    int calories;
    float total, weight, height, bmi;
};
struct recordDate {
    int dd, mm, yyyy;
    int duration;
};
struct user {
    char username[50];
    struct fitnessTracking tracker;
    struct recordDate record;
};

struct Number {
    struct Number *ptrnext;
    struct user member;
};

struct Number *newptr, *currentptr;

int main(void) {
    int selection;
    int choice=TRUE;
    char searchValue[20];
    struct Number* head = NULL;

    while(choice==TRUE) {
        printf("\n\n1 - Enter new user");
        printf("\n2 - Search by username");
        printf("\n3 - Display user list");
        printf("\n4 - Delete user from list");
        printf("\n5 - Exit\n");
        printf("\nEnter choice: ");
        scanf("%d",&selection);
        switch(selection) {
            case 1: addUser(&head);
                break;
            case 2: printf("\nSearch username: ");
                scanf(" %[^\n]%*c", &searchValue);
                searchUser(head, searchValue);
                break;
            case 3: listUser(head);
                break;
            case 4: deleteUser();
                break;
            case 5: choice=FALSE;
                break;
            default: printf("\nEnter available choice from above");
        }
    }
  return 0;
}

void addUser(struct Number** head_ref){
    newptr=(struct Number *)malloc(sizeof(struct Number));

    printf("\nEnter name: ");
    scanf(" %[^\n]%*c", &newptr->member.username);
    printf("\nEnter weight: ");
    scanf("%f", &newptr->member.tracker.weight);
    printf("\nEnter height: ");
    scanf("%f", &newptr->member.tracker.height);
    printf("\nEnter activity: ");
    scanf(" %[^\n]%*c", &newptr->member.tracker.activity);
    printf("\nEnter activity duration (in minutes): ");
    scanf("%d", &newptr->member.record.duration);
    printf("\nEnter total: ");
    scanf("%f", &newptr->member.tracker.total);
    printf("\nEnter unit: ");
    scanf(" %[^\n]%*c", &newptr->member.tracker.unit);
    printf("\nEnter calories: ");
    scanf("%d", &newptr->member.tracker.calories);
    printf("\nEnter activity date (dd/mm/yyyy): ");
    scanf("%d/%d/%d", &newptr->member.record.dd, &newptr->member.record.mm, &newptr->member.record.yyyy);
    float bmi = 0;
    float weight = newptr->member.tracker.weight;
    float height = newptr->member.tracker.height;
    bmi = (weight) / (height*height);
    newptr->member.tracker.bmi = bmi;

    //link the  old list to the new node
    newptr->ptrnext = (*head_ref);
    //move head to point to the new node
    (*head_ref) = newptr;

}

void searchUser(struct Number* head, char searchValue){
    char a[20]="Name",b[20]="Weight(kg)", c[20]="Height(m)", d[20]="BMI", e[20]="Activity";
    char f[20]="Duration(minutes)", g[20]="Total", h[20]="Unit", i[20]="Calories(kal)", j[20]="Date";

    struct Number* temp=head;

    printf("%-15s %-10s %-10s %-10s %-15s %-20s %-15s %-15s %-15s %-15s\n", a,b,c,d,e,f,g,h,i,j);
    while(temp != NULL){
        if(temp->member.username == searchValue)
            printf("%-15s %-10.2f %-10.2f %-10.2f %-15s %-20d %-15.2f %-15s %-15d %d/%d/%-15d\n\n", temp->member.username, temp->member.tracker.weight, temp->member.tracker.height, temp->member.tracker.bmi, temp->member.tracker.activity, temp->member.record.duration, temp->member.tracker.total, temp->member.tracker.unit, temp->member.tracker.calories, temp->member.record.dd, temp->member.record.mm, temp->member.record.yyyy);

        temp=temp->ptrnext;
    }
    printf("Sorry, no match found\n");
}

CodePudding user response:

Here you are comparing pointers:

if(temp->member.username == searchValue)

What you do want to do is comparing what they are pointing to, ideally using a ready made function for the purpose.
See https://en.cppreference.com/w/cpp/string/byte/strcmp

  • Related