Fixing Roman Numeral Conversion Program Errors

In summary, the conversation is about a program that converts user input numbers into Roman numerals using two parallel arrays. The code provided has some errors such as using a variable without giving it a value, trying to add string values to an integer variable, and declaring an unused enumeration. The code also contains some logic errors that may result in incorrect output.
  • #1
bbeth
2
0
I am trying to make a program that let's a user input a number(which counts up 20 elements) and that is converted into roman numerals. I need 2 parallel arrays, one which holds only positive numbers and one which holds only strings. I keep getting 2 error messages "+= int differs in levels of undirection from 'const char[2]'" and "+= illegal right operand has type 'const char[3]'". Any help would be very much appreciated.

This is what I have so far.

#include "stdafx.h"
#include <string>
#include <iomanip>
#include <iostream>
#include <cmath>

using namespace std;

void getRoman(int& roman);
const char;
enum romanType{ M = 1000,D = 500,C = 100,L = 50,X = 10,V = 5, I = 1};

int _tmain( )
{
int n;

int numeral;
int Decimal;
int roman;
string list[20];

cout << "Enter an integer from 1-5000" << endl;
cin >> n;


if((Decimal >= 5000) || (Decimal <=0))
{
cout << "Invalid Integer" << endl;
}

for (int row = 0; row < 20; row++)
{
list[row]=Decimal;
list[row]=n;
}

for( int n = 1; n <= 9999; n++)
{
cout << list[n] << " "<< Decimal;
}

for(n = 1; n <= 5000; n++)


if( n < 5000 )
{
roman = (n / 1000);
roman += "M";
}
roman%= 1000;

if( n >= 100)
{
roman =(Decimal / 100);
if( roman ==9)
{
roman += "CM";
}
else if (roman >= 5)
{
roman += "D";

for( int i = 0; i < roman-5; i++)
{
roman += "C";
}
}
else if ( roman == 4)
{
roman += "CD";
}
else if (roman >1)
{
for( int i= 0; i < roman; i++)
{
roman += "C";
}
}
Decimal %= 100;
}
if ( Decimal >= 10)
{
roman = (Decimal / 10);
if ( roman ==9)
{
roman += "XC";
}
else if( roman >= 5)
{
roman += "L";
for( int n = 0; n < roman-5; n++)
{
roman += "X";
}
}
else if ( roman == 4)
{
roman += "XL";
}
else if ( roman >= 1)
{
for( int n = 0; n < roman; n++)
{
roman += "X";
}
}
Decimal %= 10;
}
if( Decimal >= 1)
{
roman = Decimal;

if( roman == 9)
{
roman += "IX";
}
else if( roman >=5)
{
roman += "V";
for( int n = 0; n < roman-5;n++)
{
roman += "I";
}
}
else if( roman ==4)
{
roman += "IV";
}
else if( roman >= 1)
{
for( int n = 0; n < roman; n++)
{
roman += "I";
}
}
}


system ("pause");
return 0;
}
 
Physics news on Phys.org
  • #2


I figured that out but now I still have a problem with
enum romanType{M=1000 and so on}
I don't have something right but I cannot figure it out. I tried taking out "enum" but that made even more errors. Does someone know what I am doing wrong? please
 
  • #3


When you enter code, surround your code with a [ code ] tag at the top and a [ /code ] tag at the bottom (no spaces). This preserves your indentation and makes your code easier to read. I have done this in your code below.

bbeth said:
I am trying to make a program that let's a user input a number(which counts up 20 elements) and that is converted into roman numerals. I need 2 parallel arrays, one which holds only positive numbers and one which holds only strings. I keep getting 2 error messages "+= int differs in levels of undirection from 'const char[2]'" and "+= illegal right operand has type 'const char[3]'". Any help would be very much appreciated.
The first thing I noticed is that you used Decimal in the first if block before you gave it a value. You never want to do this, because it will have a garbage value.

Looking through your code, I don't see Decimal getting a value anywhere, but you use its value in a lot of places. When you get past your compile errors to a program that runs, you are going to get garbage results because of this.

The next thing I noticed was that you are trying to add a string value to an int variable.
Code:
roman += "M";
You have a lot of lines of code like this. This is very likely the source of at least some of your compile errors.

Since roman is going to contain Roman numerals (i.e., characters), it should be declared as a string, not an int.

Why do you have your romanType enumeration? It's not used anywhere. If it's not used, you should remove it.

bbeth said:
This is what I have so far.
Code:
#include "stdafx.h"
#include <string>
#include <iomanip>
#include <iostream>
#include <cmath>

using namespace std;

void getRoman(int& roman);
const char;
enum romanType{ M = 1000,D = 500,C = 100,L = 50,X = 10,V = 5, I = 1};

int _tmain( )
{
	int n;
	 
	int numeral;
	int Decimal;
	int roman;
	string list[20];
   
	 cout << "Enter an integer from 1-5000" << endl;
    cin >> n;

	
	if((Decimal >= 5000) || (Decimal <=0))
		{
			cout << "Invalid Integer" << endl;
		}

	for (int row = 0; row < 20; row++)
		{
			list[row]=Decimal;
			list[row]=n;
		}

	for( int n = 1; n <= 9999; n++)
	{
		cout << list[n] << " "<< Decimal;
	}
			
		for(n = 1; n <= 5000; n++)
		
		
		if(  n < 5000 )
		{
			roman = (n / 1000);
			roman += "M";
		}
			roman%= 1000;
		
		if( n >= 100)
				{
					roman =(Decimal / 100);
				if( roman ==9)
				{
					roman += "CM";
				}
				else if (roman >= 5)
				{
					roman += "D";
				
				for( int i = 0; i < roman-5; i++)
				{
					roman += "C";
				}
				}
				else if ( roman == 4)
				{
					roman += "CD";
				}
				else if (roman >1)
				{
					for( int i= 0; i < roman; i++)
					{
						roman += "C";
					}
				}
				Decimal %= 100;
				}
				if ( Decimal >= 10)
				{
					roman = (Decimal / 10);
				if ( roman ==9)
				{
					roman += "XC";
				}
				else if( roman >= 5)
				{
					roman += "L";
					for( int n = 0; n < roman-5; n++)
					{
						roman += "X";
					}
				}
				else if ( roman == 4)
				{
					roman += "XL";
				}
				else if ( roman >= 1)
				{
					for( int n = 0; n < roman; n++)
					{
						roman += "X";
					}
				}
				Decimal %= 10;
				}
				if( Decimal >= 1)
				{
					roman = Decimal;
				 
				if( roman == 9)
				{
					roman += "IX";
				}
				else if( roman >=5)
				{
					roman += "V";
				for( int n = 0; n < roman-5;n++)
				{
					roman += "I";
				}
				}
				else if( roman ==4)
				{
					roman += "IV";
				}
				else if( roman >= 1)
				{
					for( int n = 0; n < roman; n++)
					{
						roman += "I";
					}
				}
				}


   system ("pause");
  return 0;

}
 

1. Why is my Roman Numeral Conversion Program not working?

There could be multiple reasons why your program is not working. Some common errors include incorrect syntax, missing variables, or logical errors in the code. Double check your code for any typos or mistakes and make sure you are using the proper algorithms for converting Roman numerals.

2. How do I fix a syntax error in my Roman Numeral Conversion Program?

To fix a syntax error, carefully review your code and make sure all parentheses, brackets, and quotation marks are properly closed. You can also use a syntax checker or debugger to help identify and fix any errors.

3. My program is not converting all Roman numerals correctly. What could be the issue?

There are a few potential issues that could cause incorrect conversions. One possibility is that your program is not accounting for subtractive notation, where a smaller Roman numeral is placed before a larger one to indicate subtraction. Another possibility is that your program is not accounting for repeat symbols, where a Roman numeral is repeated to represent a larger value.

4. How can I improve the efficiency of my Roman Numeral Conversion Program?

To improve efficiency, you can use a lookup table or dictionary to store pre-calculated conversions instead of calculating them each time. You can also optimize your algorithms and eliminate unnecessary steps in the conversion process.

5. What is the best way to test my Roman Numeral Conversion Program?

The best way to test your program is to use a variety of input values and compare the output with expected results. You can also use automated testing tools or unit tests to check for errors and validate the accuracy of your program. Additionally, it can be helpful to have someone else review your code for any potential issues or errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Introductory Physics Homework Help
Replies
21
Views
177
  • Engineering and Comp Sci Homework Help
Replies
3
Views
883
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top