0

I'm having trouble getting this program to work for roman numeral conversion. I hope it says that i have one error, which is C2110: cannot add two pointers. here's what i have so far... ANY help would be great, thanks a bunch! :)

#include <iostream>
using namespace std;

int main()
{
	//Type in an integer between 1 and 4000
	int numToConvert;
	cout << "Enter an integer between 1 and 4000";
	cin >> numToConvert;
	int th,h,t,o;
	char thousands[] = {"", "M", "MM", "MMM", "MMMM"};
	char hundreds[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
	char tens[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
	char ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

	if (numToConvert<=4000){
		int romanNumber;
		th = numToConvert / 1000;
		numToConvert = numToConvert % 1000;
		h = numToConvert % 100;
		t = numToConvert / 10;
		o = numToConvert % 10;
		romanNumber = romanNumber + thousands[th] + hundreds[h] + tens[t] + ones[o];
		cout << "The roman number for this integer is " << romanNumber;
	}
	else
	{
		cout << "Invalid number, please enter a new number between 1 and 4000";
		return 0;
	}
}

Edited by WaltP: Added CODE Tags -- please use them

5
Contributors
8
Replies
17
Views
5 Years
Discussion Span
Last Post by raptr_dflo
Featured Replies
  • 1

    [icode]char thousands[] = {"", "M", "MM", "MMM", "MMMM"};[/icode] This is an error; thousands is an array of chars, not an array of c-style strings. You need to do something like this instead. [icode]const char* thousands[] = {"", "M", "MM", "MMM", "MMMM"};[/icode] > i have one error, which is C2110: cannot … Read More

  • Also, if you haven't discovered it yet, your conversion is broken starting at line 20. Take any starting number, say, 2937, and trace through that block of code by hand, and see what values it's going to get for the four index variables. Read More

  • And romanNumber is declared as int at line 17. Definitely not the case. :) Read More

0

I'm having trouble getting this program to work for roman numeral conversion. I hope it says that i have one error, which is C2110: cannot add two pointers. here's what i have so far... ANY help would be great

1) Use CODE Tags
2) Don't tell us you hope for an error, tell us exactly what error you get
3) Don't make us guess what line the error is on. Tell us. We aren't psychicprogrammers.com you know.

1

char thousands[] = {"", "M", "MM", "MMM", "MMMM"}; This is an error; thousands is an array of chars, not an array of c-style strings.
You need to do something like this instead. const char* thousands[] = {"", "M", "MM", "MMM", "MMMM"}; > i have one error, which is C2110: cannot add two pointers.

If you want to use the + operator to concatenate strings, use std::string instead of c-style string.

char a[100] = "abcd" ;
const char b[] = "ef" ;
a = a + b ; // *** error *** 

std::string c = "abcd" ;
c = c + b ; // fine

See: http://www.cprogramming.com/tutorial/string.html

1

Also, if you haven't discovered it yet, your conversion is broken starting at line 20. Take any starting number, say, 2937, and trace through that block of code by hand, and see what values it's going to get for the four index variables.

0

Hmmm my programming teacher hasn't even taught what a string is... nor is it explained in my book. Not quite sure where to put the std::string function in my program, but i'll give it a whack. thanks for the feedback, and i'll post my program once i get it edited

0

Hey i figured it out. thanks for the help guys. :) cheers

Edited by Jared1337: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.