dimanche 26 septembre 2021

Compare strings - C

I hope that someone can help me since I'm clueless about the problem in my code. I am a new starter in programming and a week ago I tried an exercise which I completely failed. I tried to compare the elements in the array with the input text on the compiler. But the ("if") is always giving a false. Even if I write down an existing element of the array on the compiler. I know that there must be a lot of logical mistakes but I am really helpless. I am really looking for your answers. Thanks for the help!

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

struct category {
    char item_name[50];
    int item_quantity;
    float item_price;
};

void items_search (struct category a[], int length)
{
    char answer[] = { "yes" };
    int number = 0;
    int item_quantity = 0;
    char name[200];
    int i = 0;

    printf ("What are you looking for?\n");
    scanf ("%s", &name);

    if (a[i].item_name == name) {
        printf ("What is the desired number of the item?");
        scanf ("d", &number);
        fflush (stdin);
        if (number == a[i].item_quantity) {
            printf ("Order is possible. Should the order be placed?\n");
            scanf ("%s", &answer);
            fflush (stdin);
            if (answer == "yes") {
                puts ("The order is placed");
            }
            else {
                puts ("Order was cancelled");
            }
        }
        else {
            printf ("There are only %d pieces of the item. Should the "
                    "order be canceled?\n", a[i].item_quantity);
            scanf ("%s", &answer);
            fflush (stdin);
            if (answer == "yes") {
                puts ("Order was cancelled. Should be bought later?");
                scanf ("%s", &answer);
                fflush (stdin);
                if (answer == "yes") {
                    puts ("The order is placed");
                }
                else {
                    puts ("The order was cancelled");
                }
            }
            else {
                puts ("The order is placed");
            }
        }
    }
    else {
        printf ("The specified item does not exist\n");
    }
}

int main (void)
{
    struct category laptops[] = {
        {"Asus_365", 7, 499.00},
        {"Lenovo_L49", 30, 699.91},
        {"HP_Alien", 20, 649.99},
        {"Acer Alpha touch", 10, 899.99}
    };
    
    items_search (laptops, sizeof (laptops) / sizeof (struct category));
}

Aucun commentaire:

Enregistrer un commentaire