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;
}

First of all it would be a lot safer to use STL's stringstream, instead of extracting the input stream's content this way. There are a few things I would like to mention.

In your main function you did not cout your complex class, that is one of the reasons why you won't see what you've extracted from the input stream. That is a minor problem it can be fixed easily, however there is bigger one in your >> operator's definition. You've changed the passed stream's flag bit into error state, by saying input.clear ( ios::failbit ). Now if we look back to the main function you have the following if condition:

if( cin.good() )
cout << "Complex number entered was:\n";
 
else
cerr << "Invalid entry.\n";

If you call cin.good () it will return false because you've previously set the errobit. That's why your if condition always ends up in the else branch.

If the sequence of characters in the input stream is valid (a sequence that could have been written out during output), consume all the valid characters and update the object to reflect the new values

Else leave the object unchanged and set the stream to a failed state.

With this stream output operator

std::ostream& operator<< ( std::ostream& stm, const Complex& c )
{ return stm << c.real << std::showpos << c.imaginary << 'i' ; }

The corresponding input operator would be something like:

std::istream& operator>> ( std::istream& stm, Complex& c )
{
    int real;
    int imaginary ;

    if( ( stm >> real >> imaginary ) && ( stm.get() == 'i' ) )
    {
        // successful; put values in the complex object
        c.real = real ;
        c.imaginary = imaginary ;
    }

    else // set the stream to a failed state
           stm.setstate( std::ios::failbit ) ;

    return stm ;
}

thanks for the help. I am always being told by my professor that I am over thinking these things...maybe one day i'll learn.

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.