-
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;
}
-
Did you include iostream? Also, did you overload the input and output operators (>> + <<) for the complex data type?
-
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;
}
-
Include the error message, that might help.
-
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.
-
How is complex gonna equal EOF if it doesn't use a file?
-
I though ctrl-z is the same as EOF in windows.
-
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.
-
I have been wrong many times before and maybe again here, but I think EOF in Windows is Ctrl-C.
-
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.