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

C++作业问题:无法将字符串传递至romanToPositiveInteger函数

Hey there! Let's break down the issues in your code step by step, starting with the main problem you're facing: getting the Roman string into your conversion function.

Core Issue: Not Using the Class Member romanNum

In your romanToPositiveInteger() function, you declared a local variable string romanString; but never assigned any value to it—it stays empty this whole time! Meanwhile, you already stored the input string from main.cpp into the class's private member romanNum via the setRoman() method.

The fix here is simple:

  • Delete the local string romanString; line
  • Replace every instance of romanString in the conversion function with romanNum
Other Critical Fixes to Get Your Code Working Right

1. Broken Constructor Initialization

Your default constructor has type mismatches that will cause unexpected behavior:

romanType::romanType() {
 romanNum ='I'; // romanNum is a string, use double quotes: "I"
 num='1';       // num is an int—'1' is a char (ASCII value 49), use 1 instead
}

Fix it to:

romanType::romanType() {
 romanNum = "I";
 num = 1;
}

2. Flawed Roman Numeral Conversion Logic

Your current approach using find() has major issues:

  • It doesn't account for order (e.g., "IX" would trigger both "X" and "IX" checks, leading to wrong sums)
  • find() only checks if a substring exists, not its position or context

A much more reliable way is to iterate through the string left to right, comparing each character's value to the next one:

// Add this helper function in roman.cpp (before romanToPositiveInteger)
int romanCharToInt(char c) {
    switch(c) {
        case 'I': return 1;
        case 'V': return 5;
        case 'X': return 10;
        case 'L': return 50;
        case 'C': return 100;
        case 'D': return 500;
        case 'M': return 1000;
        default: return 0;
    }
}

void romanType::romanToPositiveInteger() {
    num = 0;
    int strLength = romanNum.length();
    
    for (int i = 0; i < strLength; ++i) {
        int currentVal = romanCharToInt(romanNum[i]);
        // Subtract current value if next character is larger (e.g., IV = 4, IX =9)
        if (i < strLength - 1 && currentVal < romanCharToInt(romanNum[i+1])) {
            num -= currentVal;
        } else {
            num += currentVal;
        }
    }
}

3. Remove Debug Outputs

You have cout << romanString; and cout << numResult; inside romanToPositiveInteger()—these are redundant since you already have printPositiveInteger() to display the final result. Delete them to avoid messy duplicate outputs.

Final Notes

After making these changes, your main.cpp will correctly pass romanString2 to setRoman(), which stores it in romanNum. The romanToPositiveInteger() function will then use that stored string to calculate the correct integer value, and printPositiveInteger() will output it as expected.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:49:08