Dec 1, 2014 at 2:14am UTC
No error but for some reason this wont execute the functions
#include<iostream>
using namespace std;
char num2rom();
int rom2num();
int quit();
bool validate(char choice);
int main()
{
char choice;
cout << "\n1.) Convert a Roman Numeral to an Integer\n"
<< "\n2.) Convert an Integer to a Roman Numeral\n"
<< "\n3.) Exit the Program\n\n";
cin >> choice;
bool validate(char choice);
switch (choice)
{
case 1:
if(choice = 1)
{
choice = num2rom();
}
else
break;
case 2:
if(choice == 2)
{
rom2num();
}
else
break;
case 3:
if(choice == 3)
{
quit();
}
else
break;
}
system("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////
char num2rom()
{
int num = 0;
string rep = "";
cout << "Please enter an interger." << endl;
cin >> num;
switch(num)
{
case 4:
rep += "IV";
num -= 4;
break;
case 40:
rep += "XL";
num -= 40;
break;
case 90:
rep += "XC";
num -= 90;
break;
}
if (num >= 1000){
rep += string(num / 1000,'M');
num %= 1000;
}
if (num >= 500){
rep += string(num / 500,'D');
num %= 500;
}
if (num >= 100){
rep += string(num / 100,'C');
num %= 100;
}
if (num >= 50){
rep += string(num / 50,'L');
num %= 50;
}
if (num >= 10){
rep += string(num / 10,'X');
num %= 10;
}
if (num >= 5){
rep += string(num / 5,'V');
num %= 5;
}
if (num >= 1){
rep += string(num,'I');
num %= 1;
}
cout << rep << endl;
}
////////////////////////////////////////////////////////////////////////////////
int rom2num()
{
char romanNum[16];
int sum=0,i=0;
cout << "Enter the roman numeral :";
cin >> romanNum;
while(romanNum[i]!= '\0')
{
switch(romanNum[i])
{
case 'm':sum+=1000;
break;
case 'd':if(romanNum[i-1]=='c')
sum+=400;
else if(romanNum[i-1]=='l')
sum+=450;
else
sum+=500;
break;
case 'c':if(romanNum[i+1]!='d')
{
if(romanNum[i-1]=='l')
sum+=50;
else
sum+=100;
}
break;
case 'l':if(romanNum[i+1]!='c')
sum+=50;
break;
case 'x':if(romanNum[i-1]=='i')
sum+=9;
else
sum+=10;
break;
case 'v':if(romanNum[i-1]=='i')
sum+=4;
else
sum+=5;
break;
case 'i':if(romanNum[i+1]!='x' || romanNum[i+1]!='v')
sum+=1;
break;
default :cout<<"Invalid Roman numeral arises :";
}
i++;
}
cout << endl << sum << endl << endl;
}
////////////////////////////////////////////////////////////////////////////////
int quit()
{
system("pause");
return 0;
}
////////////////////////////////////////////////////////////////////////////////
bool validate(char choice)
{
if ((choice < 1) || (choice > 3))
cout << "Please enter a valid choice between 1 and 3.\n\n";
else
return true;
}
Dec 1, 2014 at 2:57am UTC
I don't know if this will solve your problem but this jumped out at me
if (choice = 1)
should be if (choice == 1)
Last edited on Dec 1, 2014 at 2:58am UTC
Dec 1, 2014 at 3:04am UTC
I did that with case 2 and 3 with no change. No case executes.
Dec 1, 2014 at 4:59am UTC
its because 1 and 2 and 3 is not considered as a char on your prog. try change char into int to hold the choice variable
Last edited on Dec 1, 2014 at 5:39am UTC
Dec 1, 2014 at 5:04am UTC
That was it. I figured it out 10 minutes ago. lol. just gotta clean it up and add input validation and fix iiii to iv but im a lot closer. thanks!