十进制到罗马数字转换器-输出不正确的输出没有明显的原因
C++ Decimal to Roman Numeral converter - outputting incorrect output for no apparent reason
程序在接收到"1984"输入时,应该输出"MCMLXXXIV"。它正在输出"MDCCLXXIV"。"
代码似乎是正确的思想。我已经想过很多很多次了,但我不知道我做错了什么。也不知道如何让输入失败捕获工作…它被注释掉了…任何帮助都太好了!
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
string convert(int, string, string, string);
int main()
{
int userNum = 0;
const int MIN_NUM = 0;
const int MAX_NUM = 3999;
const int ARRAY_SIZE = 4;
string answers[ARRAY_SIZE] = { "", "", "", "", };
cout << "Enter a negative number to end the program.n";
cout << "Enter an arabic number between 1 and 3999: ";
cin >> userNum;
while (userNum <= MIN_NUM || userNum >= MAX_NUM)
{
cout << "nInvalid Value. Number must be between 1 and 3999: ";
cin >> userNum;
}
if (userNum < 0)
{
cout << "Program ending due to user request.n";
EXIT_SUCCESS;
}
/*{
cout << "enter an integer: ";
if (cin >> num)
cout << "num: " << num << endl; }
else
{
cin.clear();
cin.ignore(1000, 'n');
cout << "Bad data detected..." << endl;
system("pause");
system("cls");
}
}
return 0;
} */
int thous = userNum / 1000; // Extract digits and store as digit
cout << thous; // also printing them out
int hund = userNum % 1000 / 100;
cout << "hundreds:" << hund;
int tens = userNum % 100 / 10;
cout << "tens:" << tens;
int ones = userNum % 10 / 1;
cout << "Ones: " << ones << endl;
/* Attempting to make four calls to a function named convert
with returns a string and accepts arguments of the digit
from above extraction and the low, mid, and high,
numeral for that place (i.e. ones, tens, hundreds,
attempting to store as a string. */
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 << answers[0] << endl << answers[1] << endl << answers[2];
cout << endl << answers[3] << endl;
system("PAUSE");
return 0;
}
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 "";
}
}
当用户输入一个负数时,整个程序也应该结束。我在想,最好把整个事情在一个do - while循环,做int main,而userNum> 0??这能行吗?
任何帮助都非常感谢!谢谢各位。
表示7和8的if
s后面的分号将阻止8或9的正确编码。
这就是编译器警告的作用。如果我用-Wall -Wextra
编译,我立即得到:
main.cpp:113:24: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
if (digit == 7);
^
main.cpp:117:24: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
if (digit == 8);
^
这就是你的问题所在。整个函数可以更好地写成switch
语句,如果你想这么做的话。虽然有更好的方法来做罗马数字转换。
我也得到一个警告:
main.cpp:32:25: warning: statement has no effect [-Wunused-value]
EXIT_SUCCESS;
^
,因为你可能是指return EXIT_SUCCESS;
相关文章:
- Windows 10命令提示符的输出不正确
- C++ - 为什么 unicode 输出不正确?
- 我的叉子输出不正确
- C++使用 CEIL 和长度输出不正确的值
- 使用 iconv 进行 UTF8 转换的输出不正确
- C strftime()输出不正确
- 当行数和列不匹配时,输出不正确
- C++ 嵌套循环输出不正确
- 矩阵和矢量乘法,输出不正确的产品
- 我对这个问题有问题,我的输出不正确
- 上一个较大的数字代码中的输出不正确
- 二进制搜索树的输出不正确
- 拆解输出不正确(BeaEngine)
- 最大输出不正确
- 基本合并排序中的输出不正确
- 更改声明顺序时输出不正确
- Base64 转换器的输出不正确
- 使用 find_first_of() 和 substr() 分隔字符串,但我得到的输出不正确
- 对字符串进行索引时输出不正确
- .wav C++数据输出不正确