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

In summary: Get rid of 1000s h = num / 100; num = num % 100; //Get rid of 100s t = num / 10; o = num % 10; roman += thousands[
  • #1
Demon117
165
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
  • #2
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.
 
  • #3
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:
  • #4
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
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.
 
  • #6
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
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
 
  • #8
Right. cin does the conversion from characters coming in the input stream to an int value.
 

1. How can I convert an integer to a roman numeral in C++?

To convert an integer to a roman numeral in C++, you will need to use a combination of mathematical operations and conditional statements. You can start by breaking down the integer into its corresponding values in the Roman numeral system, and then use conditional statements to determine the appropriate numerals to use.

2. What is the most efficient way to write a program for converting integers to roman numerals in C++?

The most efficient way to write a program for converting integers to roman numerals in C++ will depend on your specific approach and coding style. However, some tips for optimizing your program's efficiency include using efficient algorithms, minimizing the number of operations, and avoiding unnecessary loops.

3. Can I use a library or built-in function for converting integers to roman numerals in C++?

There are no built-in functions or libraries in C++ specifically for converting integers to roman numerals. However, you can use various libraries or functions for converting integers to strings and vice versa, which can be helpful in creating a program for converting integers to roman numerals.

4. How do I handle edge cases while converting integers to roman numerals in C++?

Some common edge cases to consider while converting integers to roman numerals in C++ include handling negative integers, handling very large integers, and handling non-integer inputs. To handle these cases, you can use conditional statements and error-handling techniques to ensure your program runs smoothly.

5. Can I convert roman numerals back to integers in C++ using the same program?

Yes, you can modify your program for converting integers to roman numerals in C++ to also handle the reverse conversion. This can be achieved by using the same principles of breaking down the roman numeral into its corresponding values and using conditional statements to determine the appropriate integer value.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
836
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
29
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
17K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Programming and Computer Science
Replies
6
Views
6K
Back
Top