Hi Guys, I need your help here, I'm working on a assignment, and had done up the source code, but I'm getting wrong results:

Summarised below:

I screenshot the output:

[IMG]http://i4.photobucket.com/albums/y101/clement_twk/Output.png[/IMG]

I noticed under the output on these 2 lines are wrong:

Addition A + B = 100+103i+106j+109k
I supposed to get 4+7i+10j+13k

Prefix ++A = 52+54i+56j+58k
I supposed to get 4+6i+8j+10k

The rest are correct.

Attached the below source code:

#include <iostream>
#include <string>

using namespace std;

class Quarterian
{
friend istream& operator>>(istream&, Quarterian&);
friend ostream& operator<<(ostream&, Quarterian&);

public:
    Quarterian operator+(Quarterian);
    Quarterian operator-(Quarterian);
    bool operator==(Quarterian&);
    bool operator>(Quarterian&);
    Quarterian& operator++();
    
private:
    int A, B, C, D, E;
};

istream& operator>>(istream& in, Quarterian& num)
{
    string input;
    in >> input; 
    
    num.A = input[0];
    num.B = input[2];
    num.C = input[5];
    num.D = input[8];
    
    return in;
}

ostream& operator<<(ostream& out, Quarterian& num)
{  
    out << num.A << "+" << num.B << "i" << "+" << num.C << "j" << "+" << num.D << "k";
    return out;
}


Quarterian Quarterian::operator+(Quarterian t)
{
    Quarterian temp_result;
    temp_result.A = A + t.A;
    temp_result.B = B + t.B;
    temp_result.C = C + t.C;
    temp_result.D = D + t.D;
    return temp_result;       
}

Quarterian Quarterian::operator-(Quarterian t)
{
    Quarterian temp_result;
    temp_result.A = A - t.A;
    temp_result.B = B - t.B;
    temp_result.C = C - t.C;
    temp_result.D = D - t.D;
    return temp_result;      
}

bool Quarterian::operator==(Quarterian& num)
{
    bool truth = false;
    if(A == num.A && B == num.B && C == num.C && D == num.D)
    {
        truth = true;
    }
    return truth;
}

bool Quarterian::operator>(Quarterian& num)
{
    bool truth = false;
    if(A > num.A && B > num.B && C > num.C && D > num.D)
    {
        truth = true;
    }
    return truth;
}

Quarterian& Quarterian::operator++()
{
    A++;
    B++;
    C++;
    D++;
    
    return *this;
}

int main()
{
    Quarterian A, B, C, D, E;
    cout << "Enter first quaternion, format a + bi + cj + dk: ";
    cin >> A;
    cout << "Enter second quaternion, format a + bi + cj + dk: ";
    cin >> B;
    
    C = A + B;
    D = A - B;
    cout << "Addition A + B = " << C << endl;
    cout << "Subtraction A - B = " << D << endl;
    if (A == B)
    cout << "A = B: True" << endl;
    else
    cout << "A = B: False" << endl;
    if (A > B)
    cout << "A > B: True" << endl;
    else
    cout << "A > B: False" << endl;
    E = ++A;
    cout << "Prefix ++A = " << E << endl;
    system("pause");
    return 0;    
    
}

Your input operator >> is not correctly parsing the input data.

To start with what if someone types a space before the equation, accessing the characters by direct offset is almost bound to cause an input error at some time. What if one of the numbers has more than a single digit.

Also you are storing the character value num.A = input[0] does not set num.A to the value 3 but rather sets it to the value '3' or 51.

You need to parse the input string, probably the easiest way would be to use a istringstream the following gets you started.

istringstream ss(input);

ss >> num.A;

Finally cin >> input; if a space is put in the equation then this will only get the first part of the equation because space is used as a field separator. You want to get the entire line with getline(cin, input);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.