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;
} 4 6698
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.
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....
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.
"Rock" <ra*****@aol.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com... 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: franky.backeljauw |
last post by:
Hello,
I have a problem with using a copy constructor to convert an object of a
templated class to object of another templated class. Let me first
include the code (my question is below):
...
|
by: Victor Irzak |
last post by:
Hello,
I have an ABC.
it supports:
ostream & operator <<
I also have a derived class that supports this operator.
How can I call operator << of the base class for derived object??? Is it...
|
by: Jacob Foshee |
last post by:
Greetings,
Using MS Visual C++ .NET 2003, I have the following:
template <class T>
class vector3 {
// ...
friend ostream& operator<<(ostream&, const vector3<T>& ); // ostream
operator
}:
|
by: Tim Partridge |
last post by:
I want operator<< to be a friend function of a class inside a namespace, but
I don't know how. For example:
#include <iostream>
namespace ns {
class foo {
public:
// there is something...
|
by: Ook |
last post by:
This is my code in it's entireity:
#include <iostream>
using namespace std;
template <typename T>
class SortedList
{
public:
friend ostream& operator<< (ostream& os, const SortedList<T>&...
|
by: Amadeus W. M. |
last post by:
What is the difference between
friend ostream & operator<<(ostream & OUT, const Foo & f){
// output f
return OUT;
}
and
template <class X>
|
by: atomik.fungus |
last post by:
Hi, as many others im making my own matrix class, but the compiler is
giving me a lot of errors related to the friend functions which
overload >> and <<.I've looked around and no one seems to get...
|
by: noone |
last post by:
hi.
I don't use exceptions much in the embedded world, but for my plugin
interface to a hardware MPEG encoder I'd like to, since there are so many
places that the crummy kernel driver can do bad...
|
by: aaragon |
last post by:
Hi everyone,
I was unable to find out why my code is not compiling. I have a
template class and I'm trying to write the operator<< for standard
output. Does anyone know why this is not right?...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| | |