You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CS50编程新手求教:如何在C语言中检查get_string输入的特定字符串?

字符串判断与文本商店逻辑实现

用户问题

我目前处于CS50课程第二周,是纯编程新手,想知道能否像判断整数那样检查特定字符串?我正在制作文本版物品商店练习语法,却不知如何获取用户选品输入,并跳转至对应的确认/拒绝逻辑及物品描述环节。因毫无头绪暂无可展示的尝试内容,以下是当前代码供参考:

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

解决方案

C语言里不能直接用==比较字符串,得用strcmp函数(CS50库已兼容该函数,需引入string.h头文件)来判断字符串是否相等。下面是补全后的完整商店逻辑代码,包含输入匹配、物品描述、余额检查和购买流程:

#include <cs50.h>
#include <stdio.h>
#include <string.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);
    
    // 匹配用户选择的物品
    if (strcmp(select, "Matchbox") == 0)
    {
        printf("\nMatchbox: A sturdy little box, perfect for keeping small trinkets safe.\n");
        if (buckaroonies >= 50)
        {
            buckaroonies -= 50;
            printf("You bought the Matchbox! You now have %i buckaroonies left.\n", buckaroonies);
        }
        else
        {
            printf("Sorry, you don't have enough coin for that!\n");
        }
    }
    else if (strcmp(select, "Wool Hat") == 0)
    {
        printf("\nWool Hat: Warm, thick, and stylish—keeps your head toasty in the cold.\n");
        if (buckaroonies >= 125)
        {
            buckaroonies -= 125;
            printf("You bought the Wool Hat! You now have %i buckaroonies left.\n", buckaroonies);
        }
        else
        {
            printf("Sorry, you don't have enough coin for that!\n");
        }
    }
    else if (strcmp(select, "Heavy Coat") == 0)
    {
        printf("\nHeavy Coat: Built for harsh winters—blocks wind and snow like a charm.\n");
        if (buckaroonies >= 250)
        {
            buckaroonies -= 250;
            printf("You bought the Heavy Coat! You now have %i buckaroonies left.\n", buckaroonies);
        }
        else
        {
            printf("Sorry, you don't have enough coin for that!\n");
        }
    }
    else if (strcmp(select, "Canned Food") == 0)
    {
        printf("\nCanned Food: Non-perishable, filling—great for long journeys.\n");
        if (buckaroonies >= 25)
        {
            buckaroonies -= 25;
            printf("You bought the Canned Food! You now have %i buckaroonies left.\n", buckaroonies);
        }
        else
        {
            printf("Sorry, you don't have enough coin for that!\n");
        }
    }
    else
    {
        printf("Hmm, we don't sell that here. Did you spell it right?\n");
    }
    
    return 0;
}

核心知识点说明

  • 字符串判断: strcmp(a, b)函数返回0时,表示两个字符串完全一致,这就是C语言里检查特定字符串的标准方式,逻辑和整数用==判断类似,但需要用专门的字符串比较函数。
  • 分支逻辑: 用if-else if链逐个匹配用户输入,每个分支先展示物品描述,再检查余额是否足够,执行购买或拒绝操作。
  • 状态更新: 购买成功后直接扣减对应金额,实时更新并显示剩余余额。

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 17:56:22