Tech Support Guy banner
Status
Not open for further replies.
1 - 8 of 8 Posts

·
Registered
Joined
·
41 Posts
Discussion Starter · #1 ·
I am trying to convert decimal to Roman numeral can anyone tell me what I am doing wrong because its not compiling.

#include <iostream>
using namespace std;

class decimalType{
public:
void decimal();
int convert();
void print();
void get();

private:
int 1 = I;
int 2 = II;
int 3 = III;
int 4 = IV;
int 5 = V;
char decimalNumber;
};

void decimalType::decimal(){
1 = I;
2 = II;
3 = III;
4 = IV;
5 = V;

}

int decimalType::convert(){
if (decimalNumber = 1){
cout << I;
}else if(decimalNumber = 2){
cout << II;
}else if(decimalNumber = 3){
cout << III;
}else if(decimalNumber = 4){
cout << IV;
}else if(decimalNumber = 5){
cout << V;

}

return decimalNumber;
}

void decimalType::print(){
cout << decimalNumber << endl;
}

void decimalType::get(){

}

int main(){
char decimalNumber;
cout << "Welcome to the Roman numeral to decimal converter!\nPlease enter a number in Roman numerals to be converted: ";
cin >> decimalNumber;

//print();
return 0;
}
 

·
Registered
Joined
·
106 Posts
Well... without writing it all your going to have to accept a string or array of char's...

string numeral;
cin << numeral;

Then you're going to have to parse it. This part I'm a little scratchy on, but it should be something like this....

for (int i = 0;char numeral.c_str != "\n";i++)
{
if (numeral.c_str = "I")
total = total +1; //This can be simplified into total++;
if (numeral.c_str = "V")
total += 5;
//etc
}
 

·
Registered
Joined
·
41 Posts
Discussion Starter · #5 ·
string numeral;
cin << numeral;

I think I got something mixed up there, but its almost there

#include <iostream>
using namespace std;
using std::cin;

string numeral;
cin << numeral;

for (int i = 0;char numeral.c_str != "\n";i++)
{
if (numeral.c_str = "I")
total = total +1;
if (numeral.c_str = "II")
total = total +2;
if (numeral.c_str = "III")
total = total +3;
if (numeral.c_str = "IV")
total = total +4;
if (numeral.c_str = "V")
total += 5;

}

int main(){
char decimalNumber;
cout << "Welcome to the Roman numeral to decimal converter!\nPlease enter a number in Roman numerals to be converted: ";
cin >> numeral;

//print();
return 0;
}
 

·
Registered
Joined
·
106 Posts
Yes, you do. What's going on is that you're only checking for ONE character with the numeral.c_str. So you it's I, not II. Might have to also check for special cases.

Also, I made a mistake, it's if (numeral.c _str == "I"). two ='s, not one.

My suggestion is go through a tutorial or something. I personally made this harder than it should be. You should be able to use strings for it.

if (numeral == "I")
total = 1;
if (numeral == "II")
total = 2;
if (numeral == "III")
etc.

Also, give us compiler errors when you post.

Also, what are the requirements would be nice. If you need to be able to post something like IX (9), etc.
 

·
Registered
Joined
·
551 Posts
The above code would only work for single Roman Numerals - if you want it to be flexible, you need to search the string for each character, then determine where they are (e.g. if it finds a I before a X it knows that the number is 9, not just searching for "IX".)
 

·
Registered
Joined
·
112 Posts
Don't forget to put your code in a function and actually call it from main . . .

How I would tackle this problem is to convert all of your roman numerals to decimals one-by-one from left to right as you read your input string. Add each of them to a running sum.

After you get that working, if you need to handle cases such as IX=9, you're going to have to keep up with the runningTotal, the previously read numeral, and the currently read numeral. If the currently read numeral is larger than the previously read one, you need to subtract the previous one and add the current one to the sum. If the current one is smaller or equal to the previous one, just add the current one.
 
1 - 8 of 8 Posts
Status
Not open for further replies.
Top