friend ostream& operator <<, compiles and runs in Dev++ no output or error in Borland

R

Rock

I'm in the process of writing this program for complex numbers and I
use DevC++. My professor on the other hand compiles on Borland 5.5. So
I ocasionally save and run my work on Borland to see if it caught
anything, it's very picky... Anyway, the code below works on Dev, and
it compiles fine on Borland, but when I run it from borland, there is
no output, no error, it just skips right over the freind ostream call.
HELP! I'm new to this and so far it's been a very trying experience...
any help would be greatly appreciated. Rick



#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
using namespace std;

//Class for complex numbers.
class Complex
{
public:
friend Complex operator +(const Complex& left, const Complex&
right);
friend Complex operator -(const Complex& left, const Complex&
right);
friend Complex operator *(const Complex& left, const Complex&
right);
friend bool operator ==(const Complex& left, const Complex&
right);
//overloaded operators

Complex(char in1[], char in2[]);//inputs taken in as Cstrings
Complex(char in1[]);
Complex();

void get_first(char ansr1[]);//returns doubles of first string
void get_secon(char ansr2[]);//returns doubles of second string
void foil(double lr, double li, double rr, double ri);
//Performs multiplication of 2 imaginary numbers


friend ostream& operator <<(ostream& outs, const Complex&
number);

private:
char answer[20];
char l_sign;
double l_real;
double l_imaginary;
char r_sign;
double r_real;
double r_imaginary;
double real_result1;
double imaginary_result1;
double real_result2;
double imaginary_result2;
double real_result3;
double imaginary_result3;
};


int main()
{
system("cls");
char answer1[80], answer2[80];
int x;
do
{
cout << "\n" << "Enter a complex number in the form of a + bi: ";
cin.getline(answer1,80);
cout << "\n" << "Enter the second complex number (Enter for
default): ";
cin.getline(answer2,80);
if (answer2[0] == '\0')
{
break;
}
Complex start(answer1, answer2);
cout << "\n" << start;
return 0;
}while(x==1);

Complex start(answer1);
cout << "\n" << start;
return 0;


}

Complex operator +(const Complex& left, const Complex& right)
{
return(left + right);


}

Complex operator -(const Complex& left, const Complex& right)
{
return(left - right);
}

Complex operator *(const Complex& left, const Complex& right)
{

return(left * right);

}

bool operator ==(const Complex& left, const Complex& right)
{

return(left == right);
}

Complex::Complex(char in1[], char in2[])
{

get_first(in1);
get_secon(in2);

if (!strcmp(in1, in2))
{
strcpy(answer, "equal");
}
else
{
strcpy(answer, "not equal");
}
real_result1 = (l_real + r_real);
imaginary_result1 = (l_imaginary + r_imaginary);
real_result2 = (l_real - r_real);
imaginary_result2 = (l_imaginary - r_imaginary);
foil(l_real, l_imaginary, r_real, r_imaginary);
}

Complex::Complex(char in1[]) : r_real(5), r_imaginary(2), r_sign('+')
{
get_first(in1);
if (l_real == r_real && l_imaginary == r_imaginary)
{
strcpy(answer, "equal");
}
else
{
strcpy(answer, "not equal");
}
real_result1 = (l_real + r_real);
imaginary_result1 = (l_imaginary + r_imaginary);
real_result2 = (l_real - r_real);
imaginary_result2 = (l_imaginary - r_imaginary);
foil(l_real, l_imaginary, r_real, r_imaginary);
}

Complex::Complex()
{

}

void Complex::get_first(char ansr1[])
{
char ans2[80], ans3[2], ans4[80], hold[80];
int x=0, y=0;
while(ansr1[x] != '+' && ansr1[x] != '-' || x == 0)
{
ans2[y] = ansr1[x];
x = x+1;
y = y+1;
}
ans2[y] = '\0';
y=0;
ans3[y] = ansr1[x];
x = x+1;
y=y+1;
ans3[y] = '\0';
y=0;

while(ansr1[x] != 'i')
{
ans4[y] = ansr1[x];
x = x+1;
y = y+1;
}
ans4[y] = '\0';

l_real = atof(ans2);
strcat(ans3, ans4);
l_imaginary = atof(ans3);
l_sign = ans3[0];

}

void Complex::get_secon(char ansr2[])
{
char ans2[80], ans3[2], ans4[80], hold[80];
int x=0, y=0;
while(ansr2[x] != '+' && ansr2[x] != '-' || x == 0)
{
ans2[y] = ansr2[x];
x = x+1;
y = y+1;
}
ans2[y] = '\0';
y=0;
ans3[y] = ansr2[x];
x = x+1;
y=y+1;
ans3[y] = '\0';
y=0;

while(ansr2[x] != 'i')
{
ans4[y] = ansr2[x];
x = x+1;
y = y+1;
}
ans4[y] = '\0';

r_real = atof(ans2);
strcat(ans3, ans4);
r_imaginary = atof(ans3);
r_sign = ans3[0];

}

void Complex::foil(double lr, double li, double rr, double ri)
{
double hold1, hold2;
hold1 = (lr * rr) + ((li * ri)*-1);
hold2 = (li * rr) + (lr * ri);
real_result3 = hold1;
imaginary_result3 = hold2;


}


ostream& operator <<(ostream& outs, const Complex& number)
{

outs << "\n" << "Numbers started with: " << number.l_real <<
number.l_sign
<< abs(number.l_imaginary) << "i" << " and " << number.r_real
<< number.r_sign
<< abs(number.r_imaginary) << "i" << "." << "\n" << "\n" <<
"Added they equal: "
<< number.real_result1 << "+" << number.imaginary_result1 <<
"i" << endl
<< "Subtracted they equal: " << number.real_result2 << "+" <<
number.real_result2
<< "i" << endl << "Multiplied they equal: " <<
number.real_result3 << "+"
<< number.imaginary_result3 << "i" << endl << "They are " <<
number.answer << "."
<< "\n" << endl;



return outs;
}
 
E

Earl Purple

There are many issues with your code. Why does your Complex class have
so many members and things irrelevant to complex numbers? (By the way
did you know the standard already has a complex?)

One clear error in your code is here:

Rock wrote:
Complex operator +(const Complex& left, const Complex& right)
{
return(left + right);
}

that is simply recursing ad-infinitum.
 
R

Rock

It's a non-finished homework assignment, written by a 4 week old (in
C++ that is). When completed, the user will enter two imaginary
numbers in the fomat a+bi, and the program will add, subtract, multiply
and check for eqality of the two inputs. Anyway, my question/problem is
with the member function:

friend ostream& operator <<(ostream& outs, const Complex& number);

taking me to this output line:

ostream& operator <<(ostream& outs, const Complex& number)
{

outs << "\n" << "Numbers started with: " << number.l_real <<
number.l_sign
<< abs(number.l_imaginary) << "i" << " and " << number.r_real
<< number.r_sign
<< abs(number.r_imaginary) << "i" << "." << "\n" << "\n" <<
"Added they equal: "
<< number.real_result1 << "+" << number.imaginary_result1 <<
"i" << endl
<< "Subtracted they equal: " << number.real_result2 << "+" <<
number.real_result2
<< "i" << endl << "Multiplied they equal: " <<
number.real_result3 << "+"
<< number.imaginary_result3 << "i" << endl << "They are " <<
number.answer << "."
<< "\n" << endl;


return outs;


This menagerie compiles and runs on DevC++, however, when compiled on
Borland 5.5, it compiles fine, but when run, it takes the two inputs
and promptly (no puns intended) returns to the command prompt with no
errors and no output....nothing....
 
W

wittempj

Hi Rock,

I suggest you cut your code back to something basic like:

#include <iostream>

using namespace std;

class Complex
{
public:
Complex(long& r, long& i):real(r), imaginary(i) {};
friend ostream& operator << (ostream& outs, const Complex& number);

private:
long real;
long imaginary;
};

ostream& operator << (ostream& outs, const Complex& number)
{

outs << number.real << " + " << number.imaginary << "i";
return outs;
}

int main ()
{
long real, imaginary;
cout << endl << "Enter a complex number in the form of a + bi: " <<
endl;
cout << "Enter real part" << endl;
cin >> real;
cout << "Enter imaginary part" << endl;
cin >> imaginary;
Complex complex(real, imaginary);

cout << complex << endl;

return 0;
}

Once you got that working in both environments start studying
overloading operators, and start adding addition, subtraction,
etcetera.

Good luck.
 
J

Jim Langston

Rock said:
I'm in the process of writing this program for complex numbers and I
use DevC++. My professor on the other hand compiles on Borland 5.5. So
I ocasionally save and run my work on Borland to see if it caught
anything, it's very picky... Anyway, the code below works on Dev, and
it compiles fine on Borland, but when I run it from borland, there is
no output, no error, it just skips right over the freind ostream call.
HELP! I'm new to this and so far it's been a very trying experience...
any help would be greatly appreciated. Rick
....

//Class for complex numbers.
class Complex
{
public: ....

friend ostream& operator <<(ostream& outs, const Complex&
number);
....
};


int main()
{ ....
Complex start(answer1, answer2);
cout << "\n" << start; ....
....

ostream& operator <<(ostream& outs, const Complex& number)
{

outs << "\n" << "Numbers started with: " << number.l_real <<
number.l_sign
<< abs(number.l_imaginary) << "i" << " and " << number.r_real
<< number.r_sign
<< abs(number.r_imaginary) << "i" << "." << "\n" << "\n" <<
"Added they equal: "
<< number.real_result1 << "+" << number.imaginary_result1 <<
"i" << endl
<< "Subtracted they equal: " << number.real_result2 << "+" <<
number.real_result2
<< "i" << endl << "Multiplied they equal: " <<
number.real_result3 << "+"
<< number.imaginary_result3 << "i" << endl << "They are " <<
number.answer << "."
<< "\n" << endl;



return outs;
}

It seems to me that the line:
cout << "\n" << start;
is simply not seeing the overloaded << operator. Why this would be, I'm not
enough of a C++ guru to figure out. One thing I would try would be tomove
the overloaded << definition above main just for the heck of it to see if
that did anything, because you are using the overload before it is defined
publicly. Not sure if that would do anything, but I would try it anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top