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 6730
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| | |