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
 ******************************************************/

Here's a revised version. It's not tested so you'll have to test it. If you don't know why I changed something ask. Basically you seemed to be trying too hard.

#include <iostream>
#include <fstream>

using namespace std;

char get_Data(ifstream&); 
int convert_Roman_to_Decimal(char); 

int main ()
{

  // Declare variables
  char roman1; 
  char blank;
  int deci = 0;
  int decimal1 = 0;

  ifstream mp4romanletrdata;
  ofstream outRoman;

  // Open files
  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; 
    exit(1);
  }

  do 
  { 
   roman1 = get_Data(mp4romanletrdata);
   
   if(roman1 != '~')
   {
     deci = convert_Roman_to_Decimal(roman1);
     decimal1 = decimal1 + deci;
   }
  }while (roman1 != '~');

  outRoman << decimal1;

  // Close files
  mp4romanletrdata.close();
  outRoman.close();

  return 0;
}


char get_Data(ifstream& in_File)
{
  char romanLetter;

  if(in_File.get(romanLetter)) 
    return romanLetter;
  else 
    return '~';
}

int convert_Roman_to_Decimal(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;
}

I'm not sure I'd do it the way you have, unless I had too per my instrcutors instructions, but so be it. At least this should do what you had been trying to do.

Also, it won't take into account that going from left to right, for any two consecutive roman numeral char if the left roman numeral char has a smaller value than the right, then the left should be subtracted from the right. So:

IV

is 4 not six.

Thanks, Lerner.

The tilde is to mark the ws?

The program deals with additive romans only.

The tilde is just some char that isn't used in any other context within the program to indicate that the end of the roman numeral was reached, presumably because end of file was found. If there is more than one roman numeral in the file, say VI and MMCII both, then you'll need to be able to pick that up, too, to know when to stop this roman numeral conversion and move on to the next. For example, if there is more than one roman numeral per file then I'd probably read in a whole roman numeral at once into a string and then send each char of the roman numeral string for conversion using a loop before reading the next roman numeral from the file.

OK, I got this to work as desired/required. Thanks.

Going to go beat my head against my other problems now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.