I need help figuring out what\'s wrong with this C++ code! Please let me know as
ID: 3848576 • Letter: I
Question
I need help figuring out what's wrong with this C++ code! Please let me know asap.
//
// main.cpp
// A1
//
// Created by unknown
// ONLY 4 STEPS
#include
#include
#include
#include
#include
#include
using namespace std;
/* Char class */
class Char //define complex data type, Char...models the primitive type char
{
char value; //declare a char under value (private is assumed)
public:
Char() { this->value = ''; }
//Default constructor that sets the data section of the class to null (binary 0)
Char(char c) { this->value = c; }
//Overloaded constructor that takes a primitive char as an argument and sets data section of the class to the argument
Char(int c) { this->value = (char)c; }
//Overloaded constructor that takes a primitive int as parameter and sets data sect of class as character from argument
Char(const Char &c) { this->value = c.value; }
//Overloaded constructor that takes complex Char type as a parameter and sets data sect of class with that of argument
Char(string s) { this->value = s[0]; }
//Overloaded Constructor that takes string type as a parameter and sets data sect of class to first char in the string
char getValue() { return this->value;}
//returns data sect of class
void equals(const Char &c) { this->value = c.value; }
//mutator sets the data section to the data section of the argument
void equals(char c) { this->value = c; }
//mutator sets the data section to the primitive argument
void equals(int);
//mutator prototype
char toChar() const { return this->value; }
//accessor returns the data section of the class as a char
int toInt() const { return (int)this->value; }
//accessor returns the data section of the class as an int
string toString() { return to_string(this->value); }
//accessor returns the data section of the class as a string
string toHexString();
//accessor prototype
string operator+(char c);
//accessor prototype
string operator + (const Char &c);
//accessor prototype
};
void Char::equals(int c)
{
if (c<32 || c>127)
{
cout<<"Invalid Character";
}
this->value = (char)c;
}
//Concatenates data section of the class with the data section of the parameter and returns the two characters as a string
string Char::toHexString()
{
stringstream output; //create local object of string stream
output< return output.str(); //finally return string
}
//Returns the data section of the class as a hexadecimal valued string
string Char::operator+(char c)
{
value + to_string(c);
string s;
s=(char)c;
return value + s;
}
//Concatenates data section of the class with the parameter and returns the two characters as a string
string Char::operator + (const Char &c)
{
value + to_string(c.value);
string s;
s=(char)c.value;
return value + s;
}
//Concatenates data section of the class with data section of the parameter and returns the two characters as a string
/* BigDecimal class */
class BigDecimal //define complex data type, BigDecimal
{
vector chars; //declare a vector
public:
BigDecimal();
//default constructor prototype
BigDecimal(string value);
//overloaded constructor prototype
BigDecimal(const BigDecimal &value)
{
vectorvalList = value.chars;
for (int i=0; i {
chars.push_back(valList.at(i));
}
}
//overloaded constructor adds chars to container
void equals(char ch);
//mutator prototype
void equals(string value);
//mutator prototype
vectorgetValue() {return this->chars;}
//returns from data section
BigDecimal add(BigDecimal)
{
double num1 = this->wholeNumber()+this->fraction();
double num2 = wholeNumber()+fraction();
return to_string(num1+num2);
}
//mutator adds the values together and returns the result as a Big Decimal
BigDecimal sub(BigDecimal)
{
double num1 = this->wholeNumber()+this->fraction();
double num2 = wholeNumber()+fraction();
return to_string(num1-num2);
}
//mutator subtracts the two values and returns the result as a BigDecimal
int wholeNumber();
//method proto
double fraction();
//method proto
double toDouble() { return this->wholeNumber()+this->fraction(); }
//accessor returns the value stored in the container as a double
string toString() { return to_string(toDouble()); }
//accessor returns the value store in the container as a string
Char at(int index) { return chars.at(index); }
//accessor returns the value at the particular index as a Char
private:
bool isNumber(Char ch)
{
char c = ch.toChar();
return (c>='0' && c<='9');
}
//check if the number has a decimal point
bool containsDecimal()
{
bool contains = false;
for (Char c : this->chars)
{
if('.'==c.toChar())
{
contains = true;
}
}
return contains;
}
//validate if the string contains a valid number
bool validateString(string value)
{
bool flag = false;
for(int i=0;value[i]!='';i++) //check each character from beginning till the end
{
if(value[i] == ' ') //if the space is found, just continue the process
{
continue;
}
else
{
if((value[i] >= 'A' && value[i] <= 'Z') || (value[i] >= 'a' && value[i] <= 'z')) //if any character is found in string, it will check whether the character is alphabet or not
flag = true; //accepts the alphabet in lower case and upper case
else
{
flag = false;
//if number is found or any special character is found, it will break the loop by sending false
break;
}
}
}
return flag;
}
};
BigDecimal::BigDecimal()
{
chars.push_back('0');
chars.push_back('.');
chars.push_back('0');
}
//default constructor sets contianer to three Char objects that contain the values '0' '.' '0'
BigDecimal::BigDecimal(string value)
{
// in case of #'s like .76
if (value[0]=='.')
{
chars.push_back('0');
}
bool validateString(string value);
for (int i=0; i {
chars.push_back(value[i]);
}
}
//overloaded constructor parses the string taking each digit, putting it into a new Char and adding the Char to the vector
void BigDecimal::equals(char ch)
{
BigDecimal number = to_string(ch);
this->chars = number.chars;
}
//a char that contains a digit
void BigDecimal::equals(string value)
{
bool validateString(string value);
BigDecimal number = value;
this->chars = number.chars;
}
//does the same as the overloaded constructor that takes a string
int BigDecimal::wholeNumber()
{
vectorsubChars;
if (containsDecimal())
{
int index = 0;
for (Char c : this->chars)
{
if ('.'==c.toChar())
{
break;
}
index++;
}
vectorsubChars (chars.begin()+0, chars.begin()+index);
}
else
{
subChars = this->chars;
}
string s="";
for (Char c : subChars)
{
s += c.toString();
}
return stoi(s);
}
//returns only the whole number portion of the decimal number as an int
double BigDecimal::fraction()
{
string fraction;
vectorsubChars;
if (containsDecimal())
{
int index = 0;
for (Char c : this->chars)
{
if ('.'==c.toChar())
{
break;
}
index++;
}
vectorsubChars (chars.begin()+index+1, chars.begin()+this->chars.size());
fraction = "0.";
for (Char c : subChars)
{
fraction += c.toString();
}
}
else
{
//If no fraction part, return default fraction = 0.0
fraction = "0.0";
}
return stod(fraction);
}
//returns the fractional portion of the number as double
/* Exception Handling */
class CharException:public exception
{
private:
static constexpr long long serialVersionUID = 1LL;
public:
CharException(string message)
{
}
};
class BigDecimalException:public CharException
{
private:
static constexpr long long serialVersionUID = 1LL;
public:
BigDecimalException(string message);
};
BigDecimalException::BigDecimalException(string message):CharException(message)
{
}
/* Testing */
int main()
{
Char ch = 'A';
Char c = 'B';
cout << ch.operator +(c) << endl;
cout << ch.toChar() << " In Hex: " << ch.toHexString() << endl;
cout << ch.toChar() << " In Int: " << ch.toInt() << endl;
try
{
ch.equals(140);
cout << ch.toChar() << endl;
}
catch(CharException ce)
{
cout << ce.what() << endl;
}
Char x = 34;
cout << x.toString() << endl;
/*
- BigDecimal Test Class
- Reads numbers from a file
- Outputs 2 files - 1 file contains whole part and 2nd file contains fraction part
*/
vectornumbersList;
string line;
ifstream myfile ("file.txt");
while(getline(myfile,line))
{
try
{
BigDecimal b;
numbersList.push_back(b);
} catch (BigDecimalException bde)
{
cout<< bde.what() << endl;
}
}
vectorwholeList;
vectorfracList;
for (BigDecimal bd : numbersList)
{
wholeList.push_back(bd.wholeNumber());
fracList.push_back(bd.fraction());
}
fstream file, file1;
//opens the file if exist
file.open("wholeNumber.txt", fstream::in | fstream::out | fstream::app);
file1.open("fraction.txt", fstream::in | fstream::out | fstream::app);
wholeList[20];
fracList[20];
//creates new file if the file does not exist
if(!file.is_open()){
ifstream file("wholeNumber.txt");
}
if(!file1.is_open()){
ifstream file("fraction.txt");
}
//Write to the file
ofstream os("wholeNumber.txt");
if (!os) {
std::cerr<<"Error writing to ..."< } else {
for(int i=0;i<20;i++)
os << wholeList[i]<<" ";
}
ofstream os1("fraction.txt");
if (!os1) {
std::cerr<<"Error writing to ..."< } else {
for(int i=0;i<20;i++)
os1 << fracList[i]<<" ";
}
return 0;
}
//
// main.cpp
// A1
//
// Created by unknown
// ONLY 4 STEPS
#include
#include
#include
#include
#include
#include
using namespace std;
/* Char class */
class Char //define complex data type, Char...models the primitive type char
{
char value; //declare a char under value (private is assumed)
public:
Char() { this->value = ''; }
//Default constructor that sets the data section of the class to null (binary 0)
Char(char c) { this->value = c; }
//Overloaded constructor that takes a primitive char as an argument and sets data section of the class to the argument
Char(int c) { this->value = (char)c; }
//Overloaded constructor that takes a primitive int as parameter and sets data sect of class as character from argument
Char(const Char &c) { this->value = c.value; }
//Overloaded constructor that takes complex Char type as a parameter and sets data sect of class with that of argument
Char(string s) { this->value = s[0]; }
//Overloaded Constructor that takes string type as a parameter and sets data sect of class to first char in the string
char getValue() { return this->value;}
//returns data sect of class
void equals(const Char &c) { this->value = c.value; }
//mutator sets the data section to the data section of the argument
void equals(char c) { this->value = c; }
//mutator sets the data section to the primitive argument
void equals(int);
//mutator prototype
char toChar() const { return this->value; }
//accessor returns the data section of the class as a char
int toInt() const { return (int)this->value; }
//accessor returns the data section of the class as an int
string toString() { return to_string(this->value); }
//accessor returns the data section of the class as a string
string toHexString();
//accessor prototype
string operator+(char c);
//accessor prototype
string operator + (const Char &c);
//accessor prototype
};
void Char::equals(int c)
{
if (c<32 || c>127)
{
cout<<"Invalid Character";
}
this->value = (char)c;
}
//Concatenates data section of the class with the data section of the parameter and returns the two characters as a string
string Char::toHexString()
{
stringstream output; //create local object of string stream
output< return output.str(); //finally return string
}
//Returns the data section of the class as a hexadecimal valued string
string Char::operator+(char c)
{
value + to_string(c);
string s;
s=(char)c;
return value + s;
}
//Concatenates data section of the class with the parameter and returns the two characters as a string
string Char::operator + (const Char &c)
{
value + to_string(c.value);
string s;
s=(char)c.value;
return value + s;
}
//Concatenates data section of the class with data section of the parameter and returns the two characters as a string
/* BigDecimal class */
class BigDecimal //define complex data type, BigDecimal
{
vector chars; //declare a vector
public:
BigDecimal();
//default constructor prototype
BigDecimal(string value);
//overloaded constructor prototype
BigDecimal(const BigDecimal &value)
{
vectorvalList = value.chars;
for (int i=0; i {
chars.push_back(valList.at(i));
}
}
//overloaded constructor adds chars to container
void equals(char ch);
//mutator prototype
void equals(string value);
//mutator prototype
vectorgetValue() {return this->chars;}
//returns from data section
BigDecimal add(BigDecimal)
{
double num1 = this->wholeNumber()+this->fraction();
double num2 = wholeNumber()+fraction();
return to_string(num1+num2);
}
//mutator adds the values together and returns the result as a Big Decimal
BigDecimal sub(BigDecimal)
{
double num1 = this->wholeNumber()+this->fraction();
double num2 = wholeNumber()+fraction();
return to_string(num1-num2);
}
//mutator subtracts the two values and returns the result as a BigDecimal
int wholeNumber();
//method proto
double fraction();
//method proto
double toDouble() { return this->wholeNumber()+this->fraction(); }
//accessor returns the value stored in the container as a double
string toString() { return to_string(toDouble()); }
//accessor returns the value store in the container as a string
Char at(int index) { return chars.at(index); }
//accessor returns the value at the particular index as a Char
private:
bool isNumber(Char ch)
{
char c = ch.toChar();
return (c>='0' && c<='9');
}
//check if the number has a decimal point
bool containsDecimal()
{
bool contains = false;
for (Char c : this->chars)
{
if('.'==c.toChar())
{
contains = true;
}
}
return contains;
}
//validate if the string contains a valid number
bool validateString(string value)
{
bool flag = false;
for(int i=0;value[i]!='';i++) //check each character from beginning till the end
{
if(value[i] == ' ') //if the space is found, just continue the process
{
continue;
}
else
{
if((value[i] >= 'A' && value[i] <= 'Z') || (value[i] >= 'a' && value[i] <= 'z')) //if any character is found in string, it will check whether the character is alphabet or not
flag = true; //accepts the alphabet in lower case and upper case
else
{
flag = false;
//if number is found or any special character is found, it will break the loop by sending false
break;
}
}
}
return flag;
}
};
BigDecimal::BigDecimal()
{
chars.push_back('0');
chars.push_back('.');
chars.push_back('0');
}
//default constructor sets contianer to three Char objects that contain the values '0' '.' '0'
BigDecimal::BigDecimal(string value)
{
// in case of #'s like .76
if (value[0]=='.')
{
chars.push_back('0');
}
bool validateString(string value);
for (int i=0; i {
chars.push_back(value[i]);
}
}
//overloaded constructor parses the string taking each digit, putting it into a new Char and adding the Char to the vector
void BigDecimal::equals(char ch)
{
BigDecimal number = to_string(ch);
this->chars = number.chars;
}
//a char that contains a digit
void BigDecimal::equals(string value)
{
bool validateString(string value);
BigDecimal number = value;
this->chars = number.chars;
}
//does the same as the overloaded constructor that takes a string
int BigDecimal::wholeNumber()
{
vectorsubChars;
if (containsDecimal())
{
int index = 0;
for (Char c : this->chars)
{
if ('.'==c.toChar())
{
break;
}
index++;
}
vectorsubChars (chars.begin()+0, chars.begin()+index);
}
else
{
subChars = this->chars;
}
string s="";
for (Char c : subChars)
{
s += c.toString();
}
return stoi(s);
}
//returns only the whole number portion of the decimal number as an int
double BigDecimal::fraction()
{
string fraction;
vectorsubChars;
if (containsDecimal())
{
int index = 0;
for (Char c : this->chars)
{
if ('.'==c.toChar())
{
break;
}
index++;
}
vectorsubChars (chars.begin()+index+1, chars.begin()+this->chars.size());
fraction = "0.";
for (Char c : subChars)
{
fraction += c.toString();
}
}
else
{
//If no fraction part, return default fraction = 0.0
fraction = "0.0";
}
return stod(fraction);
}
//returns the fractional portion of the number as double
/* Exception Handling */
class CharException:public exception
{
private:
static constexpr long long serialVersionUID = 1LL;
public:
CharException(string message)
{
}
};
class BigDecimalException:public CharException
{
private:
static constexpr long long serialVersionUID = 1LL;
public:
BigDecimalException(string message);
};
BigDecimalException::BigDecimalException(string message):CharException(message)
{
}
/* Testing */
int main()
{
Char ch = 'A';
Char c = 'B';
cout << ch.operator +(c) << endl;
cout << ch.toChar() << " In Hex: " << ch.toHexString() << endl;
cout << ch.toChar() << " In Int: " << ch.toInt() << endl;
try
{
ch.equals(140);
cout << ch.toChar() << endl;
}
catch(CharException ce)
{
cout << ce.what() << endl;
}
Char x = 34;
cout << x.toString() << endl;
/*
- BigDecimal Test Class
- Reads numbers from a file
- Outputs 2 files - 1 file contains whole part and 2nd file contains fraction part
*/
vectornumbersList;
string line;
ifstream myfile ("file.txt");
while(getline(myfile,line))
{
try
{
BigDecimal b;
numbersList.push_back(b);
} catch (BigDecimalException bde)
{
cout<< bde.what() << endl;
}
}
vectorwholeList;
vectorfracList;
for (BigDecimal bd : numbersList)
{
wholeList.push_back(bd.wholeNumber());
fracList.push_back(bd.fraction());
}
fstream file, file1;
//opens the file if exist
file.open("wholeNumber.txt", fstream::in | fstream::out | fstream::app);
file1.open("fraction.txt", fstream::in | fstream::out | fstream::app);
wholeList[20];
fracList[20];
//creates new file if the file does not exist
if(!file.is_open()){
ifstream file("wholeNumber.txt");
}
if(!file1.is_open()){
ifstream file("fraction.txt");
}
//Write to the file
ofstream os("wholeNumber.txt");
if (!os) {
std::cerr<<"Error writing to ..."< } else {
for(int i=0;i<20;i++)
os << wholeList[i]<<" ";
}
ofstream os1("fraction.txt");
if (!os1) {
std::cerr<<"Error writing to ..."< } else {
for(int i=0;i<20;i++)
os1 << fracList[i]<<" ";
}
return 0;
}
Explanation / Answer
Note: In the above code never used a method vectorsubChars.
Program
//
// main.cpp
// A1
//
// Created by unknown
// ONLY 4 STEPS
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
void vectorsubChars();
/* Char class */
class Char //define complex data type, Char...models the primitive type char
{
char value; //declare a char under value (private is assumed)
public:
Char() { this->value = ''; }
//Default constructor that sets the data section of the class to null (binary 0)
Char(char c) { this->value = c; }
//Overloaded constructor that takes a primitive char as an argument and sets data section of the class to the argument
Char(int c) { this->value = (char)c; }
//Overloaded constructor that takes a primitive int as parameter and sets data sect of class as character from argument
Char(const Char &c) { this->value = c.value; }
//Overloaded constructor that takes complex Char type as a parameter and sets data sect of class with that of argument
Char(string s) { this->value = s[0]; }
//Overloaded Constructor that takes string type as a parameter and sets data sect of class to first char in the string
char getValue() { return this->value;}
//returns data sect of class
void equals(const Char &c) { this->value = c.value; }
//mutator sets the data section to the data section of the argument
void equals(char c) { this->value = c; }
//mutator sets the data section to the primitive argument
void equals(int);
//mutator prototype
char toChar() const { return this->value; }
//accessor returns the data section of the class as a char
int toInt() const { return (int)this->value; }
//accessor returns the data section of the class as an int
string toString() { return to_string(this->value); }
//accessor returns the data section of the class as a string
string toHexString();
//accessor prototype
string operator+(char c);
//accessor prototype
string operator + (const Char &c);
//accessor prototype
};
void Char::equals(int c)
{
if (c<32 || c>127)
{
cout<<"Invalid Character";
}
this->value = (char)c;
}
//Concatenates data section of the class with the data section of the parameter and returns the two characters as a string
string Char::toHexString()
{
stringstream output; //create local object of string stream
return output.str(); //finally return string
}
//Returns the data section of the class as a hexadecimal valued string
string Char::operator+(char c)
{
value + to_string(c);
string s;
s=(char)c;
return value + s;
}
//Concatenates data section of the class with the parameter and returns the two characters as a string
string Char::operator + (const Char &c)
{
value + to_string(c.value);
string s;
s=(char)c.value;
return value + s;
}
//Concatenates data section of the class with data section of the parameter and returns the two characters as a string
/* BigDecimal class */
class BigDecimal //define complex data type, BigDecimal
{
vector<int> chars; //declare a vector
vector<int> vectorvalList;
vector<int> valList;
public:
BigDecimal();
//default constructor prototype
BigDecimal(string value);
//overloaded constructor prototype
BigDecimal(const BigDecimal &value)
{
vectorvalList = value.chars;
for (int i=0;i<vectorvalList.size();i++)
{
chars.push_back(valList.at(i));
}
}
//overloaded constructor adds chars to container
void equals(char ch);
//mutator prototype
void equals(string value);
//mutator prototype
vector<int> vectorgetValue()
{
return this->chars;
}
//returns from data section
BigDecimal add(BigDecimal)
{
double num1 = this->wholeNumber()+this->fraction();
double num2 = wholeNumber()+fraction();
return to_string(num1+num2);
}
//mutator adds the values together and returns the result as a Big Decimal
BigDecimal sub(BigDecimal)
{
double num1 = this->wholeNumber()+this->fraction();
double num2 = wholeNumber()+fraction();
return to_string(num1-num2);
}
//mutator subtracts the two values and returns the result as a BigDecimal
int wholeNumber();
//method proto
double fraction();
//method proto
double toDouble() { return this->wholeNumber()+this->fraction(); }
//accessor returns the value stored in the container as a double
string toString() { return to_string(toDouble()); }
//accessor returns the value store in the container as a string
Char at(int index) { return chars.at(index); }
//accessor returns the value at the particular index as a Char
private:
bool isNumber(Char ch)
{
char c = ch.toChar();
return (c>='0' && c<='9');
}
//check if the number has a decimal point
bool containsDecimal()
{
bool contains = false;
for (Char c : this->chars)
{
if('.'==c.toChar())
{
contains = true;
}
}
return contains;
}
//validate if the string contains a valid number
bool validateString(string value)
{
bool flag = false;
for(int i=0;value[i]!='';i++) //check each character from beginning till the end
{
if(value[i] == ' ') //if the space is found, just continue the process
{
continue;
}
else
{
if((value[i] >= 'A' && value[i] <= 'Z') || (value[i] >= 'a' && value[i] <= 'z')) //if any character is found in string, it will check whether the character is alphabet or not
flag = true; //accepts the alphabet in lower case and upper case
else
{
flag = false;
//if number is found or any special character is found, it will break the loop by sending false
break;
}
}
}
return flag;
}
};
BigDecimal::BigDecimal()
{
chars.push_back('0');
chars.push_back('.');
chars.push_back('0');
}
//default constructor sets contianer to three Char objects that contain the values '0' '.' '0'
BigDecimal::BigDecimal(string value)
{
// in case of #'s like .76
if (value[0]=='.')
{
chars.push_back('0');
}
bool validateString(string value);
for (int i=0;i<10;i++)
{
chars.push_back(value[i]);
}
}
//overloaded constructor parses the string taking each digit, putting it into a new Char and adding the Char to the vector
void BigDecimal::equals(char ch)
{
BigDecimal number = to_string(ch);
this->chars = number.chars;
}
//a char that contains a digit
void BigDecimal::equals(string value)
{
bool validateString(string value);
BigDecimal number = value;
this->chars = number.chars;
}
//does the same as the overloaded constructor that takes a string
int BigDecimal::wholeNumber()
{
//vectorsubChars;
vector<int> subChars;
if (containsDecimal())
{
int index = 0;
for (Char c : this->chars)
{
if ('.'==c.toChar())
{
break;
}
index++;
}
// vectorsubChars (chars.begin()+0, chars.begin()+index);
}
else
{
subChars = this->chars;
}
string s="";
for (Char c : subChars)
{
s += c.toString();
}
return stoi(s);
}
//returns only the whole number portion of the decimal number as an int
double BigDecimal::fraction()
{
string fraction;
//vectorsubChars;
vector<int> subChars;
if (containsDecimal())
{
int index = 0;
for (Char c : this->chars)
{
if ('.'==c.toChar())
{
break;
}
index++;
}
// vectorsubChars (chars.begin()+index+1, chars.begin()+this->chars.size()); // Never Used a function
fraction = "0.";
for (Char c : subChars)
{
fraction += c.toString();
}
}
else
{
//If no fraction part, return default fraction = 0.0
fraction = "0.0";
}
return stod(fraction);
}
//returns the fractional portion of the number as double
/* Exception Handling */
class CharException:public exception
{
private:
static constexpr long long serialVersionUID = 1LL;
public:
CharException(string message)
{
}
};
class BigDecimalException:public CharException
{
private:
static constexpr long long serialVersionUID = 1LL;
public:
BigDecimalException(string message);
};
BigDecimalException::BigDecimalException(string message):CharException(message)
{
}
/* Testing */
int main()
{
Char ch = 'A';
Char c = 'B';
cout << ch.operator +(c) << endl;
cout << ch.toChar() << " In Hex: " << ch.toHexString() << endl;
cout << ch.toChar() << " In Int: " << ch.toInt() << endl;
try
{
ch.equals(140);
cout << ch.toChar() << endl;
}
catch(CharException ce)
{
cout << ce.what() << endl;
}
Char x = 34;
cout << x.toString() << endl;
/*
- BigDecimal Test Class
- Reads numbers from a file
- Outputs 2 files - 1 file contains whole part and 2nd file contains fraction part
*/
vector<BigDecimal> numbersList;
//vectornumbersList;
string line;
ifstream myfile ("file.txt");
while(getline(myfile,line))
{
try
{
BigDecimal b;
numbersList.push_back(b);
} catch (BigDecimalException bde)
{
cout<< bde.what() << endl;
}
}
// vectorwholeList;
// vectorfracList;
vector<int> wholeList;
vector<double> fracList;
for (BigDecimal bd : numbersList)
{
wholeList.push_back(bd.wholeNumber());
fracList.push_back(bd.fraction());
}
fstream file, file1;
//opens the file if exist
file.open("wholeNumber.txt", fstream::in | fstream::out | fstream::app);
file1.open("fraction.txt", fstream::in | fstream::out | fstream::app);
//creates new file if the file does not exist
if(!file.is_open()){
ifstream file("wholeNumber.txt");
}
if(!file1.is_open()){
ifstream file("fraction.txt");
}
//Write to the file
ofstream os("wholeNumber.txt");
if (!os) {
std::cerr<<"Error writing to ...";
} else {
for(int i=0;i<20;i++)
os << wholeList[i]<<" ";
}
ofstream os1("fraction.txt");
if (!os1) {
std::cerr<<"Error writing to ...";
} else {
for(int i=0;i<20;i++)
os1 << fracList[i]<<" ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.