Short, hopefully easy question... Complex numbers

bottom...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Lab 2: Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream> 
using std::ostream;
using std::istream;

class Complex 
{
   // overloaded input and output operators
	friend ostream &operator<<( ostream &output, const Complex &c );
	friend istream &operator>>( istream &input, Complex &c );
   /* Write friend declarations for the stream insertion
      and extraction operators */

public:
   Complex( void ); // constructor
private:
	int real;
	int imaginary;
   /* Write declarations for data members real and imaginary */
}; // end class Complex

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Lab 2: Complex.cpp
// Member-function definition of class Complex.
#include <iostream> 
using std::ios;
using std::istream;
using std::ostream;

#include <iomanip> 
using std::showpos;

#include "Complex.h"

// default constructor
Complex::Complex( void ):
   real( 0 ),
   imaginary( 0 )
{
   // empty body
} // end Complex constructor

// overloaded output (<<) operator
ostream &operator<<( ostream &output, const Complex &c )
{
   output << c.real << showpos << c.imaginary << "i\n" << showpos;
   return output; // return ostream reference
} // end overloaded output (<<) operator

// overloaded input (>>) operator
istream &operator>>( istream &input, Complex &c )
{
   int number;
   int multiplier;
   char temp; // temporary variable used to store input

   input >> number; // get input

   // test if character is a space
   if ( input.peek() == ' '  /* Write a call to the peek member function to
		   test if the next character is a space ' ' */ ) // case a + bi
   {
      c.real = number;
      input >> temp;

      multiplier = ( temp == '+' ) ? 1 : -1;

      // set failbit if character not a space
      if ( input.peek() != ' ' ) 
         /* Write a call to the clear member function with
		    ios::failbit as the argument to set input's fail bit */
			input.clear( ios::failbit );
      else 
      {
         // set imaginary part if data is valid
         if ( input.peek() == ' ' ) 
         {
            input >> c.imaginary;
            c.imaginary *= multiplier;
            input >> temp;

            if ( input.peek() == '\n' /* Write a call to member function peek to test if the next
				    character is a newline \n */ ) // character not a newline
               input.clear( ios::failbit ); // set bad bit
         } // end if
         else
            input.clear( ios::failbit ); // set bad bit
      } // end else
   } // end if
   else if ( input.peek() == 'i' /* Write a call to member function peek to test if
			    the next character is 'i' */ ) // test for i of imaginary number
   {
      input >> temp;

      // test for newline character entered
      if ( input.peek() == '\n' ) 
      {
         c.real = 0;
         c.imaginary = number;
      } // end if
      else
         input.clear( ios::failbit ); // set bad bit
   } // end else if
   else if ( input.peek() == '\n' ) // set real number if it is valid
   {
      c.real = number;
      c.imaginary = 0;
   } // end else if
   else
      input.clear( ios::failbit ); // set bad bit

   return input;
} // end overloaded input (>>) operator 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Lab 2: ComplexInput.cpp
// Complex test program.
#include <iostream> 
using std::cin;
using std::cout; 
using std::endl;

#include "Complex.h"

int main()
{
   Complex complex; // create Complex object

   // ask user to enter complex number
   cout << "Input a complex number in the form A + Bi:\n";
   cin >> complex; // store complex number

   if (  /* Write a call to member funciton fail to determine if the 
		   stream operation failed, then negate it to test if input 
		   was valid */ ) // display complex number entered by user if valid
      cout << "Complex number entered was:\n" << complex << endl;
   else
      cout << "Invalid Data Entered\n";

   return 0;
} // end main 



Ok i think i got this pretty much done but i got confused in the ComplexInput.cpp where it says
/* Write a call to member funciton fail to determine if the
stream operation failed, then negate it to test if input
was valid */ ) // display complex number entered by user if
Im not sure what this means, or how to write this.... can anyone help? Im pretty sure its an easy question, and i think the rest of the code is right (i didnt get any errors). Also if anyone can explain what the purpose of this is and how it works that would be great!! Thanks in advance!
Anyone help with this please....
if (cin.good())
and cin.clear() sets the error flags to good again.
So this... if (cin.good())....should work?
Topic archived. No new replies allowed.