Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Should I learn LISP?

Name: Faggot !tsGpSwX8mo 2012-01-01 16:04

Is it worth it?

Name: Anonymous 2012-01-01 16:11

C++ newfaf here. The output from line 70 should be 2x^3, but it's outputting 2686708x^3. When term is constructed, *coeff is printed as 2, but when getWritten() is called, *coeff is printed as 2686708. What's going on?

#include <iostream>
#include <sstream>

using namespace std;

string parseInt(int toParse);

class Term
{
    public:
        Term(int c, int e);
        ~Term();
        string getWritten();
        int *coeff, *exp;
};

Term::Term(int c, int e)
{
    coeff = &c;
    exp = &e;
}

Term::~Term()
{
    delete coeff;
    delete exp;
}

string Term::getWritten()
{
    if(*coeff == 0)
    {
        return 0;
    }
    else if(*coeff == 1)
    {
        if(*exp == 0)
        {
            return parseInt(*coeff);
        }
        else if(*exp == 1)
        {
            return "x";
        }
        else
        {
            return "x^" + parseInt(*exp);
        }
    }
    else
    {
        if(*exp == 0)
        {
            return parseInt(*coeff);
        }
        else if(*exp == 1)
        {
            return (parseInt(*coeff) + "x");
        }
        else
        {
            return (parseInt(*coeff) + "x^" + parseInt(*exp));
        }
    }
}

int main()
{
    Term test(2, 3);
    cout << test.getWritten() << endl;
    return 0;
}

string parseInt(int toParse)
{
    stringstream stream;
    stream << toParse;

    if(!stream.fail())
        return stream.str();
    else
        return "";
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List