loop problem

This is a discussion on loop problem within the C++ Programming forums, part of the General Programming Boards category; I finally have my whole program done for school... but when I add the final loop to run until the ...

  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,136
    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,136
    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,136
    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, 07:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 11:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 05:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 11:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21