Fixing Roman Numeral Conversion Program Errors

  • Thread starter Thread starter bbeth
  • Start date Start date
  • Tags Tags
    Errors Program
Click For Summary
SUMMARY

The forum discussion focuses on resolving errors in a Roman numeral conversion program written in C++. The user encounters two specific error messages related to type mismatches when attempting to concatenate strings to an integer variable. Key issues identified include the uninitialized variable 'Decimal', incorrect data types for the 'roman' variable, and the unnecessary 'romanType' enumeration. The solution involves declaring 'roman' as a string and ensuring 'Decimal' is assigned a value before use.

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of data types (int vs string)
  • Basic knowledge of arrays in C++
  • Familiarity with control structures (if statements, loops)
NEXT STEPS
  • Refactor the Roman numeral conversion logic to use string concatenation correctly
  • Implement input validation for the 'Decimal' variable before processing
  • Explore C++ string manipulation techniques for efficient concatenation
  • Review C++ enumerations and their appropriate use cases
USEFUL FOR

Programmers and developers working on C++ projects, particularly those interested in string manipulation and error handling in coding practices.

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];

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


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

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

for( int n = 1; n <= 9999; n++)
{
count << 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 ·
Replies
19
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K