如何将另一个数组添加到集合中,特别是字符串?

问题描述 投票:0回答:1

这是我在这里发表的上一篇文章的后续内容,现在询问如何将另一个字符串数组添加到我之前基于文本的物品商店。上次有人非常有帮助,告诉我我可以通过数组跟踪价格和商品名称,虽然我仍然不完全理解,但我正在尝试利用。我还应该先说一下,我的代码非常简单,因为我才刚刚开始编程。

我的代码如下,

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

int string_to_price(string item)
{
    //The array/s that's catching me up.
    string items[] = { "Matchbox", "Wool Hat", "Heavy Coat", "Canned Food" };
    int prices[] = { 50, 125, 250, 25 };
    string description[] = { "They're cheap, and they'll keep you warm.\n", "A nice, handknit, cozy hat. Thank my ma for that!\n", "This coat'll keep you cozy through the night no matter the situation!\n", "It don't taste too good, but it'll last you a lifetime. Eat up!" };

    for (size_t i = 0; i < (sizeof items / sizeof *items); i++)
    {
        if (0 == strcmp(items[i], item))
        {
            return prices [i];
        }
    }
    //Item not available
    return -1;
}

int main(void)
{
    //Dialogue, then ask for item selection
    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);

    //Get item price from selection
    int price = string_to_price(select);

    //Return dialogue based on selected item.
    if (-1 == price)
    {
        printf("We aint got that, pal.\n");
    }
    else
    {
        char c = get_char ("That %s'll cost ya %i buckaroonies. %s You want it?\n", select, price, description);
    }
}

我肯定做错了什么,因为我收到一条错误消息,告诉我“描述”不是声明的字符串。

c cs50
1个回答
0
投票

您的数组(

items
prices
description
)是string_to_price函数的
本地
- 这意味着它们只能在该函数内部访问。
main
看不到它们。有几个修复。

此外,该函数可能应该返回索引,而不是价格,以便您可以访问其他数组。

使数组全局化:

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

string items[] = { "Matchbox", "Wool Hat", "Heavy Coat", "Canned Food" };
int prices[] = { 50, 125, 250, 25 };
string description[] = { "They're cheap, and they'll keep you warm.\n", "A nice, handknit, cozy hat. Thank my ma for that!\n", "This coat'll keep you cozy through the night no matter the situation!\n", "It don't taste too good, but it'll last you a lifetime. Eat up!" };
const size_t num_items = sizeof(items) / sizeof(items[0]);

int find_item(string item)
{
    for (size_t i = 0; i < num_items ; i++)
    {
        if (0 == strcmp(items[i], item))
        {
            return i;
        }
    }
    //Item not available
    return -1;
}

int main(void)
{
    //Dialogue, then ask for item selection
    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);

    //Find item from selection
    int index = find_item(select);

    //Return dialogue based on selected item.
    if (-1 == item)
    {
        printf("We aint got that, pal.\n");
    }
    else
    {
        char c = get_char ("That %s'll cost ya %i buckaroonies. %s You want it?\n", items[index], prices[index], description[index]);
    }
}

由于全局值不受欢迎,另一种选择是使 then 成为

main
的本地值并作为参数传递:

int find_item(string item, string items[], int num_items)
{
    for (size_t i = 0; i < num_items; i++)
    {
        if (0 == strcmp(items[i], item))
        {
            return i;
        }
    }
    //Item not available
    return -1;
}

int main(void)
{
    string items[] = { "Matchbox", "Wool Hat", "Heavy Coat", "Canned Food" };
    int prices[] = { 50, 125, 250, 25 };
    string description[] = { "They're cheap, and they'll keep you warm.\n", "A nice, handknit, cozy hat. Thank my ma for that!\n", "This coat'll keep you cozy through the night no matter the situation!\n", "It don't taste too good, but it'll last you a lifetime. Eat up!" };
    const size_t num_items = sizeof(items) / sizeof(items[0]);

    //Dialogue, then ask for item selection
    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);

    //Find item from selection
    int index = find_item(select, items, num_items);

    //Return dialogue based on selected item.
    if (-1 == index)
    {
        printf("We aint got that, pal.\n");
    }
    else
    {
        char c = get_char ("That %s'll cost ya %i buckaroonies. %s You want it?\n", items[index], prices[index], description[index]);
    }
}

最终,您将学习结构,这将使您能够将数据保存在一起。

struct item {
    string name;
    int price;
    string description;
};

int find_item(string item_name, struct item items[], size_t num_items)
{
    for (size_t i = 0; i < num_items ; i++)
    {
        if (0 == strcmp(items[i].name, item_name))
        {
            return i;
        }
    }
    //Item not available
    return -1;
}

int main(void)
{
   struct item items[] = {
        {"Matchbox", 50, "They're cheap, and they'll keep you warm."},
        {"Wool Hat", 125, "A nice, handknit, cozy hat. Thank my ma for that!"},
        {"Heavy Coat", 250, "This coat'll keep you cozy through the night no matter the situation!"},
        {"Canned Food", 25, "It don't taste too good, but it'll last you a lifetime. Eat up!"}
    };
    const size_t num_items = sizeof(items) / sizeof(items[0]);

    //Dialogue, then ask for item selection
    int buckaroonies = 500;
    puts("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");
    puts("Store:\n");
    for (size_t i = 0; i < num_items; i++) {
        printf("%s - %d\n", items[i].name, items[i].price);
    }
    string select = get_string ("What'll it be? You have %i buckaroonies, pal.\n", buckaroonies);

    //Find item from selection
    int index = find_item(select, items, num_items);

    //Return dialogue based on selected item.
    if (-1 == index)
    {
        puts("We aint got that, pal.");
    }
    else
    {
        char c = get_char("That %s'll cost ya %i buckaroonies. %s You want it?\n", items[index].name, items[index].price, items[index].description);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.