having issues with my program running propperly. I think i have it right but i need a more skilled eye to take a look at it for me and set me in right direction. the code will complie, but everything i enter comes up invalid when some of it should be coming out correctly.
my header file
#include <iostream>
using namespace std;
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
friend ostream &operator <<( ostream &output, const Complex &c );
friend istream &operator >>( istream &input, Complex &c );
public:
Complex();
private:
int real;
int imaginary;
};
#endif
my class diffentions
#include "complex.h"
#include <iomanip>
Complex::Complex()
:real(0), imaginary( 0 )
{
}
ostream &operator <<( ostream & output, const Complex & c )
{
output << c.real << showpos << c.imaginary << "i\n" << showpos;
return output;
}
istream & operator >>( istream & input, Complex & c )
{
int num;
int multiplier;
char temp;
input >> num;
if( input.peek() == ' ' )
{
c.real = num;
input >> temp;
multiplier = (temp == '+' ) ? 1: -1;
if( input.peek() == ' ')
input.clear( ios::failbit );
else
{
if( input.peek() == ' ' )
{
input >> c.imaginary;
c.imaginary *= multiplier;
input >> temp;
if( input.peek() == '\n')
input.clear( ios::failbit );
}
else
input.clear( ios::failbit );
}
}
else if( input.peek() == 'i')
{
input >> temp;
if( input.peek() == '\n' )
{
c.real = 0;
c.imaginary = num;
}
else
input.clear( ios::failbit );
}
else if( input.peek() == '\n' )
{
c.real = num;
c.imaginary = 0;
}
else
input.clear( ios::failbit );
return input;
}
my test drive file
#include "complex.h"
int main()
{
Complex complex;
cout << "Enter a complex number in format of x + yi:\n";
cin >> complex;
if( cin.good() )
cout << "Complex number entered was:\n";
else
cerr << "Invalid entry.\n";
system("pause");
return 0;
}