Trying to write a program that converts integers to roman numerals in C++

  • C/++/#
  • Thread starter Demon117
  • Start date
  • #1
165
1

Main Question or Discussion Point

Here is the code that I have come up with. What could be implimented to make it compile and run? It runs, but all that comes up is the request for the integer between 1 and 3999, after I enter the number the program simply ends.

CODE:

#include <iostream>
using namespace std;

string intToRoman(int num) {
string roman;
int th,h,t,o;
char *ones[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
char *tens[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
char *hundreds[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
char *thousands[] = {"","M","MM","MMM","MMMM","MMMMM"};

//if (num =< 4000) {
if (num <= 4000) {
th = num / 1000;
num = num % 1000; //Get rid of 1000s

h = num / 100;
num = num % 100; //Get rid of 100s

t = num / 10;
o = num % 10;
roman += thousands[th];
roman += hundreds[h];
roman += tens[t];
roman += ones[o];
}
else
roman = "Please enter a smaller number \n";
return roman;
}

int main()
{
int num;
cout<<"What number would you like to convert between 1 and 3999? ";
cin>>num;
string output = intToRoman(num);
system("pause");
return 0;
}
 

Answers and Replies

  • #2
33,503
5,191
Here is the code that I have come up with. What could be implimented to make it compile and run? It runs, but all that comes up is the request for the integer between 1 and 3999, after I enter the number the program simply ends.

CODE:

#include <iostream>
using namespace std;

string intToRoman(int num) {
string roman;
int th,h,t,o;
char *ones[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
char *tens[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
char *hundreds[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
char *thousands[] = {"","M","MM","MMM","MMMM","MMMMM"};

//if (num =< 4000) {
if (num <= 4000) {
th = num / 1000;
num = num % 1000; //Get rid of 1000s

h = num / 100;
num = num % 100; //Get rid of 100s

t = num / 10;
o = num % 10;
roman += thousands[th];
roman += hundreds[h];
roman += tens[t];
roman += ones[o];
}
else
roman = "Please enter a smaller number \n";
return roman;
}

int main()
{
int num;
cout<<"What number would you like to convert between 1 and 3999? ";
cin>>num;
string output = intToRoman(num);
system("pause");
return 0;
}
The only thing your intToRoman function returns is a string asking the user to enter a smaller number if the user entered a number larger than 4000.

If the user actually enters a number that is less than 4001, your routine needs to return the string of Roman numerals that it painstakingly put together. The routine should disallow negative numbers, I think, but it doesn't.
 
  • #3
297
2
Here is the code that I have come up with. What could be implimented to make it compile and run? It runs, but all that comes up is the request for the integer between 1 and 3999, after I enter the number the program simply ends.

Code:
int main()
{
    int num;
    cout<<"What number would you like to convert between 1 and 3999? ";
    cin>>num;
    string output = intToRoman(num);
    system("pause");
    return 0;
}
So where, exactly, do you print out the results? Of course it ends before printing anything. You didn't print anything!

Otherwise, the code works. Though you should have gotten a warning for those arrays of strings. They should be 'const char*' not just 'char*'.

Though I strongly suggest you put curly braces after that else. Sure, it's optional since there's only one line, but it's much more obvious and clear. For one, it's easy to read the code wrong. At least put a blank line after then line that's part of the else or people will tend to read them as going together.

Also, Mark is right in that you should also check for negative numbers.

Lastly, it's hard to read and indentation is very important. It would be a kindness if you would wrap your code in CODE tags. Do it like this, removing the spaces between the square brackets that I added so the forum doesn't interpret them as actual CODE tags:

[ CODE ]
YOUR CODE HERE
[ /CODE ]

EDIT: Actually, you allow numbers from 0 to 4000 inclusive, as things stand. 4000 works fine, but you also want to disallow anything less than 1. If you allow 4000, you would want to change the prompt to ask for a number from 1 to 4000. I'll add that you don't have what I'd call good error handling, but not a big deal in this context, perhaps.
 
Last edited:
  • #4
664
3
A few problems that I can see:

1) I believe you're accepting a string as an input, and then passing it to intToRoman as an integer. It's been a long time since I took C++, but I think you're passing it a memory address of the "num" variable, yes? I think you need to convert the string to an integer, for starters.

2) intToRoman passes back a string, but the main program doesn't actually seem to DO anything with that string. Shouldn't it print the string that it received?

DaveE
 
  • #5
33,503
5,191
1) I believe you're accepting a string as an input, and then passing it to intToRoman as an integer. It's been a long time since I took C++, but I think you're passing it a memory address of the "num" variable, yes? I think you need to convert the string to an integer, for starters.
In main, the user is asked to enter num, a variable to type int. num is then passed to intToRoman.
 
  • #6
165
1
I believe I've figured out the issue, like Grep said I needed to print out something. That was left undeclared so now that I have adjusted the code a little bit and added some bits and pieces it compiles and runs just fine. Thanks for your help!!
 
  • #7
664
3
In main, the user is asked to enter num, a variable to type int. num is then passed to intToRoman.
Ok, so cin does the modification from string to int, thanks to the typecasting of "num"? That makes sense-- that's just me being out of C++ practice I guess!

DaveE
 
  • #8
33,503
5,191
Right. cin does the conversion from characters coming in the input stream to an int value.
 

Related Threads on Trying to write a program that converts integers to roman numerals in C++

Replies
1
Views
7K
Replies
1
Views
2K
Replies
6
Views
5K
Replies
18
Views
6K
  • Last Post
Replies
8
Views
3K
  • Last Post
Replies
4
Views
2K
Replies
11
Views
6K
  • Last Post
Replies
24
Views
4K
Replies
14
Views
2K
Top