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

AI Thread Summary
The discussion centers on a C++ program designed to convert integers to Roman numerals, but it encounters issues where the output is not displayed after user input. The primary problem identified is that the program does not print the result of the conversion; it only prompts for input and ends without displaying any output. Suggestions include adding a print statement to show the converted Roman numeral string. Additionally, the code needs to handle negative numbers and ensure that the input is limited to the range of 1 to 3999, with a recommendation to clarify the input prompt accordingly. There are also notes on improving code readability, such as using `const char*` for string arrays and adding curly braces for clarity in control structures. Overall, the code compiles but requires these adjustments to function as intended.
Demon117
Messages
162
Reaction score
1
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;
}
 
Technology news on Phys.org
matumich26 said:
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.
 
matumich26 said:
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:
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
 
davee123 said:
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.
 
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!
 
Mark44 said:
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
 
Right. cin does the conversion from characters coming in the input stream to an int value.
 
Back
Top