1

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 :)

H. Earley
  • 15
  • 4
  • 3
    Looks like an uninitialized variable on the stack. 0xcc is a special value in microsoft debug builds. https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations – drescherjm May 17 '18 at 17:36
  • In the output of the main program it can recognize the input for the romanString string, and I had set num to 0 in the beginning of the function, so where else could there be an issue? I thought about that as well but wasn't sure how to fix it or where the issue could be – H. Earley May 17 '18 at 17:39
  • I will go back now and make sure that everything is initialized just to see if it fixes it, I will report back if it does! – H. Earley May 17 '18 at 17:40
  • @H.Earley You could take a look at the answer section, too. ;-) – John Perry May 17 '18 at 17:41
  • Your debugger should help you figure that out. That is if you properly use it. Meaning single step through your algorithm looking at the variables at each step to make sure the values match your expectations. – drescherjm May 17 '18 at 17:41
  • Since I can't edit my first comment anymore -858993460 = 0xcccccccc – drescherjm May 17 '18 at 17:42
  • Problem solved! Thanks for the help everyone, I appreciate all of it. Also to drescherjm, unfortunately we have not covered how to use the debugger in class very well so I am still learning how to use it. I think we only spent an hour talking about it the whole semester so far. – H. Earley May 17 '18 at 17:47
  • 1
    I advise you to learn to use the debugger on your own. The debugger is an essential tool. You will save many hours by learning proper debugging. I still debug almost daily and I have programmed since the early 1980s / written more than a million lines of c++ code professionally. – drescherjm May 17 '18 at 17:49
  • BTW you should initialize num in `romanType::romanType()` perhaps setting it to `-1` and complaining in printPositiveInteger() if the value is less than 0. – drescherjm May 17 '18 at 17:50
  • drescherjm, I will definitely keep that in mind and try to practice it more often! Until this point I haven't had many issues, so not many chances to use the debugger. I will try to become more familiar with it though, since it is so valuable. Thanks for all the advice and help, it is greatly appreciated! I have been really enjoying C++ so far so I want to learn much more about it. – H. Earley May 17 '18 at 17:57
  • This should help with debugging: https://tutorials.visualstudio.com/vs-get-started/debugging – drescherjm May 17 '18 at 18:20
  • @drescherjm I will check this out tomorrow morning, thank you!! – H. Earley May 17 '18 at 18:47
  • If you have a different IDE than Visual Studio 2017 the buttons may be different but the concept is the same. – drescherjm May 17 '18 at 18:52
  • -858993460 = 0xCCCCCCCC which means [you've accessed uninitialized memory](https://stackoverflow.com/q/370195/995714) – phuclv Aug 18 '18 at 11:08

1 Answers1

3

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

John Perry
  • 2,497
  • 2
  • 19
  • 28
  • 1
    Well, now I just feel like the biggest idiot alive for missing something so obvious and so simple... I guess this is a lesson learned the hard way by making myself look stupid online? Either way, thank you so much! Greatly appreciated :) – H. Earley May 17 '18 at 17:46
  • 1
    Don't feel bad. Programming is a pursuit in which an unintelligent machine demonstrates conclusively to intelligent people just what fools they are. I doubt there's anyone giving answers on this website who hasn't made a dumb mistake at least once, and possibly asked about it online, too. Then there are people like me who sometimes give dumb answers... :-) – John Perry May 17 '18 at 17:59
  • BTW, @drescherjm 's advice about learning to use a debugger is solid. I'm not sure it would have helped here, but gdb is one of the greatest inventions ever. – John Perry May 17 '18 at 18:01
  • That is the greatest explanation of programming I have ever heard, and I will always remember that now! It is so true though, and I'm cracking up in class thinking about it. I'm eager to keep the persuit alive and hopefully will never forget this lesson. Thanks again! :) – H. Earley May 17 '18 at 18:02
  • The key here is single stepping (instead of just running) through the debugger looking at the values of variables in the ide at each step of the algorithm or program in this case. That would help you determine that `num` was garbage from the start and never changed from that.. – drescherjm May 17 '18 at 18:15