URGENT DUE TOMORROW C++ Roman Num to Int Program

Dec 20, 2013 at 2:11am
okay so basically i am really close (i think) and the numbers im getting when i run it are weird like for instance i should get back a one if i enter in I or i but instead im getting like 178976087. please help me its due tomorrow and its my final

MAIN:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdafx.h"
#include "iostream"
#include <iomanip>
#include "romanType.h"

using namespace std;
int main()
{
	romanType r;

    char romanNumber;
	int  romanTotal;
  

    cout << "Hello! Please enter your Roman Numeral: " << endl; 
    cin >> romanNumber; 
    cout << endl;

    r.convertedRomanNum(romanTotal);
    r.printRoman(romanNumber);
	cout << endl;
	cout << endl;
	r.printInt(romanTotal);
	cout << endl;
    system ("pause");
	system ("CLS");
    return 0;

}




CLASS FILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#ifndef ROMANTYPE_h_
#define ROMANTYPE_h_

#include <iostream>

using namespace std;

class romanType
{
public:
	void printRoman(char romanNumber);
	void setRomanNum(char &romanNumeral);
	int printInt(int &romanTotal);
	int convertedRomanNum(int &romanTotal);

	romanType();
	romanType(char);

private:
	char romanNumber[14];
	int decimalNum;
	int totalCount;
};
#endif 


IMPLEMENTATION FILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "romanType.h"
#include <iostream>

using namespace std;


romanType::romanType(void)
{

};
void romanType::printRoman(char romanNumber)
{
	cout << "This is the number in Roman Numeral form: " << romanNumber << endl;
};
int romanType::printInt(int &romanTotal)
{
	cout << "This is the number in decimal form: " << romanTotal << endl;
	return romanTotal;
};
void romanType::setRomanNum(char &romanNumber)
{

};
int romanType::convertedRomanNum(int &romanTotal)
{
	int length = 0;
	length = strlen(romanNumber);
	int count[1];

		for(int i = 0; i < length; i++)
		{
			switch(romanNumber[i])
			{
				 case 'M':
                      count[i] = 1000;
                      break;
                 case 'm':
                      count[i] = 1000;
                      break;
                 case 'D':
                      count[i] = 500;
                      break;
                 case 'd':
                      count[i] = 500;
                      break;
                 case 'C':
                      count[i] = 100;
                      break;
                 case 'c':
                      count[i] = 100;
                      break;
                 case 'L':
                      count[i] = 50;
                      break;
                 case 'l':
                      count[i] = 50;
                      break;
                 case 'X':
                      count[i] = 10;
                      break;
                 case 'x':
                      count[i] = 10;
                      break;
                 case 'V':
                      count[i] = 5;
                      break;
                 case 'v':
                      count[i] = 5;
                      break;
                 case 'I':
                      count[i] = 1;
                      break;
                 case 'i':
                      count[i] = 1;
                      break;
               
           }   
           romanTotal = romanTotal + count[0];
		}
		 return romanTotal;
};
Dec 20, 2013 at 2:45am
on line 19. shouldn't you be passing romanNumber instead of romanTotal?
Dec 20, 2013 at 2:50am
Your input and conversion is a little confused. As there is no correlation between your input and output, and you are not initializing the char[] array in your class, you are getting a random number for output. (You are lucky it isn't crashing.)

A char is not a string -- a char holds a single letter, where a string can hold many.


The user will input a string, like:

    IV
    MMXIII
    XIX

I presume your task is to take a string like that and convert it to an integer.

Or to take an integer and convert it into a string like that.

Hence, your main function should look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    romanType r;
    string input;

    cout << "Please enter a Roman Numeral to convert to integer: ";
    getline( cin, input );

    r.setRomanNum( input );

    if (!r.convertRomanNumToInt())
    {
        cout << "That was not a valid Roman Numeral.\n";
        return 1;
    }

    cout << "The decimal equivalent is " << r.getInt() << ".\n";
    return 0;
}

Were you given those methods to use, or did you create them yourself? I've modified them some... just to make life easier...

In the a switch statement, you can combine labels for the same things...

1
2
3
4
5
6
7
8
switch (foo)
{
    case 'M':
    case 'm':
        // stuff for M/m goes here
        break;
    ...
}

You can safely make assumptions about the ordering, because the LARGEST values will always be listed FIRST, except for a couple of weird subtractive things, explained nicely here at Wikipedia:
http://en.wikipedia.org/wiki/Roman_numerals#Reading_Roman_numerals

Good luck! (You're going to be up all night.)
Dec 20, 2013 at 3:35am
i tried changing the main but it doesnt give the error message when i put in something thats not a Roman numeral also im not sure what you mean by im not initializing the array i thought i did that by putting

 
char romanNumber[14];
Dec 20, 2013 at 10:56am
char romanNumber[14];

creates the array, but doesn't initialise it. You have no idea what the initial values of that array are.
Dec 20, 2013 at 6:05pm
Don't know if it is too late... how did you do?
Topic archived. No new replies allowed.