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

请求协助调试:基于switch case的1-99数字转英文单词程序

Fixing Your C++ Number-to-English-Word Converter

Hey there! I see the root of your problem right away: using a single char variable to handle input means you can only capture one character at a time. That's why two-digit numbers (like 15 or 99) aren't being processed correctly—your program only reads the first digit and chokes on the rest. Let's fix this properly.

The Solution: Use Strings to Capture Input, Then Convert to Integers

Here's a complete, working version of your program. It handles 1-99 correctly, detects when the user presses just Enter to exit, and uses switch cases as you intended:

#include <iostream>
#include <string>
#include <cctype> // For isdigit()

using namespace std;

int main() {
    string input;
    int num;

    cout << "Enter a number between 1-99 (press Enter to exit): ";
    while (getline(cin, input)) {
        // Check if user pressed only Enter (empty input)
        if (input.empty()) {
            cout << "Exiting program...\n";
            break;
        }

        // Validate input is a number
        bool isNumber = true;
        for (char c : input) {
            if (!isdigit(c)) {
                isNumber = false;
                break;
            }
        }
        if (!isNumber) {
            cout << "Invalid input! Please enter a number between 1-99: ";
            continue;
        }

        // Convert string to integer
        num = stoi(input);

        // Check if number is in valid range
        if (num < 1 || num > 99) {
            cout << "Number out of range! Please enter 1-99: ";
            continue;
        }

        // Convert number to words using switch cases
        if (num >= 1 && num <= 19) {
            // Handle 1-19 (unique words)
            switch(num) {
                case 1: cout << "One\n"; break;
                case 2: cout << "Two\n"; break;
                case 3: cout << "Three\n"; break;
                case 4: cout << "Four\n"; break;
                case 5: cout << "Five\n"; break;
                case 6: cout << "Six\n"; break;
                case 7: cout << "Seven\n"; break;
                case 8: cout << "Eight\n"; break;
                case 9: cout << "Nine\n"; break;
                case 10: cout << "Ten\n"; break;
                case 11: cout << "Eleven\n"; break;
                case 12: cout << "Twelve\n"; break;
                case 13: cout << "Thirteen\n"; break;
                case 14: cout << "Fourteen\n"; break;
                case 15: cout << "Fifteen\n"; break;
                case 16: cout << "Sixteen\n"; break;
                case 17: cout << "Seventeen\n"; break;
                case 18: cout << "Eighteen\n"; break;
                case 19: cout << "Nineteen\n"; break;
            }
        } else {
            // Handle 20-99 (tens place + optional ones place)
            int tens = num / 10;
            int ones = num % 10;

            // Print tens word
            switch(tens) {
                case 2: cout << "Twenty"; break;
                case 3: cout << "Thirty"; break;
                case 4: cout << "Forty"; break;
                case 5: cout << "Fifty"; break;
                case 6: cout << "Sixty"; break;
                case 7: cout << "Seventy"; break;
                case 8: cout << "Eighty"; break;
                case 9: cout << "Ninety"; break;
            }

            // Print ones word if not zero
            if (ones != 0) {
                cout << "-";
                switch(ones) {
                    case 1: cout << "One"; break;
                    case 2: cout << "Two"; break;
                    case 3: cout << "Three"; break;
                    case 4: cout << "Four"; break;
                    case 5: cout << "Five"; break;
                    case 6: cout << "Six"; break;
                    case 7: cout << "Seven"; break;
                    case 8: cout << "Eight"; break;
                    case 9: cout << "Nine"; break;
                }
            }
            cout << "\n";
        }

        cout << "\nEnter another number (press Enter to exit): ";
    }

    return 0;
}

Key Fixes & Explanations:

  • Input Handling: We use getline(cin, input) to read the entire line of input. This lets us detect when the user presses just Enter (empty input string) to exit.
  • Validation: We check if the input is a valid number using isdigit(), so non-numeric entries don't crash the program.
  • Integer Conversion: The stoi() function converts the valid string input to an integer, which we can then split into tens and ones places for the switch cases.
  • Switch Case Logic: We split the problem into two parts: unique words for 1-19, and tens + optional ones for 20-99. This keeps the switch cases organized and easy to read.

Why Your Original Code Failed:

A single char variable can only hold one character. When you entered a two-digit number like "25", your program would read '2' into the char variable, leaving '5' stuck in the input buffer. This caused unexpected behavior in subsequent input reads and made it impossible to process two-digit numbers correctly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:48:03