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]