I'm having trouble figuring out how to approach this problem of real + imaginary numbers.
This is the code I have so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Complex {
public:
Complex();
Complex(double realNum);
Complex(double realNum, double imagNum);
//Complex(double real = 0.0, double imaginary = 0.0); This avoids the 3 above?
Complex(const Complex& obj);
private:
double real;
double imaginary;
};
int main () {
Complex *complexArray;
complexArray = new Complex[8];
ifstream myfile("complex.txt");
string line;
int i = 0;
if (myfile.is_open()) {
while (! myfile.eof()) {
getline(myfile, line);
complexArray[i] = line;
i++
};
myfile.close();
};
else {
cout << "Error. Could not find/open file." ;
}
return 0;
};
Complex::Complex(const Complex& obj) {
real = obj.real;
imaginary = obj.imaginary;
};
Complex::Complex () {
real = 0;
imaginary = 0;
};
Complex::Complex (double realNum) {
real = realNum;
imaginary = 0;
};
Complex::Complex (double realNum, double imagNum) {
real = realNum;
imaginary = imagNum;
};
So I realized I cannot read in the complex numbers and store directly into my array...
I'm thinking maybe I should do this?
- read in the numbers and store as a string into an array of strings.
Do a loop to go through the array of strings and for the loop...
- check (how do I do this?) to make sure it is a complex number in correct format, to avoid the "fake Line hi!"
- real = myStringArray[i].at(0) imaginary = myStringArray[i].at(1 & 2 positions...do this somehow)
Anyway, I'm just confused how to approach this problem.
Thanks!