LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2010, 11:02 AM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Checking of Input in Roman Program


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


Code:
#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.
 
Old 04-11-2010, 11:10 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
CXX111---that is not correct for Roman numerals

Should be:
CXXIII
 
Old 04-11-2010, 12:52 PM   #3
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
sorry i meant III.
 
Old 04-11-2010, 12:56 PM   #4
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
Quote:
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.
 
Old 04-11-2010, 01:06 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
How did you get that to compile is what I wonder?
Even with the function added (which I only saw after I
posted this post).

Last edited by Tinkster; 04-11-2010 at 01:08 PM.
 
Old 04-11-2010, 03:04 PM   #6
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Original Poster
Rep: Reputation: 15
There will errors but it will compile and run. I am using Netbeans 6.8.
 
Old 04-11-2010, 03:35 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
No it won't because checkfile::FileCheck isn't known to the class
in the state it's in above.

I added the missing declaration here, it compiles with warnings,
and runs. As expected, may I add (if we disregard the attempt to
read a file-name and use the hard-coded Test.txt).



Cheers,
Tink
 
Old 04-11-2010, 07:28 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Instead of answers, I actually have 2 questions (as this is (I think) the third time I have seen this similar code):

1. And this one is perhaps personal, but as you are learning this language, have you not been told about formatting?
I am not trying to be harsh, but I have notice that others have commented also about how difficult you code is to read / follow
due to lack of indentation or misaligned braces (small example)

Quote:
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';

}

}
Compared to:
Code:
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';
    }
}
Now you can easily line up your braces to know which test / loop is being involved with the necessary lines
and by indenting the statements inside the braces it is clearer as to what is happening.

2. Why do you process the same function twice throughout the program:

Code:
ck.FileCheck(num);
if (ck.FileCheck(num) == 1)

//and

if (Numeral != " "){
    ck.is_number(Numeral);  
}   
if (ck.is_number(Numeral)){
Both the call to FileCheck and is_number are called twice. The first is superfluous as you are not assigning the output
to anything.

I hope some of this helps, again you can take query 1 or not but the second is worth noting.
 
Old 04-12-2010, 03:42 AM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I've not read your code but I have a question for you...

Does your first line include a return character and if so is it being checked?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking input is Numerical gregarion Programming 24 01-25-2010 01:57 PM
Checking Input gregarion Programming 6 01-14-2010 08:04 PM
User input into Bash scripts and checking validity of user input?? helptonewbie Programming 8 07-07-2008 06:40 PM
Repeatedly checking for input kamransoomro84 Programming 16 05-26-2004 03:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration