Conversion of Roman Numerals

Hey guys for my assignment i have to write a program which checks the information inside a text file to make sure it is all integers and then convert the integers to roman Numerals. My program seems to have a bit of a problem and im not sure where exactly the problem is....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

#include<stdlib.h>
#include<string>
#include<iostream>
#include<fstream>

using namespace std ;

class checkfile
{
public :
bool is_number (string& );
   private :

};

class Convert
{
public :
    void ConvertToRoman (string& );
private :
};



////cpp file to check for number/////
bool checkfile::is_number(string&s)
{
   for (int i = 0; i < s.length(); i++) {
       if (!isdigit(s[i]))
           return false;       
  }
   return true;
}

/////cpp.file convert numbers to roman Numerals///
void Convert::ConvertToRoman(string& ArabicNumber)
{
     int h,th,t,o,number;
  string roman ;
  number = atoi (ArabicNumber.c_str());
  int number2 = 0;
  int test1 = 0;
  int test2 = 0;
  int test3 = 0;
        char *ones[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
        char *tens[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
        char *hundreds[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
     number2 = number / 1000;
for(int i = 1; i <= number2; i++)
cout << 'M' ;
     test1 = number % 1000;
            h = test1 / 100;
             test2 = test1 % 100; 
            t = test2 / 10 ;
             test3 = test2 % 10;
            o = test3 / 1;
            roman  = roman +  hundreds[h] + tens[t] + ones[o];
  cout << roman << endl ;

}

///////main////
using namespace std;
int main() {

    string num ;
     checkfile ck ;
          Convert cv;
 
    cout << "Enter the text file please ";
	cin >> num ;
        ck.FileCheck(num);
    if (ck.FileCheck(num) == 1)
        {
            cout << "File is open"<< endl ;
        }
       else
        {
            cout << "File not found" << endl;
       }
      fstream readFile("Test.txt");
       string Numeral ;       
while (getline(readFile,Numeral))
{
    if (Numeral != " "){
        ck.is_number(Numeral);  
    }   
    if (ck.is_number(Numeral)){     
        cv.ConvertToRoman(Numeral);
    }
    else {
        cout << "it has alphabets" << endl;
    }
}
      }



The probem i am facing is when my text file has only one line of information inside it....

123

THe program works fine. It is able to check if the string is just all numbers or has some alphabets. After checking, it will the pass the input into the other function to convert it to Roman Numeral. Thus, the output will be....

CXX111

which is correct. But when my text file has data in this way...


122
123

For some reason, it will not work fine. In fact, the output will be...

it has alphabets
CXX111

From the input, you realize that the program seems to read the first input wrongly and only read the 2nd one properly.

To check for mistakes, i took out the function to check for integers and just run it throught the function to convert the input into Roman numerals. In this case. the program worked and i get the correct output which is...

CXX11
CXX111

So i guess the problem might lie in the way the checking for integer function pass back the input into the main and into the other functions. Im very sure the functions are working properly. So i guess it has something to do with the way the information is passed? If anybody has any idea bout this, please tell me how i can solve this issue.






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int checkfile::FileCheck(string& num) 
{ 
string num2 ; 
int num3 ; 
num2 = num + ".txt"; 
 
const char* pszConstString = num2.c_str (); 
 
 
fstream checkfile( pszConstString) ; 
 
if (checkfile.is_open()) 
{ 
num = '1'; 
} 
else 
{ 
num = '0'; 
 
} 
 
}  


Sorry, i missed posting that. this is my function for the checkfile::FileCheck.
hello, i am doing this as an assignment too. can u post your latest codes please
Topic archived. No new replies allowed.