Hello!

I am trying to make a program that reads in two numbers from a file and turns one into an imaginary number and then does different math functions with them. I am trying to use overloaded functions to do them. I am not getting any errors with what I have already but my main is not complete and I am not sure what I have to do from here to call all the functions and make it work correctly.

This is what the input and output should look like:
Input Sample:

2.5 -2.2
1.0 1.0

Output Sample:

A = (2.5) + (-2.2) i
B = (1.0) + (1.0) i

A + B = (3.5) + (-1.2) i
A - B = (1.5) + (-3.2) i
A * B = (2.5) + (-2.2) i
A / B = (2.5) + (-2.2) i

#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

class Complex
{

 public:
  Complex();
  Complex(float r, float i);
  friend ofstream &operator << (ofstream& outFile, Complex num);
  friend ifstream &operator >> (ifstream& inFile, Complex &num);
  double getReal();
  double getImag();
  void setReal(float i);
  void setImag(float i);
  friend Complex operator +(const Complex& num1, const Complex& num2);
  friend Complex operator *(const Complex& num1, const Complex& num2);
  friend Complex operator -(const Complex& num1, const Complex& num2);
  friend Complex operator /(const Complex& num1, const Complex& num2);

  private:
  float real;
  float imag;

};
Complex::Complex()
//***************************************************************
{
  real = 0;
  imag = 0;
}

Complex::Complex(float r, float i)
//***************************************************************
{

  real = r;
  imag = i;
}

double Complex::getImag()
//***************************************************************
{
  return imag;
}

double Complex::getReal()
//***************************************************************
{
  return real;
}

void Complex::setImag(float i)
//***************************************************************
{
  imag = i;
}

void Complex::setReal(float r)
//***************************************************************
{
  real = r;
}

std::ofstream& operator << (std::ofstream& outFile, Complex num)
//***************************************************************
{
  outFile << num.getReal();
  if(num.getImag() > 0)
    outFile << "+";
  outFile << num.getImag();
  outFile << "i";
  return outFile;
}

std::ifstream& operator >> (std::ifstream& inFile, Complex& num)
//***************************************************************
{
  double real, imag;

  inFile >> num.real >>  num.imag;
  num.setReal(real);
  num.setImag(imag);
  return inFile;
}


Complex operator *(const Complex& num1, const Complex& num2)
//***************************************************************
{
  Complex temp;


  temp.real = num1.real * num2.real - num1.imag * num2.imag;
  temp.imag = num1.real * num2.imag + num1.imag * num2.real;

  return temp;
}

Complex operator +(const Complex& num1, const Complex& num2)
//***************************************************************
{
  Complex temp;

  temp.real = num1.real + num2.real;
  temp.imag = num2.imag + num2.imag;

  return temp;
}

Complex operator -(const Complex& num1, const Complex& num2)
//***************************************************************

{
  Complex temp;

  temp.real = num1.real - num2.real;
  temp.imag = num1.imag - num2.imag;

  return temp;
}

Complex operator /(const Complex& num1, const Complex& num2)
//***************************************************************
{
  Complex temp;

  temp.real = (num1.real*num2.real + num1.imag*num2.imag)/(num2.real*num2.real + num2.imag*num2.imag);
  temp.imag = (num1.imag*num2.real - num1.real*num2.imag)/(num2.real*num2.real + num2.imag*num2.imag);

  return temp;
}
int main()
{
  ifstream inFile;
  ofstream outFile;
  inFile.open("in.data");
  outFile.open("out.data");

  if(inFile.fail() || outFile.fail())
    {
      outFile << "Input file or Output file opening failed" << endl;
    }

  Complex complex;

  outFile << "~~~Complex Numbers~~~" << endl;
  outFile << "---------------------" << endl;

  return 0;
}

Thanks for looking and for all of your help.

First make sure the constructors and output functions work right.
Then test the input functions.
Then test each of the math functions separately, adding each to a menu/switch as you call them.

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.