如何为C语言文本物品商店添加字符串数组?解决未声明变量报错
解决文本物品商店代码的编译报错问题
你的代码编译报错'description'未声明,原因很明确:
description数组是定义在string_to_price函数内部的局部变量,main函数无法访问这个变量;- 同时当前
string_to_price只返回物品价格,就算能拿到description,也没法对应到用户选中的物品(丢失了索引关联)。
下面给两种解决思路,从简单到规范:
方案1:调整变量作用域,返回物品索引
把物品相关的三个数组移到全局作用域,让两个函数都能访问,同时修改string_to_price返回物品的索引(找不到返回-1),这样就能在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 descriptions[] = { "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!" }; int get_item_index(string item) { int item_count = sizeof(items) / sizeof(items[0]); for (int i = 0; i < item_count; i++) { if (strcmp(items[i], item) == 0) { return i; } } // 找不到物品返回-1 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 item_index = get_item_index(select); if (item_index == -1) { printf("We aint got that, pal.\n"); } else { int price = prices[item_index]; string desc = descriptions[item_index]; char c = get_char ("That %s'll cost ya %i buckaroonies. %s You want it?\n", select, price, desc); // 这里可以加后续的购买逻辑,比如扣钱之类的 } }
方案2:用结构体封装物品信息(更规范)
把物品的名称、价格、描述打包成一个结构体,避免多个数组同步的问题(比如不小心修改了一个数组的长度,其他数组没改就会出错),代码可读性和维护性更好:
#include <cs50.h> #include <stdio.h> #include <string.h> // 定义物品结构体,打包所有相关信息 typedef struct { string name; int price; string description; } Item; // 定义商店的物品列表 Item shop_items[] = { {"Matchbox", 50, "They're cheap, and they'll keep you warm.\n"}, {"Wool Hat", 125, "A nice, handknit, cozy hat. Thank my ma for that!\n"}, {"Heavy Coat", 250, "This coat'll keep you cozy through the night no matter the situation!\n"}, {"Canned Food", 25, "It don't taste too good, but it'll last you a lifetime. Eat up!"} }; Item* find_item(string item_name) { int item_count = sizeof(shop_items) / sizeof(shop_items[0]); for (int i = 0; i < item_count; i++) { if (strcmp(shop_items[i].name, item_name) == 0) { // 返回找到的物品指针 return &shop_items[i]; } } // 找不到返回NULL return NULL; } 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\n"); int item_count = sizeof(shop_items) / sizeof(shop_items[0]); for (int i = 0; i < item_count; i++) { printf("%s - %i\n", shop_items[i].name, shop_items[i].price); } string select = get_string ("What'll it be? You have %i buckaroonies, pal.\n", buckaroonies); Item* selected_item = find_item(select); if (selected_item == NULL) { printf("We aint got that, pal.\n"); } else { char c = get_char ("That %s'll cost ya %i buckaroonies. %s You want it?\n", selected_item->name, selected_item->price, selected_item->description); // 后续购买逻辑示例 if (c == 'y' || c == 'Y') { if (buckaroonies >= selected_item->price) { buckaroonies -= selected_item->price; printf("Enjoy your %s! You now have %i buckaroonies left.\n", selected_item->name, buckaroonies); } else { printf("You don't have enough coin, pal!\n"); } } else { printf("No worries, come back anytime!\n"); } } }
额外说明
- 方案2里打印商店内容不用硬编码,直接遍历结构体数组即可,后续新增物品只需要修改
shop_items数组,不用改打印代码,更灵活; - 结构体方式更符合结构化编程的思想,把相关数据打包在一起,逻辑更清晰。
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

