Hello,

This is my program here...I want to make it read from a textfile and displayed in this way

Number : 1
Roman Numeral : I

Number : 2
Roman Numeral : II

My textfile is in this form:

1
2

but i still unable to solve it...is something to do with the highlight part. Please guide me along.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fin;
    string romNum;
    //string filename;
    int araNum;

    fin.open("number.txt");
    if (!fin.good())
    {
        cout << "File not found" << endl;
		return 1;
    }

	while(!fin.eof())
	{

       [B] fin >> romNum >> araNum ;
        cout << "Number : " << romNum << endl;
        cout << "Roman Numeral : " << araNum << endl << endl;[/B]

    while (araNum >=1000)
        {
            cout <<"M";
            araNum -= 1000; //to stop infinity of while loop
        }
    while (araNum >=900)
        {
            cout <<"CM";
            araNum -= 900;
        }
    while (araNum >=500)
        {
            cout <<"D";
            araNum -= 500;
        }
    while (araNum >=400)
        {
            cout <<"CD";
            araNum -= 400;
        }
    while (araNum >=100)
        {
            cout <<"C";
            araNum -= 100;
        }
    while (araNum >=90)
        {
            cout <<"XC";
            araNum -= 90;
        }
    while (araNum >=50)
        {
            cout <<"L";
            araNum -= 50;
        }
    while (araNum >=40)
        {
            cout <<"XL";
            araNum -= 40;
        }
    while (araNum >=10)
        {
            cout <<"X";
            araNum -= 10;
        }
    while (araNum >=9)
        {
            cout <<"IX";
            araNum -= 9;
        }
    while (araNum >=5)
        {
            cout <<"V";
            araNum -= 5;
        }
    while (araNum >=4)
        {
            cout <<"IV";
            araNum -= 4;
        }
    while (araNum >=1)
        {
            cout <<"I";
            araNum -= 1;
        }
	}

}

This line:

fin >> romNum >> araNum ;

It will try to pull in 2 numbers at once from your input file. It should be:

fin >> araNum ;

Since you are reading in the Arabic number. Then convert that to Roman.

After the fin method, the while method is doing the conversion... I not quite sure. ..i tried using just fin >> araNum;

The result is like this

Number :
Roman Numeral : 1

INumber :
Roman Numeral : 2

IINumber :
Roman Numeral : 0

The converted roman tend to be behind the Number as u see

That's just a formatting issue. You want to insert a line to write out another carriage return when you are at the bottom of the main while loop - just before you are going to read in another line from the input file.

Also, make sure you're consistent with your variable names. You really don't need a variable for "romNum", since you are outputting the result a-Roman-numeral-at-a-time as you go through your while loops.

I see...Thank you..Last thing is..i not very sure how come my output there is a 0. I check my code i nv handle anything that is related with 0

You may have a trailing empty line at the bottom of your input file. Make sure the last line of the file doesn't have a CR/LF on it, and that should take care of the issue.

You could also check to see if your Arabic number is zero, and if so, skip the conversion.

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.