First off, happy thanksgiving everybody. Second, my program has problems. I've discovered a horrible way (for me anyway) to design and my attempts to fix it aren't getting any where. I have to design a roman numeral calculator (input romans, output romans) with at least two functions (decimal->roman, roman->decimal). I started by designing three programs, a calculator, a decimal converter, and a roman converter, and thought I'd combine them later. All three worked fine independently, but as I've learned, combining them has been difficult.
The program compiles and runs but takes 3 inputs, on 3 separate lines, and gives me nothing back. Example:

Enter a roman expression.
X
X
X
=
Press any key to continue...

What I want:
Enter a roman expression
V + VI
= XI


Here's my code. Any help will be greatly appreciated.

[LIST=1]
[*]#include<iostream>
[*]#include<string>
[*]#include<iomanip>

[*]using namespace std;
[*]int sum;
[*]int solution;
[*]int result;

[*]int convertToDecimal(string roman)
[*]{
[*]	char romanNum[100];
[*]	
[*]	cin >> romanNum;
[*]	int length = strlen(romanNum);
[*]	int number = 0;
[*]	int counter = 0;
[*]	
[*]	for (counter = length; counter >= 0; counter--)
[*]	{
[*]		if (romanNum[counter] == 'M')
[*]			number = 1000;
[*]		else if (romanNum[counter] == 'D')
[*]			number = 500;
[*]		else if (romanNum[counter] == 'C')
[*]			number = 100;
[*]		else if (romanNum[counter] == 'L')
[*]			number = 50;
[*]		else if (romanNum[counter] == 'X')
[*]			number = 10;
[*]		else if (romanNum[counter] == 'V')
[*]			number = 5;
[*]		else if (romanNum[counter] == 'I')
[*]			number = 1;
[*]		else
[*]			number = 0;

[*]		sum = sum + number;
[*]}
[*]	
[*]	return sum;
[*]}

[*]string convertToRoman (int solution)
[*] {
[*]    string romanNum = "";
[*]    
[*]	while (solution >=1000)
[*] {
[*]        romanNum+="M";
[*]        solution = solution - 1000;
[*] }
[*]    
[*]while(solution >=500) 
[*] {
[*]        romanNum+="D";
[*]        solution = solution - 500;
[*] }
[*] while(solution >=100) 
[*] {
[*]        romanNum+="C";
[*]        solution = solution - 100;
[*] }
[*] 
[*] while(solution >=50)
[*] {
[*]        romanNum+="L";
[*]        solution = solution - 50;
[*] }
[*] 
[*] while(solution >=10)
[*] {
[*]        romanNum+="X";
[*]        solution = solution - 10;
[*] }
[*]  
[*] while(solution >=5)
[*] {
[*]        romanNum+="V";
[*]        solution = solution - 5;
[*] }

[*] while(solution >=1)
[*]{
[*]        romanNum+="I";
[*]        solution = solution - 1;
[*]}

[*]return romanNum;
[*]}



[*]int main()
[*]{
[*]string firstNum;
[*]string secondNum;
[*]char operation;


[*]cout << "Enter a roman expression." << endl;
[*]cin>>firstNum>>operation>>secondNum;

[*]//need while loop for 0 values

[*]{

[*]switch(operation)
[*]{
[*]case '+': result = convertToDecimal(firstNum) + convertToDecimal(secondNum);
[*]	break;

[*]case '-': result = convertToDecimal(firstNum) - convertToDecimal(secondNum);
[*]	break;

[*]case '*': result = convertToDecimal(firstNum) * convertToDecimal(secondNum);
[*]	break;

[*]}

[*]cout << "= " << convertToRoman(result) << endl << endl;
[*]cout << "Enter a roman expression." << endl;
[*]}

[*]    return 0;
[*]}

[*]

[/LIST]

You also need to consider that 900 say is written as "CM" (ie 100 before 1000), not as 9 C's in a row.

But according to your input, 'X' is not a valid operation?

Per the assignment we don't have to account for subtraction values. So IV and VI = 6.

And my example was bad and confusing, sorry. I input three capital X's (not a multiplication lower x) but it doesn't matter as far as running the program. Any input, outputs nothing. So even X + V leaves a blank after =. But I don't get the = until I input 3 values on 3 separate lines. So more accurately:

X + V ->program waits until two more characters are input
here
and here, then
=

or

X
+
V
=
Press any key...

Hopes this clears it up.

Intinalize sum to zero in convertToDecimal() before you try to add something to it.

You never use the values passed to convertToDecimal() in the form of roman. Instead, you ask for additional input in the form of romanNum.

You never use the values passed to convertToDecimal() in the form of roman. Instead, you ask for additional input in the form of romanNum.

I'm not sure I understand this part. I pass the value as a string to convertToDecimal and need to return an integer but can't redefine things. I used the length member function to read from the string but convert the characters individually and sum the values. Is the parameter wrong, or should I change the function?

Delete lines 10 and 11, and make it use the parameter you pass into the function (which you've already read from the user in main).

I changed this:

int convertToDecimal(string&)
{
	
	char roman[20];
	int length = strlen(roman);

My output is in the correct format but there's no answer.

>>My output is in the correct format but there's no answer.

I'm not sure what this means. However, if all you did was change the lines you listed in post #7, then you've got some more work to do. Remove all mention to romanNum by replacing every appearance to roman in convertToDecimal(), or rename the variable passed to convertToDecimal() from roman to romanNum.

Between line 33 and 34 of the original post output the value of sum to the screen before you pass it back to main() to see what the value is. (or use your debugger to check it's value). I suspect it will be junk, unless you've initialized sum to some useful value (probably zero) before you try to use it each time convertToDecimal() is called.

My output being in the right format means it looks like this after running it:

Enter a roman expression
X + X
=
Enter a roman expression.

Post #3 has the old output.

I had changed all of the romanNums to roman and sum is initialized as 0 in convertToDecimal. I did what you suggested with cout statement, and my output was 50 lines, with one zero per line. Here's the revised code

[LIST=1]
[*]#include<iostream>
[*]#include<string>
[*]#include<iomanip>

[*]using namespace std;
[*]int sum;
[*]int solution;
[*]int result;

[*]int convertToDecimal(string&)
[*]{
[*]	
[*]	char roman[20];
[*]	int length = strlen(roman);
[*]	int number = 0;
[*]	int counter = 0;
[*]	int sum = 0;
[*]	
[*]	for (counter = length; counter >= 0; counter--)
[*]	{
[*]		if (roman[counter] == 'M')
[*]			number = 1000;
[*]		else if (roman[counter] == 'D')
[*]			number = 500;
[*]		else if (roman[counter] == 'C')
[*]			number = 100;
[*]		else if (roman[counter] == 'L')
[*]			number = 50;
[*]		else if (roman[counter] == 'X')
[*]			number = 10;
[*]		else if (roman[counter] == 'V')
[*]			number = 5;
[*]		else if (roman[counter] == 'I')
[*]			number = 1;
[*]		else
[*]			number = 0;

[*]		sum = sum + number;
[*]		
[*]		cout << sum << endl;
[*]}
[*]	
[*]	return sum;
[*]}

[*]string convertToRoman (int solution)
[*] {
[*]    string roman = "";
[*]    
[*]	while (solution >=1000)
[*] {
[*]        roman+="M";
[*]        solution = solution - 1000;
[*] }
[*]    
[*]while(solution >=500) 
[*] {
[*]        roman+="D";
[*]        solution = solution - 500;
[*] }
[*] while(solution >=100) 
[*] {
[*]        roman+="C";
[*]        solution = solution - 100;
[*] }
[*] 
[*] while(solution >=50)
[*] {
[*]        roman+="L";
[*]        solution = solution - 50;
[*] }
[*] 
[*] while(solution >=10)
[*] {
[*]        roman+="X";
[*]        solution = solution - 10;
[*] }
[*]  
[*] while(solution >=5)
[*] {
[*]        roman+="V";
[*]        solution = solution - 5;
[*] }

[*] while(solution >=1)
[*]{
[*]        roman+="I";
[*]        solution = solution - 1;
[*]}

[*]return roman;
[*]}



[*]int main()
[*]{

[*]string firstNum;
[*]string secondNum;
[*]char operation;


[*]cout << "Enter a roman expression." << endl;
[*]cin>>firstNum>>operation>>secondNum;



[*]{

[*]switch(operation)
[*]{
[*]case '+': result = convertToDecimal(firstNum) + convertToDecimal(secondNum);
[*]	break;

[*]case '-': result = convertToDecimal(firstNum) - convertToDecimal(secondNum);
[*]	break;

[*]case '*': result = convertToDecimal(firstNum) * convertToDecimal(secondNum);
[*]	break;

[*]}

[*]cout << "= " << convertToRoman(result) << endl << endl;
[*]cout << "Enter a roman expression." << endl;
[*]}

[*]    return 0;
[*]}
[/LIST]

I'm in the process of writing the function separately, with a simple driver and it is not working there either. Every time I change it it seems to get worse.

To obtain the length of a C style string object use strlen. To obtain the length of an STL string use either the size() or the length() member function of the string class.

If the length of a string called str is len then the last valid element of the string is str[len - 1] with the null char at index len -1 and the last non-null char at index len - 2. That means you are probably out of bounds with the value counter when you start the loop in convertToDecimal.

# int convertToDecimal(string&)
# {
# char roman[20];
Try
int convertToDecimal(char roman[20])

Or better yet, make roman your std::string variable.

By leaving the parameter unnamed, you're passing a value, but totally ignoring it.

Do you have a debugger?
If so, now is the time to get used to using it.

Darn it, I 'm not thinking so clearly today I guess. The off by one error I've been talking about probably isn't. Sorry.

# int convertToDecimal(string&)
# {
# char roman[20];
Try
int convertToDecimal(char roman[20])

Or better yet, make roman your std::string variable.

By leaving the parameter unnamed, you're passing a value, but totally ignoring it.

Do you have a debugger?
If so, now is the time to get used to using it.

I tried debugging with the visual studio 2005 debugger and it was reading junk values (-50 for every one). I tried changing the parameter to what you suggested but it couldn't compile since the parameter was changing the 'std::string' to 'char []'. I'm getting very confused.
I've isolated the function, and this code works fine on its own. Just can't get it to play with the rest of the program. I don't know if this will help but I'll post anyway.

#include <iostream>
#include <string>
using namespace std;

int convertToDecimal(int&);
int sum;

int main ()
{
cout << "Enter a roman numeral: ";

convertToDecimal(sum);

cout << "The decimal number is: " << sum << endl;

return 0;
}



int convertToDecimal(int& sum)
{
	char romanNumb[20];
	
	cin >> romanNumb;
	int length = strlen(romanNumb);
	int number = 0;
	int counter = 0;
	
	

	for (counter = length; counter >= 0; counter--)
	{
		if (romanNumb[counter] == 'M')
			number = 1000;
		else if (romanNumb[counter] == 'D')
			number = 500;
		else if (romanNumb[counter] == 'C')
			number = 100;
		else if (romanNumb[counter] == 'L')
			number = 50;
		else if (romanNumb[counter] == 'X')
			number = 10;
		else if (romanNumb[counter] == 'V')
			number = 5;
		else if (romanNumb[counter] == 'I')
			number = 1;
		else
			number = 0;

		sum = sum + number;


	}
	
	return sum;
}

It could be something like:

int convertToDecimal(string);

int main()
{
  string roman;
  cin >> roman;
  int num = convertToDecimal(roman);
}

int convertToDecimal(string roman)
{
  int length = roman.length();
  int number = 0;
  int counter = 0;
  sum = 0;
  for(counter = length; counter >= 0; counter --)
  //etc
  
  return sum;

or

int convertToDecimal(char * roman);
int main()
{
   char roman[10];
   cin >> roman;
   int num =  convertToDecimal(roman);
}

int convertToDecimal(char * roman)
{
  int length = strlen(roman);
  int number = 0;
  int counter = 0;
  sum = 0;

  for (counter = length; counter >= 0; counter--)
  //etc

  return sum;

It works. Thank you guys so much. I thought I was just digging a hole for a while there. I still need to loop the main until 0 is input but I'm sure I can handle that. Thank you again.

esto esta mal
falta para casos como 9,19 etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.