C++ 中的罗马数字输出总是“-858993460”,不知道为什么?

Roman Numeral Output in C++ is always "-858993460", not sure why?

我是 C++ 的新手,在 Java 和 类 和函数方面有一些经验,但现在非常多,所以这个程序给我带来了一些问题。下面是我的代码,现在对我来说一切都是正确的,即使我将 "num" 设置为 0,它总是打印出“-858993460”。

这是我的头文件:

#include <string> 
using namespace std;

class romanType
{
public:
void setRoman(string n);
void romanToPositiveInteger();
void printPositiveInteger() const;
romanType();
romanType(string n);
void printNum();

private:
string romanString;
int num;
};

这是我的实现文件:

#include "stdafx.h"
#include <iostream>
#include <string>
#include "romanType.h"

using namespace std;

int value(char num) {
if (num == 'I')
    return 1;
if (num == 'V')
    return 5;
if (num == 'X')
    return 10;
if (num == 'L')
    return 50;
if (num == 'C')
    return 100;
if (num == 'D')
    return 500;
if (num == 'M')
    return 1000;

return -1;
}

void romanType::setRoman(string n) {
romanString = n;
}

void romanType::romanToPositiveInteger() {

num = 0;

for (int i = 0; i < romanString.length(); i++)
{
    // Getting value of symbol s[i]
    int s1 = value(romanString[i]);

    if (i + 1 < romanString.length())
    {
        // Getting value of symbol s[i+1]
        int s2 = value(romanString[i + 1]);

        // Comparing both values
        if (s1 >= s2)
        {
            // Value of current symbol is greater
            // or equal to the next symbol
            num = num + s1;
        }
        else
        {
            num = num + s2 - s1;
            i++; // Value of current symbol is
                 // less than the next symbol
        }
    }
    else
    {
        num = num + s1;
        i++;
    }
}
}

void romanType::printPositiveInteger() const {
cout << num << endl;
}

romanType::romanType(string n) {
romanString = n;
}

romanType::romanType() {

}

void romanType::printNum() {
cout << num << endl;
}

这是我的主文件:

#include "stdafx.h"
//Main program

#include <iostream>
#include <string>
#include "romanType.h" 

using namespace std;

int main()
{

romanType roman;

string romanString;

while (romanString != "EXIT") {
    cout << "Enter a roman number: ";
    cin >> romanString;

    roman.printNum();

    roman.setRoman(romanString);

    cout << "The equivalent of the Roman numeral "
        << romanString << " is ";
    roman.printPositiveInteger();
    cout << endl;
    cout << endl;
}

//Pause the program
std::cout << "\n\n---------------------------------\n";
system("pause");

//Exit the program
return EXIT_SUCCESS;
}

正如我之前所说,我目前在输出部分受阻,但由于我是新手并且这段代码很可能很糟糕,所以我接受对它的任何批评。我今天的工作会很忙,直到第二天才能实施任何建议,但我会尽快回复任何有解决方案的人!在此先感谢您的帮助:)

您需要在 roman.setRoman(romanString);roman.printPositiveInteger();

之间的某个时间调用 roman.romanToPositiveInteger()