C ++ Roman Numeral Converter输出?

时间:2015-09-22 21:44:20

标签: c++ roman-numerals

所以这个程序应该输出到控制台“输入一个负数来结束。输入1到3999之间的阿拉伯数字:”然后你给它你的数字,它将它分解并转换它。我得到的输出看起来像这样 1 9 8 4 我没有IDEA我的代码的哪一部分正在制定中。

代码看起来像这样

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

1 个答案:

答案 0 :(得分:2)

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