从c++罗马数字转换器输出
C++ Roman Numeral Converter output from?
所以这个程序应该输出到控制台"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;
行。
相关文章:
- 转换器练习:跳过 if 语句和 if 语句
- 使用转换器提升 Python 问题 - 静态链接
- 我写的 ASCII 到二进制转换器C++向后显示二进制,如何使其正确显示?
- 十进制到二进制转换器 c++
- 用于 boost::p roperty_tree 的通用枚举转换器
- 字符数组到十六进制字符串的转换 - 意外输出
- 为什么我的C++基转换器中出现分段错误?
- 将字节数组转换为对象C++ |C++中的 C# 位转换器
- 英语到摩尔斯电码转换器
- 将ctypes c_void_p强制转换为c输出参数
- 我用c++做了一个转换器,把英里转换成公里,把公里转换成英里,把赛尔修斯转换成法赫伦海特,把法赫伦海特转换成赛尔修斯
- 使用 Json 转换器反序列化 WCF 服务中的C++字符串
- 十进制,包括负到 32 位二进制转换器
- 标准::映射转换器模板
- 输入 1024 后十进制到二进制转换的输出错误?
- 使用 iconv 进行 UTF8 转换的输出不正确
- Base64 转换器的输出不正确
- 转换器输出问题
- 从c++罗马数字转换器输出
- 十进制到罗马数字转换器-输出不正确的输出没有明显的原因