# Roman Numeral Calculator

This is a discussion on Roman Numeral Calculator within the C++ Programming forums, part of the General Programming Boards category; Beginning stages of a roman numeral to regular number calculator. This program assumes that the user is going to enter ...

1. ## Roman Numeral Calculator

Beginning stages of a roman numeral to regular number calculator. This program assumes that the user is going to enter a valid roman numeral. Compiles fine, but numbers are always 1000 for some reason. Need a second pair of eyes on this:

Code:
```#include<iostream>
#include<cstdlib>
#include<string>
#include<iomanip>
#include<conio.h>

#define I 1
#define V 5
#define X 10
#define L 50
#define C 100
#define D 500
#define M 1000

using namespace std;

class Roman
{
public:
//Function Prototypes
Roman(){total=0; counter=0;}
bool is_ronumeral(const char&) const;
void calculate(char&);
void error_message(void);

private:
//Globals
int total;
int counter;
string user_input;
int numarray[80];
};

int main()
{
Roman numeral;

char choice;

do{
system("cls");

choice=toupper(getch());

if(numeral.is_ronumeral(choice)  && choice!='Q')

numeral.calculate(choice);

if(!numeral.is_ronumeral(choice) && choice!='Q')

numeral.error_message();

}while(choice!='Q');

system("cls");

return EXIT_SUCCESS;
}

{
cout << endl << endl
<< setw(10) << "***************************************"     << endl
<< setw(10) << "* Welcome to Roman Numeral Calcultor! *"     << endl
<< setw(10) << "***************************************\n"   << endl
<< setw(10) << "Enter your roman numerals one at a time"     << endl
<< setw(10) << "Enter 'Q' to Quit\n\n"
<< endl << endl
<< "\tNumerals Entered: " << user_input
<< "\n\n\tCurrent Total: " << total
<< endl << endl << endl << endl << endl
<< "\tEnter Roman Numeral: ";
}

bool Roman::is_ronumeral(const char& keydown) const
{
switch(keydown)
{
case 'I':
case 'V':
case 'X':
case 'L':
case 'C':
case 'D':
case 'M': return true;

default:  return false;
}
}

void Roman::calculate(char& numeral)
{
user_input+=numeral;
user_input+=" ";

switch(numeral)
{
case 'I': numarray[counter]=I;
case 'V': numarray[counter]=V;
case 'X': numarray[counter]=X;
case 'L': numarray[counter]=L;
case 'C': numarray[counter]=C;
case 'D': numarray[counter]=D;
case 'M': numarray[counter]=M;
}

if(!counter)
total=numarray[counter];

else
{
if(numarray[counter]<=numarray[counter-1])
total += numarray[counter];

if(numarray[counter]>numarray[counter-1])
total += (numarray[counter] - numarray[counter-1]);
}

++counter;
}

void Roman::error_message(void)
{
cout << "\n\n\t\aThat is not a Roman Numeral!  Try Again.";
cout << "\n\b\tPress [Enter] to continue...";
//cin.ignore(200,'n');
cin.get();
}```

2. I think you forgot breaks on the case statement

EDIT
Yep, that was the problem

3. good call

Code:
```void Roman::calculate(char& numeral)
{
user_input+=numeral;
user_input+=" ";

switch(numeral)
{
case 'I': numarray[counter]=I;
break;
case 'V': numarray[counter]=V;
break;
case 'X': numarray[counter]=X;
break;
case 'L': numarray[counter]=L;
break;
case 'C': numarray[counter]=C;
break;
case 'D': numarray[counter]=D;
break;
case 'M': numarray[counter]=M;
break;
}```

4. Think I got the final program here:

Code:
```#include<iostream>
#include<cstdlib>
#include<string>
#include<iomanip>
#include<conio.h>

#define I 1
#define V 5
#define X 10
#define L 50
#define C 100
#define D 500
#define M 1000

using namespace std;

class Roman
{
public:
//Function Prototypes
Roman(){total=0; counter=0;}
bool is_ronumeral(const char&) const;
void calculate(char&);
void error_message(void);

private:
//Globals
int total;
int counter;
string user_input;
int numarray[80];
};

int main()
{
Roman numeral;

char choice;

do{
system("cls");

choice=toupper(getch());

if(numeral.is_ronumeral(choice)  && choice!='Q')

numeral.calculate(choice);

if(!numeral.is_ronumeral(choice) && choice!='Q')

numeral.error_message();

}while(choice!='Q');

system("cls");

return EXIT_SUCCESS;
}

{
cout << endl << endl
<< setw(10) << "***************************************"     << endl
<< setw(10) << "* Welcome to Roman Numeral Calcultor! *"     << endl
<< setw(10) << "***************************************\n"   << endl
<< setw(10) << "Enter your roman numerals one at a time"     << endl
<< setw(10) << "Enter 'Q' to Quit\n\n"
<< endl << endl
<< "\tNumerals Entered: " << user_input
<< "\n\n\tCurrent Total: " << total
<< endl << endl << endl << endl << endl
<< "\tEnter Roman Numeral: ";
}

bool Roman::is_ronumeral(const char& keydown) const
{
switch(keydown)
{
case 'I':
case 'V':
case 'X':
case 'L':
case 'C':
case 'D':
case 'M': return true;

default : return false;
}
}

void Roman::calculate(char& numeral)
{
user_input+=numeral;

switch(numeral)
{
case 'I': numarray[counter]=I;
break;
case 'V': numarray[counter]=V;
break;
case 'X': numarray[counter]=X;
break;
case 'L': numarray[counter]=L;
break;
case 'C': numarray[counter]=C;
break;
case 'D': numarray[counter]=D;
break;
case 'M': numarray[counter]=M;
}

if(!counter)
total=numarray[counter];

else
{
if(numarray[counter]<=numarray[counter-1])
total += numarray[counter];

if(numarray[counter]>numarray[counter-1])
total += (numarray[counter] - (2 * numarray[counter-1]));
}

++counter;
}

void Roman::error_message(void)
{
cout << "\n\n\t\aThat is not a Roman Numeral!  Try Again.";
cout << "\n\tPress [Enter] to continue...";
cin.get();
}```

Popular pages Recent additions