C++ 一些错误-尝试将罗马条目转换为十进制
现在我要改变我的问题-我可以用一些指导说明为什么我的程序中有三个编译器错误,不太确定我做错了什么/我缺少了什么-我在代码中添加了注释只是为了说明它们在哪里。谢谢C++ 一些错误-尝试将罗马条目转换为十进制,c++,C++,现在我要改变我的问题-我可以用一些指导说明为什么我的程序中有三个编译器错误,不太确定我做错了什么/我缺少了什么-我在代码中添加了注释只是为了说明它们在哪里。谢谢 #include <iostream> #include <string> using namespace std; class romanType { public: void setRomanNum(string store); // this function will
#include <iostream>
#include <string>
using namespace std;
class romanType
{
public:
void setRomanNum(string store);
// this function will store the Roman numeral
int convertNum(char rNum);
// this function will convert the Roman numeral to a decimal
void decimalPrint(int total);
// this function will print the decimal number
void romanPrint(char rNum);
// this function will print the Roman numeral
int getNum(char letter);
// this function will get the number input
romanType(int store);
//Constructor with parameter
romanType();
char roman[7];
string num;
int length = 0;
string dNum;
int equals;
};
romanType::romanType(int store)
{
dNum = 1;
}
void romanType::setRomanNum (string store)
{
dNum = store;
}
void romanType::romanPrint(char rNum)
{
cout << "The Roman numeral is: " << roman << endl;
}
void romanType::decimalPrint(int total)
{
cout << "The Decimal number is: " << equals << endl;
}
int romanType::convertNum (char rNum)
{
int letter;
int totalNum = 0;
for (int i = 0; i< dNum.length(); i++)
// "loop will run at most once (loop increment never executed)"?
{
switch (roman[i])
{
case 'M':
totalNum+= 1000;
break;
case 'D':
totalNum += 500;
break;
case 'C':
totalNum += 100;
break;
case 'L':
totalNum += 50;
break;
case 'X':
totalNum += 10;
break;
case 'V':
totalNum += 5;
break;
case 'I':
totalNum += 1;
break;
}
totalNum = totalNum + letter;
equals = totalNum;
return equals;
}
};
// "control may reach end of non-void function"
int main()
{
romanType output;
int rNumeral;
char entry;
cout << "Please enter a Roman numeral (Capitalized only): " << endl;
cin >> rNumeral;
cout << "Print Decimal or Roman Numeral? Type 1 for Decimal, 2 for Roman Numeral: " << endl;
cin >> entry;
if (entry == '1')
{
cout << "You chose to view the decimal conversion." << endl;
output.decimalPrint(rNum);
// How do I output the decimal conversion from the void romanType::decimalPrint(int total) function?
}
else if (entry == '2')
{
cout << "You chose to view the Roman numeral." << endl;
output.romanPrint(rNumeral);
}
else
cout << "Error: bad input" << endl;
return 0;
exit(1);
}
#包括
#包括
使用名称空间std;
类罗马类型
{
公众:
void setromanum(字符串存储);
//此函数将存储罗马数字
int convertNum(字符数);
//此函数将罗马数字转换为十进制
无效分号打印(整数合计);
//此函数将打印十进制数
无效打印(字符);
//此函数将打印罗马数字
int getNum(字符字母);
//此函数将获取输入的数字
romanType(内部存储);
//带参数的构造函数
romanType();
char-roman[7];
字符串数;
整数长度=0;
字符串dNum;
int等于;
};
romanType::romanType(int存储)
{
dNum=1;
}
void romanType::setRomanNum(字符串存储)
{
dNum=存储;
}
void romanType::romanPrint(字符数)
{
cout算法中的错误在于罗马系统是非位置的。请参见en.wikipedia.org/wiki/Roman_numbers和en.wikipedia.org/wiki/Subtractive_notation。您不能只将结果数字(即字母)相加,还必须识别并说明发生减法时的情况
为什么会出现错误“roman类型的初始化没有匹配的构造函数”
因为唯一提供的构造函数不是默认构造函数,而是采用一个类型为int
的参数。由于提供了这样的构造函数,默认构造函数不是由编译器生成的。请定义romanType::romanType()
或将现有构造函数更改为romanType::romanType(int i=0)
(添加默认参数)。请参阅和
期望表达
在前面的else
块周围提供大括号。需要多个语句->大括号
if (entry == '1') {
cout << "You chose to view the decimal conversion." << endl;
output.decimalPrint(rNum);
} else if (entry == '2')
cout << "You chose to view the Roman numeral." << endl;
output.romanPrint(rNumeral);
}
您的开关逻辑错误,请尝试以下操作:
int totalNum = 0;
for (int i = 0; i< dNum.length(); i++)
// the loop ran once because you were returning values when catching a letter
{
switch (roman[i])
{
case 'M': // roman[i] is a char, the cases should try to catch chars
totalNum += 1000; // increment your global number
break;
case 'D':
totalNum += 500;
break;
...
}
return totalNum;
inttotalnum=0;
对于(int i=0;i
请每个问题问一个问题。stackoverflow.com不是教程,也不是调试服务。请陈述一个具体的、离散的、有说服力的问题,而不是要求每个人寻找随机散布在代码中的所有问题,就像寻找复活节彩蛋一样。首先修复编译器错误,学习如何阅读。运行完后代码通过调试器一步一步地跟踪逻辑错误。您不能期望我们在这里为您这样做。只是修复了一些编译器错误,我不确定如何修复它们-尽管仍然有一些错误。如果有人可以帮助我处理编译器错误,那将非常好。我在收到错误的地方添加了一些注释我不明白为什么。我可以在一个单独的问题中询问逻辑-只是认为这是不允许的。对不起,你不需要3个返回语句,你需要在switch语句中使用默认的大小写。查一查。你的主要错误是罗马系统是非位置的。你不能只把后面的罗马数字相加就得到正确的结果。请参阅en.wikipedia.org/wiki/Roman_numerics和en.wikipedia.org/wiki/Subtractive_notationahhh!!!默认构造函数谢谢。正是我所需要的。非常感谢:)
int totalNum = 0;
for (int i = 0; i< dNum.length(); i++)
// the loop ran once because you were returning values when catching a letter
{
switch (roman[i])
{
case 'M': // roman[i] is a char, the cases should try to catch chars
totalNum += 1000; // increment your global number
break;
case 'D':
totalNum += 500;
break;
...
}
return totalNum;