如何将主函数的内容移动到其他函数并保留功能?

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

我目前正在尝试将“main”函数的内容移动到它自己的函数中,这样我就可以重复事情,并且对发生的事情和时间有更多的选择。功能完成后,我还想在每次购买商品时增加一个整数,以便可以使用某种库存命令进行检查。

我犯了一个错误,即有效地将我以前在 main 中的内容复制粘贴到我想要成为它自己的函数中。不幸的是,这实际上是我第一次尝试声明除 main 之外的函数,该函数不是本网站上其他人给我的,所以我真的不知道如何声明它们。我猜声明不正确/缺少数据,导致出现问题。

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

//Attempting to declare the function early, which is the only thing CS50 has taught me about custom functions thus far.
void itemshop(int buckaroonies);

//Struct given to me in another post
struct item
    {
        string name;
        int price;
        string description;
    };

//Part of aformentioned struct
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)
{
    //Item struct
     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]);

    //Integers for items once they are purchased
    int buckaroonies = 500;
    int matchb = 0;
    int woolh = 0;
    int heavyc = 0;
    int canf = 0;

    void itemshop();
}

//This is probably the worst part, I just copy-pasted what I had in main into this function, and likely declared it incorrectly as well
int itemshop(int buckaroonies)
{
    //Dialogue and selection
    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);
    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!" };


    //Get item details from selection, error comes from here
    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].name, items[index].price, items[index].description);

            if(c == 'y' || c == 'Y')

                {
                    printf("Pleasure doing business with you, pal. -%i Buckeroonies\n", items[index].price);
                    buckaroonies = buckaroonies - items[index].price;
                }
            else if (c == 'n' || c == 'N')
                {
                    printf("Alright, feel free to keep browsing.\n");
                }
            else
                {
                    printf("Hey, uh... I don't know what that means.\n");
                }
        }
}
c cs50
1个回答
0
投票

您在主文底部写了

void itemshop();
。这是一个声明,而不是函数调用。当您调用函数时,不要直接在函数名称之前写入类型(即
void
int
)。仔细将该行与代码中的所有其他函数调用进行比较(即当您调用
printf
strcmp
时)。

删除

void
,然后解决编译报告给您的下一个错误,即您忘记在函数中包含参数。

© www.soinside.com 2019 - 2024. All rights reserved.