Hello, everyone,
This program is supposed to read roman numerals from a file and convert them to ints using functions. My functions work fine as standalone programs, but they aren't giving me the results I'm expecting.
The weird results point to problem with my do/while loop. The goal is to read in a single character at a time, translate them one at a time, then stop when it gets to whitespace. What am I missing here?
Thanks for any pointers.
#include <iostream>
#include <fstream>
using namespace std;
char get_Data(ifstream& in_File, char& romanLet);
int convert_Roman_to_Decimal(char romanLet, int& dec1);
int main ()
{
// Declare variables
char roman1;
char blank;
int deci=0;
int decimal1=0;
ifstream mp4romanletrdata;
ofstream outRoman;
// Open file
mp4romanletrdata.open("/Users/rob/getDataFunction/build/Release/mp4romanletrdata");
outRoman.open("outRoman");
// Test opening
if (!mp4romanletrdata || !outRoman)
{ cout << "Unable to open files." << endl;
cout << "Program terminates." << endl;
}
// Call function
outRoman << get_Data(mp4romanletrdata, roman1);
do { convert_Roman_to_Decimal(roman1, deci);
decimal1 = decimal1 + deci;
outRoman << decimal1; // for testing
get_Data(mp4romanletrdata, roman1);
}
while (roman1 != ' ');
// Close files
mp4romanletrdata.close();
outRoman.close();
return 0;
}
/*****************************************************
BEGIN
get_Data Function: retrieves a single character from input file
*****************************************************/
char get_Data(ifstream& in_File, char& romanLet)
{
// Declare function variables
char romanLetter;
// Get data char
in_File.get(romanLetter);
return romanLetter;
}/****************************************************
END
get_Data
******************************************************/
/*****************************************************
BEGIN
*convert_Roman_to_Decimal: Takes a single roman character and converts it to a decimal
*****************************************************/
int convert_Roman_to_Decimal(char romanLet, int& dec1)
{
char romanNum;
int decNum;
if (romanNum == 'M')
decNum = 1000;
else if (romanNum == 'D')
decNum = 500;
else if (romanNum == 'C')
decNum = 100;
else if (romanNum == 'L')
decNum = 50;
else if (romanNum == 'X')
decNum = 10;
else if (romanNum == 'V')
decNum = 5;
else if (romanNum == 'I')
decNum = 1;
return decNum;
}/****************************************************
END
convert_Roman_to_Decimal
******************************************************/