Hi,
I'm doing a project regarding converting roman numerals to decimals and vice versa. With some reference from the internet, I was able to come up with one. However, it isn't working very well. Can someone look at my codes and point out what/where's the mistakes?
Thanks in advance.

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

int checkStatus(string);
string toRoman(string);


int main()
{
    ifstream fin;
    string filename, data, roman;
    int status;
    
    cout << "Please enter filename: ";
    getline(cin, filename);
    
    fin.open(filename.c_str());
    if (!fin.good())
    {
        cout << "File not found" << endl;
        system("pause");
		return 1;
    }

	while (!fin.eof())
	{
        getline(fin, data);       
        status = checkStatus(data);
        if (status == 0)
            cout << data << "\t\t" << "Invalid" << endl;
        else if (status == 1)
        {
            roman = toRoman(data);
            cout << data << "\t\t";
            for (int c = 0; (isalpha(roman[c])) ; c++)
                   cout << roman[c];
            cout << endl;
        }
        else if (status == 2)
            cout << data << "\t\t" << "Decimal" << endl;
       
    }
    fin.close();    
    system("pause");
    return 0;
}
 

int checkStatus(string data)
{
    char temp;
    if (!isdigit(data[0]))
    {
        for (int b = 0; b < data.length(); b++)
        {
            temp = toupper(data[b]);
            if ((temp != 'I') && (temp != 'V') &&
                (temp != 'X') && (temp != 'L') &&
                (temp != 'C') && (temp != 'D') &&
                (temp != 'M'))
                return 0;
        }
        return 2;
    }
    else
    {
        for (int b = 0; b < data.length(); b++)
        {
            if (!isdigit(data[b]))
                return 0;
        }
        return 1;   
    }
}

string toRoman(string data)
{
    int a;
    int i = 0;
    string romanF;

    a = atoi(data.c_str());
    while(a > 0)
    {
        if((a >= 1000) && (a < 4000))
        {	
            romanF[i] = 'M'; 
            a = a-1000;
            i ++;   
        }
        else if((a >= 900) && (a < 1000))
        {	
            romanF[i] = 'C';
            romanF[i+1] = 'M';
            a = a-900;
            i += 2;               
        }        
        else if((a >= 500) && (a < 900))
        {	
            romanF[i] = 'D'; 
            a = a-500;
            i ++;               
        }        
        else if((a >= 400) && (a < 500))
        {	
            romanF[i] = 'C';
            romanF[i+1] = 'D';
            a = a-400;
            i += 2;               
        }        
        else if((a >= 100) && (a < 400))
        {
            romanF[i] = 'C'; 
            a = a-100;
            i ++;                	
        }        
        else if((a >= 90) && (a < 100))
        {	
            romanF[i] = 'X';
            romanF[i+1] = 'C';
            a = a-90;
            i += 2;               
        }
        else if((a >= 50) && (a < 90))
        {
            romanF[i] = 'L'; 
            a = a-50;
            i ++;                	
        }        
        else if((a >= 40) && (a < 50))
        {	
            romanF[i] = 'X';
            romanF[i+1] = 'L';
            a = a-40;
            i += 2;               
        }    
        else if((a >= 10) && (a < 40))
        {	
            romanF[i] = 'X'; 
            a = a-10;
            i ++;   
        }        
        else if((a >= 9) && (a < 10))
        {	
            romanF[i] = 'I';
            romanF[i+1] = 'X';
            a = a-9;
            i += 2;               
        }        
        else if ((a >= 5) && (a < 9))
        {	
            romanF[i] = 'V'; 
            a = a-5;
            i ++;   
        }            
        else if ((a >= 4) && (a < 5))
        {
            romanF[i] = 'I';
            romanF[i+1] = 'V';
            a = a-4;
            i += 2;             	
        }                      
        else if ((a >= 1) && (a < 4))
        {
            romanF[i] = 'I'; 
            a = a-1;
            i ++;            
        }
    }
    return romanF;
}

The answers are suppose to be those on the textfile, instead I get some extra characters for some numbers.
I went through and followed the numbers but can't figure out why.
[IMG]http://i126.photobucket.com/albums/p98/justln/untitled-2.jpg[/IMG]


So I tried to limit the codes and location of the mistakes.[IMG]http://i126.photobucket.com/albums/p98/justln/untitled2.jpg[/IMG]

Weird... The first few numbers and once in awhile it works fine. :(

Just had a quick look but something im gonna point out.

else if ((a >= 4) && (a < 5))
        {
            romanF[i] = 'I';
            romanF[i+1] = 'V';
            a = a-4;
            i += 2;             	
        }

could become

else if (a==4)
        {
            romanF[i++] = 'I';
            romanF[i++] = 'V';
            a -= 4;             	
        }

This has made it cleaner, but also it has reduded the time complexity by only having to perform 1 comparison.

Chris

Aaa, I haven't refine my codes yet so it's a little messy.
I only did the toRoman part, haven't touch on the fromRoman part yet. But thanks for the input :D

My friend just pointed out to me that the string looks like it wasn't cleared so there's extra I's at the end. Hmmm...

Woot! I just solved it.

else if (status == 1)
{
roman = toRoman(data);
cout << data << "\t\t";
for (int c = 0; isalpha(roman[c]) ; c++)
{
cout << roman[c];
roman[c] = ' ';
}

But it's weird on how I have to clear the string. Shouldn't it be a new string everytime?
I tried using string.clear() but it doesn't work. Hmmm

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.