H. Earley
H. Earley

Reputation: 15

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

I am relatively new to C++ and have some experience in Java with classes and functions but now very much, so this program is giving me some issues. Below is the code I have, everything seems right now to me and even though I have set "num" to 0, it always prints out "-858993460".

Here are my header files:

#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;
};

Here is my implementation file:

#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;
}

And here is my main file:

#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;
}

As I said previously, I am currently held up on the output part, but since I am new and this code is most likely horrid, I am accepting any critique on it. I will be a pretty busy today with work and wont be able to implement any suggestions until the next day, but I will get back to anyone that has a solution as soon as I am able to! Thanks in advance for any help :)

Upvotes: 1

Views: 190

Answers (1)

John Perry
John Perry

Reputation: 2678

You need to call roman.romanToPositiveInteger() at some point between roman.setRoman(romanString); and roman.printPositiveInteger();

Upvotes: 3

Related Questions