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

ewm0tg9j  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(120)

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

#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);
    }
}

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

8hhllhi2

8hhllhi21#

你的数组(itemspricesdescription)对于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]);
    }
}

字符串
由于全局值不受欢迎,另一种选择是将它们本地化到main并作为参数传递:

int find_item(string item, string items[], size_t 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);
    }
}

相关问题