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

C++作业:如何针对字符串类用户输入使用switch语句?

问题描述

我知道C里switch语句只能配合int类型使用,但作业要求针对字符串类型的用户输入用switch。我听说过stoi函数,但还没学过,不确定能不能用。作为C新手,我现在的代码还没写完,想请教可行的实现方法。

当前代码
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    // Declare Constant variables
    const float DISC_GOLF_RETAIL = 14.96;
    const float ULTIMATE_RETAIL = 20.96;
    const double DISCOUNT1 = 8;
    const float DISCOUNT2 = .16;
    const float DISCOUNT3 = .24;
    const float DISCOUNT4 = .32;
    const double DEC = 100;

    // Declare variables
    double quantity;
    double pricePerDisc;
    double totalSavings;
    double total;
    char userInput;
    float discount = 0; // 初始化默认折扣为0
    string discType;
    string disc1 = "Ultimate Disc";
    string disc2 = "Disc-Golf Disc";


    // Display title
    cout << "Welcome to the Flying-Disc Shop!" << "\n" << endl;

    // Prompt the user for input
    cout << "Enter 'u' for ultimate discs and 'g' for disc golf: ";
    cin >> (userInput);
    cout << endl;

    switch (discType)
    {
        case 1: userInput == 'u' || 'U';
            discType = disc1;
            pricePerDisc = ULTIMATE_RETAIL;
            break;

        case 2: userInput == 'g' || 'G';
            discType = disc2;
            pricePerDisc = DISC_GOLF_RETAIL;
            break;

        default:
            cout << "Invalid disc type." << endl;
            return 0;
    }

    cout << "Enter the number of Ultimate Disc(s): ";
    cin >> (quantity);
    cout << endl;

    cout << "------------Receipt------------" << endl;

    if (quantity > 5 && quantity <= 9)
    {
        discount = DISCOUNT1 / DEC;
        totalSavings = (pricePerDisc * quantity) * discount;
    }
    else if (quantity > 10 && quantity <= 19)
    {
        discount = DISCOUNT2;
        totalSavings = (pricePerDisc * quantity) * discount;
    }
    else if (quantity > 20 && quantity <= 29)
    {
        discount = DISCOUNT3;
        totalSavings = (pricePerDisc * quantity) * discount;
    }
    else if (quantity >= 30)
    {
        discount = DISCOUNT4;
    }

    totalSavings = (pricePerDisc * quantity) * discount;
    total = quantity * pricePerDisc - totalSavings;
    cout << "Disc Type: " << discType << endl;
    cout << "Quantity: " << quantity << endl;
    cout << "Price per Disc: " << "$ " << fixed << setprecision(2)
    << pricePerDisc << endl;
    cout << "Total Savings: " << "$ " << "-" << fixed << setprecision(2)
    << totalSavings << endl;
    cout << "Total: " << "$ " << fixed << setprecision(2) << total << endl;
}
可行实现方案

方案1:基于字符输入适配switch(最适合当前代码)

你当前代码里用的是char类型输入,完全可以直接对userInput使用switch,这是最贴合你现有代码且无需新知识点的方案:

// 替换原有的switch代码块
switch (userInput)
{
    case 'u':
    case 'U': // 同时匹配大小写
        discType = disc1;
        pricePerDisc = ULTIMATE_RETAIL;
        break;

    case 'g':
    case 'G':
        discType = disc2;
        pricePerDisc = DISC_GOLF_RETAIL;
        break;

    default:
        cout << "Invalid disc type." << endl;
        return 0;
}

方案2:直接对字符串使用switch(C++11及以上支持)

如果作业明确要求用户输入完整字符串(比如"Ultimate Disc"),C++11及之后的标准支持直接对std::string使用switch:

// 修改输入逻辑,读取完整字符串
string userInputStr;
cout << "Enter disc type (Ultimate Disc/Disc-Golf Disc): ";
cin.ignore(); // 忽略之前输入残留的换行符
getline(cin, userInputStr);

// 字符串类型的switch
switch (userInputStr)
{
    case "Ultimate Disc":
        discType = disc1;
        pricePerDisc = ULTIMATE_RETAIL;
        break;
    case "Disc-Golf Disc":
        discType = disc2;
        pricePerDisc = DISC_GOLF_RETAIL;
        break;
    default:
        cout << "Invalid disc type." << endl;
        return 0;
}

注:大部分学校的编译器都支持C++11,无需担心兼容性。

方案3:手动映射字符串到整数(无新函数)

如果担心用C++11特性不符合要求,可以手动把字符串输入映射为整数,再用switch:

string userInputStr;
cout << "Enter disc type (u/g): ";
cin >> userInputStr;

int discCode = 0;
if (userInputStr == "u" || userInputStr == "U") {
    discCode = 1;
} else if (userInputStr == "g" || userInputStr == "G") {
    discCode = 2;
}

switch (discCode)
{
    case 1:
        discType = disc1;
        pricePerDisc = ULTIMATE_RETAIL;
        break;
    case 2:
        discType = disc2;
        pricePerDisc = DISC_GOLF_RETAIL;
        break;
    default:
        cout << "Invalid disc type." << endl;
        return 0;
}
代码额外修正点
  • 输入数量的提示文字写死为"Ultimate Disc(s)",应改为动态匹配选择的碟片类型:cout << "Enter the number of " << discType << "(s): ";
  • 初始化discount为0,避免quantity≤5时未赋值导致的计算错误
  • 输出pricePerDisc的变量名写错了(原代码是Pricer),已修正
  • 调整total的输出顺序,保证fixed << setprecision(2)生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 19:15:53