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

  • Context: C/C++ 
  • Thread starter Thread starter Demon117
  • Start date Start date
  • Tags Tags
    C++ Integers Program
Click For Summary

Discussion Overview

The discussion revolves around a C++ program designed to convert integers to Roman numerals. Participants are troubleshooting issues related to the program's functionality, including input handling, output display, and error checking.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the program compiles but does not display the converted Roman numeral, suggesting the need to print the output after calling the conversion function.
  • Another participant points out that the program should check for negative numbers and clarify the input range, indicating that the prompt should specify numbers between 1 and 4000.
  • Concerns are raised about the handling of input types, with a suggestion that the program should convert input strings to integers properly.
  • Some participants suggest improvements to code readability, such as using curly braces for clarity and proper indentation.
  • One participant mentions that the code should return a string of Roman numerals if the input is valid and less than or equal to 4000.
  • Another participant confirms that the input handling with `cin` automatically converts the input from string to integer, which is acknowledged by others.

Areas of Agreement / Disagreement

Participants generally agree on the need for output display and input validation, but there are differing views on the specifics of error handling and code structure. The discussion remains unresolved regarding the best practices for implementing these features.

Contextual Notes

Limitations include the lack of error handling for inputs outside the specified range and the absence of output statements to display results. The program currently allows numbers from 0 to 4000, which may not align with the intended functionality.

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;
count<<"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;
count<<"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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
13
Views
3K
Replies
29
Views
5K
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
18K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K