Thread: loop problem

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    46

    loop problem

    I finally have my whole program done for school... but when I add the final loop to run until the user presses ctrl-z I now get compile time errors... I don't know how else to work this in though.

    Code:
    #include "complex.h"
    using namespace std; 
    
    int main() {
    	Complex complex; 
    	//while (complex != EOF){
    	cout << "Enter complex number (^Z to end): "; 
    	cin  >> complex; 
    	cout << complex; 
                //}
    	return 0;
    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Did you include iostream? Also, did you overload the input and output operators (>> + <<) for the complex data type?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    46
    Yes... here is the whole picture.

    Code:
    #ifndef COMPLEX_H
    #define COMPLEX_H
    
    #include <iostream> 
    using namespace std;
    
    class Complex {
    	friend ostream &operator<<( ostream&, const Complex & );
    	friend istream &operator>>( istream&,  Complex & );
    
    private:
    	int real;
    	int imaginary;
    	};
    
    #endif
    Code:
    #include "complex.h"
    
    ostream &operator<<( ostream &output, const Complex &num ){
    	output << "The real value of the complex number is " << num.real << "." << "\n" 
    		   << "The imaginary value of the complex number is " << num.imaginary << "." <<"\n";
    	return output; 
    
    }
    
    istream &operator>>( istream &input,  Complex &num ){
    	input >> num.real;
    	input >> num.imaginary;
    	return input; 
    }
    Code:
    #include "complex.h"
    
    int main() {
    	Complex complex;
    	while !(complex == EOF){
    	cout << "Enter complex number (^Z to end): ";
    	cin  >> complex; 
    	cout << complex; 
    	}
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Include the error message, that might help.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    46
    I changed main to this
    Code:
    #include "complex.h"
    
    int main() {
    	Complex complex;
    	cout << "Enter complex number (^Z to end): ";
    	while (cin  >> complex){ 
    	cout << complex; 
    	cout << "Enter complex number (^Z to end): ";
    	}
    	return 0;
    }
    But, for some reason it only runs through once then quits.

  6. #6
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    How is complex gonna equal EOF if it doesn't use a file?

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    46
    I though ctrl-z is the same as EOF in windows.

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Might be, I have never tried to use it. Why don't you just test the input for a quit value such as having both parts equal 0.

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    I have been wrong many times before and maybe again here, but I think EOF in Windows is Ctrl-C.

  10. #10
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    It might be that you compare the entire complex to EOF, but if you got the complex from cin, then only the real would be EOF.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM