-6
所以這是我的代碼應該運行,但它沒有,因爲我得到的錯誤「未初始化的局部變量」rNumeral'使用了「在我的代碼在這裏,我相信是源於主。無法弄清楚爲什麼,我錯過了什麼?謝謝未初始化的局部變量使用錯誤
#include <iostream>
#include <string>
using namespace std;
class romanType
{
public:
int convertNum(char rNum);
void decimalPrint();
void romanPrint();
romanType(int store);
//Constructor with parameter
romanType();
char roman[7];
string num;
int length = 0;
string dNum;
int equals;
};
void romanType::romanPrint()
{
cout << "The Roman numeral is: " << roman << endl;
}
void romanType::decimalPrint()
{
cout << "The Decimal number is: " << equals << endl;
}
int romanType::convertNum(char rNum)
{
int subtractSum = 0;
int totalNum = 0;
for (int i = 0; i< dNum.length(); i++)
{
switch (roman[i])
{
case 'M':
totalNum += 1000;
if (subtractSum < 1000)
totalNum -= subtractSum * 2;
case 'm':
totalNum += 1000;
if (subtractSum < 1000)
totalNum -= subtractSum * 2;
break;
case 'D':
totalNum += 500;
if (subtractSum < 500)
totalNum -= subtractSum * 2;
break;
case 'd':
totalNum += 500;
if (subtractSum < 500)
totalNum -= subtractSum * 2;
break;
case 'C':
totalNum += 100;
if (subtractSum < 100)
totalNum -= subtractSum * 2;
break;
case 'c':
totalNum += 100;
if (subtractSum < 100)
totalNum -= subtractSum * 2;
break;
case 'L':
totalNum += 50;
if (subtractSum < 50)
totalNum -= subtractSum * 2;
break;
case 'l':
totalNum += 50;
if (subtractSum < 50)
totalNum -= subtractSum * 2;
break;
case 'X':
totalNum += 10;
if (subtractSum < 10)
totalNum -= subtractSum * 2;
break;
case 'x':
totalNum += 10;
if (subtractSum < 10)
totalNum -= subtractSum * 2;
break;
case 'V':
totalNum += 5;
if (subtractSum < 5)
totalNum -= subtractSum * 2;
break;
case 'v':
totalNum += 5;
if (subtractSum < 5)
totalNum -= subtractSum * 2;
break;
case 'I':
totalNum += 1;
if (subtractSum < 1)
totalNum -= subtractSum * 2;
break;
case 'i':
totalNum += 1;
if (subtractSum < 1)
totalNum -= subtractSum * 2;
break;
default:
cout << "not valid!" << endl;
}
totalNum = totalNum + subtractSum;
equals = totalNum;
return equals;
}
};
int main()
{
romanType output;
int rNumeral;
char entry;
romanType roman = romanType(rNumeral);
// input for entry
cout << "Please enter a Roman numeral (Capitalized only): " << endl;
cin >> rNumeral;
// print options
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;
roman.decimalPrint();
}
else if (entry == '2')
{
cout << "You chose to view the Roman numeral." << endl;
roman.romanPrint();
}
else
cout << "Error: bad input" << endl;
return 0;
exit(1);
}
初始化'rNumeral'。並刪除這個或發佈[mcve],因爲幾乎所有的代碼都是不相關的。 – juanchopanza
相信你的編譯器/運行時:你顯然使用了未初始化的變量。它應該告訴你哪一個。使用未初始化的變量在C++中是未定義的行爲。 – Bathsheba
應該是警告,而不是錯誤。 – iksemyonov