I'm writing a rational class, where the following must be acceptable forms of input:
3/4, 1.4545, (7/8, (7/8), 7/8), or even ((((((5/4))), where my code should rewrite it as 5/4 for the constructor to accept.
At this point I am passing a string parameter into a constructor which calls on formatStringNumerator() and formatStringDenominator() respectively;
I haven't gotten to the decimals yet, but so far, inputs with a parenthesis to the left set the numerator value to 0 somehow. I believe the fault to be the "(" before the numbers. I've commented out my attempt at removing the parentheses to the left, but they just won't go away. I've attempted to use substr() as an alternative, but just got errors when I did. Also, when I print out the find() value I get some large number, [18446744073709551615] when the parameters cannot be found? I was under the impression it should return -1(false?) if not found.
At the first comment, the erase() ought to be deleting the first char in the string, being the parenthesis, but it just does not delete itself, and the conversion from string to int gets messy, defaulting it to 0 like I mentioned before?
int formatStringNumerator(string &rational){
string tmp;
int numerator;
// if(rational.find("(") != false){
// rational.erase(0, 1);
// } //This should have deleted an open parenthesis
if(rational.find("/") != false){
tmp = rational.substr(0, rational.find("/"));
cout << "\n" << tmp << endl; //cout to test if ran
} //Should assign numbers leading to the symbol("/") to tmp
istringstream is;
is.str(tmp); //Numerator should have int value with parenthesis removed
is >> numerator;
return numerator;
}
int formatStringDenominator(string &rational){
string tmp;
int denominator;
if(tmp.find("/") != false){
tmp = rational.substr((rational.find("/")+1));
} //assign value after symbol to tmp
// if(tmp.find(")") != false){
// tmp.erase(tmp.find(")")); //Successfully removes parenthesis
// cout << "\n" << tmp << endl; //Bad condition in if statement?
// } //cout to test if ran
istringstream is;
is.str(tmp);
is >> denominator;
return denominator;
}