Search code examples
ccs50

How do I make C check for a handful of specific strings after a get_string input?


I'm on week 2 of CS50, as new to programming as it gets, and was wondering if there was a way to check for a specific strings in the same was as you can with integers?

I'm making a text-based item shop as practice of the syntax and ran into the issue of not knowing how to take the input of the item the person wants and redirect it to a confirm or deny + description of the item.

I'd love to include what I've tried, but considering I don't know where to start, I don't have anything to put here yet. My current code, for context on the simplicity of what I'm doing.

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int buckaroonies = 500;
    printf("Hey chum, welcome to the item shop. You've got a handful 'o coin on ya, huh?\nYou came to the right place, we got the best wares in town!\n\n");
    printf("Store:\n\nMatchbox - 50\nWool Hat - 125\nHeavy Coat - 250\nCanned Food - 25\n");
    string select = get_string ("What'll it be? You have %i buckaroonies, pal.\n", buckaroonies);
}

Solution

  • Considering you are taking CS50, a good page to bookmark is https://manual.cs50.io/ which contains largely simplified versions of the manuals you would find on any Unix-like operating system (e.g., https://man.openbsd.org/).

    Scanning this page, under string.h you'll see:

    • strcmp - compare two strings

    The strcmp page explains that the function has the prototype of

    int strcmp(string s1, string s2);
    

    and that it returns

    0 if s1 is the same as s2

    In use, this looks like

    if (0 == strcmp("Matchbox", select))
    {
        puts("That will be 50 buckaroonies!");
    }
    else if (0 == strcmp("Wool Hat", select))
    {
        puts("That will be 125 buckaroonies!");
    }
    

    In practice, you likely want to loop through these options in some way, instead of writing each comparison by hand. Consider the following example that uses two arrays of equal length representing the items in the shop, and their prices:

    #include <cs50.h>
    #include <stdio.h>
    #include <string.h>
    
    int string_to_price(string item)
    {
        string items[] = { "Matchbox", "Wool Hat", "Heavy Coat", "Canned Food" };
        int prices[] = { 50, 125, 250, 25 };
    
        for (size_t i = 0; i < (sizeof items / sizeof *items); i++)
        {
            if (0 == strcmp(items[i], item))
            {
                return prices[i];
            }
        }
    
        /* item does not exist */
        return -1;
    }
    
    int main(void)
    {
        int buckaroonies = 500;
        printf("Hey chum, welcome to the item shop. You've got a handful 'o coin on ya, huh?\nYou came to the right place, we got the best wares in town!\n\n");
        printf("Store:\n\nMatchbox - 50\nWool Hat - 125\nHeavy Coat - 250\nCanned Food - 25\n");
        string select = get_string ("What'll it be? You have %i buckaroonies, pal.\n", buckaroonies);
    
        int price = string_to_price(select);
    
        if (-1 == price)
        {
            puts("We aint got that, pal.");
        }
        else
        {
            printf("That %s will cost ya %i buckaroonies!\n", select, price);
        }
    }
    
    Hey chum, welcome to the item shop. You've got a handful 'o coin on ya, huh?
    You came to the right place, we got the best wares in town!
    
    Store:
    
    Matchbox - 50
    Wool Hat - 125
    Heavy Coat - 250
    Canned Food - 25
    What'll it be? You have 500 buckaroonies, pal.
    Canned Food
    That Canned Food will cost ya 25 buckaroonies!
    

    Later on, when you are more comfortable, it would be more robust to replace the two arrays with a single array of structures.

    See also: How do I determine the size of my array in C?