Question

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;
}
Was it helpful?

Solution

The functions you showed are wrong. Member function find of class std::string does not return a bool value. It returns an object of type std::string::size_type So for example the first function could look the following way

int formatStringNumerator( const string &rational )
{
    string tmp( rational, 0, rational.find("/") );

    tmp.erase( 0, tmp.find_first_not_of( "(" ) );

    return ( tmp.empty() ? 0 : std::stoi( tmp ) );
}

Here is an example of using the function

#include <iostream>
#include <string>
using namespace std;

int formatStringNumerator( const string &rational )
{
    string tmp( rational, 0, rational.find("/") );

    tmp.erase( 0, tmp.find_first_not_of( "(" ) );

    return ( tmp.empty() ? 0 : std::stoi( tmp ) );
}

int main() 
{
    std::string s( "((((12/15" );

    std::cout << formatStringNumerator( s ) << std::endl;

    std::string t( "((((12" );

    std::cout << formatStringNumerator( t ) << std::endl;

    std::string u( "((((" );

    std::cout << formatStringNumerator( u ) << std::endl;
    return 0;
}

The output is

12
12
0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top