Fixing Roman Numeral Conversion Program Errors

  • Thread starter Thread starter bbeth
  • Start date Start date
  • Tags Tags
    Errors Program
AI Thread Summary
The discussion focuses on fixing errors in a Roman numeral conversion program. Key issues include uninitialized variables, specifically the "Decimal" variable, which leads to garbage values being used in calculations. Additionally, the program attempts to concatenate string literals to an integer variable "roman," causing type mismatch errors. It is suggested that "roman" should be declared as a string to store Roman numeral characters correctly. The unused enumeration "romanType" is also identified as unnecessary and should be removed to streamline the code.
bbeth
Messages
2
Reaction score
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


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
 


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;

}
 

Similar threads

Replies
19
Views
2K
Replies
7
Views
2K
Replies
3
Views
1K
Replies
2
Views
3K
Replies
5
Views
3K
Replies
12
Views
2K
Back
Top