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;}
void menu(void);
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");
numeral.menu();
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;
}
void Roman::menu(void)
{
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();
}