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....

#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.

///function to check if file exist////;
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.

Dude, you may not want to accept it, but your errors here are the same as your errors at DIC.

Edited 6 Years Ago by r.stiltskin: n/a

This article has been dead for over six months. Start a new discussion instead.