How can i output a certain part of an object only if it's there/a certain circumstance?

Sorry if title is unclear, the problem i'm having is i have to import a txt file with complex numbers, some of them however have no imaginary or real part, and i don't know how to only output the imaginary or real part if the other is missing.

Here is my code:

.h header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef COMPLEXOBJ_H
#define COMPLEXOBJ_H
#include <iostream>


class complexType
{
friend std::ostream& operator<<(std::ostream& os, const complexType& obj);
friend double getreal(const complexType& sample1);
friend char getsign(const complexType& sample2);
public:
  complexType();
  complexType(double r, double i, char signin);
  double getreal() const;
private:
    double real;
    double imagine;
    char sign;
};

#endif // COMPLEXOBJ_H 


.cpp class file:

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
#include "Complexobj.h"
#include <iostream>
using namespace std;


complexType::complexType()
{
real=0;
imagine=0;
sign= '+';
}

complexType::complexType(double r, double i, char signin)
{
real=r;
imagine=i;
sign=signin;
}

ostream& operator<<(ostream& os, const complexType& obj)
{
os << obj.real<< obj.sign << obj.imagine << "i";

return os;
}

double complexType::getreal() const
{
return real;
}


cpp main file:
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "Complexobj.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;

void sorter(complexType[], int countin);

int main()
{
ofstream outputfile;
ifstream inputfile;
string str;
double realpart;
double imaginarypart;
int symbol;
char ch;
string strone;
string strtwo;

complexType storage[100];
int counter = 0;

inputfile.open("126import.txt");
if(inputfile.fail())
{
    cout << "File opening failed." << endl;
    exit(1);
}

outputfile.open("126export.txt");

inputfile >> str;
while(inputfile)
{
    char firstch = '+';
    if(str.at(0) == '-')
    {
        str = str.substr(1,str.length() - 1);
        firstch = '-';
    }
    symbol=str.find("+");
    ch = '+';
    if(symbol < 0)
    {
        symbol = str.find("-");
        ch = '-';
    }
    stringstream streamin(str);
    getline(streamin, strone, ch);
    getline(streamin, strtwo, 'i');


    realpart= atof(strone.c_str());
    imaginarypart= atof(strtwo.c_str());

    if(ch == '-')
        realpart *= -1;

    complexType outpobj(realpart, imaginarypart, ch);
    storage[counter]=outpobj;


    counter++;

    inputfile >> str;

}

sorter(storage, counter);



for(int u=0; u<counter;u++)
{
    outputfile << "Object " << u+1 << ": " << storage[u] << endl;
}





    inputfile.close();
    outputfile.close();

    return 0;
}

void sorter(complexType storarray[], int countin)
{
complexType temp;

for(int k=1; k<countin;k++)
{
  for(int j=0;j<countin-k;j++)
  {
    if(storarray[j].getreal() > storarray[j+1].getreal())
    {
        temp=storarray[j];
        storarray[j]=storarray[j+1];
        storarray[j+1] = temp;
    }
}
}
}


Input file(import126.txt) is:


1+1i
2+2i
3.3+3.4i
4.4-4.5i
-5.5-5.6i
-6
7i
-8i


Output file is wrong however and displays:


Object 1: -8-5.6i
Object 2: -7-5.6i
Object 3: -6-5.6i
Object 4: -5.5-5.6i
Object 5: -4.4-4.5i
Object 6: 1+1i
Object 7: 2+2i
Object 8: 3.3+3.4i


with the 5.6 i's at the start because it doesn't know how to seperate them

I know the problem is with my output overload or when my main reads in the complex objects but im not sure how to fix it.

Any ideas how to fix it?

oh and here is what i must achieve in program:


Read in a file of complex numbers (sample is attached 126import.txt) and store them in an array of complex objects.
Sort the objects use one sorting algorithm (e.g. bubble-sort)
Write the sorting results back to a file 126export.txt.

Last edited on
Topic archived. No new replies allowed.