从c++罗马数字转换器输出

C++ Roman Numeral Converter output from?

本文关键字:转换器 输出 数字 罗马 c++      更新时间:2023-10-16

所以这个程序应该输出到控制台"Enter a negative number to end "。在1和3999之间输入一个阿拉伯数字:"然后你给它你的数字,它会分解并转换它。我得到的输出是这样的1984我完全不知道我代码的哪一部分正在被执行。

代码如下所示

 #include <iostream>
 #include <string>
 #include <iomanip>
 using namespace std;
string convert(int digit, string low, string mid, string high); 

 int main()
{
    const int MAX_INPUT = 3999, MIN_INPUT = 0,      
    ARRAY_SIZE = 4;         
    string answers[ARRAY_SIZE] = { "", "", "", "" };        
    int accumulator = 0;                            
    int userNum = 0;                        
do {    
    cout << "Enter a negative number to end the program.n";
    cout << "Enter an arabic number between 1 and 3999: ";
    while (!(cin >> userNum) || (userNum < MIN_INPUT || userNum > MAX_INPUT)){              //input validation
        if (userNum < 0)
        {
            cout << "Exiting program:";
            return 0;
        }
        else {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), 'n');                    
            cout << "nInvalid Value. Number must be between 1 and 3999:  ";
        }
    }
    // Digit Extraction - turns userNum into four seperate values
    int thous = userNum / 1000;                                 //thousands place value
    int hund = userNum % 1000 / 100;                            //hundreds place value
    int tens = userNum % 100 / 10;                              //tens place value
    int ones = userNum % 10 / 1;                                //ones place    value

    // filling answers array with results from convert function. 
    answers[0] = convert(thous, "M", "M", "M");
    answers[1] = convert(hund, "C", "D", "M");
    answers[2] = convert(tens, "X", "L", "C");
    answers[3] = convert(ones, "I", "V", "X");
    cout << "Roman numeral for " << userNum << " is: ";
    cout << answers[0]  << answers[1] <<  answers[2];
    cout <<  answers[3] << endl;


    } while (userNum > 0);
    system("PAUSE");
 return 0;
 }

 // convert function
 string convert(int digit, string low, string mid, string high)
{
cout << digit << endl;
if (digit == 1)
{
    return low;
}
if (digit == 2)
{
    return low + low;
}
if (digit == 3)
{
    return low + low + low;
}
if (digit == 4)
{
    return low + mid;
}
if (digit == 5)
{
    return mid;
}
if (digit == 6)
{
    return mid + low;
}
if (digit == 7)
{
    return mid + low + low;
}
if (digit == 8)
{
    return mid + low + low + low;
}
if (digit == 9)
{
    return low + high;
}
if (digit == 0)
{
    return "";
}
}

您想从函数convert()中删除cout << digit << endl;行。