ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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).
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.