Read text file for classes object

closed account (iN8poG1T)
Hello everyone, I want to read my shape, and also the inheritance rectangle from a text file. I got an error : id returned 1 exit status. I'm still learning about classes which i think I did wrong in my int main for rectangle. Is there anything i should change?

source.txt

6 25
4 7

PSA: 6 25 is for my shape and 4 7 is for my rectangle

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 <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

class Shape
{
private :
    int x;
    int y;

public :
    Shape();
    Shape(int newx, int newy);
    int getX ();
    int getY();
    void setX(int newx);
    void setY (int newy);
    virtual void draw();


};

Shape :: Shape (){}
Shape :: Shape(int newx, int newy){}
int Shape :: getX(){return x;}
int Shape :: getY(){return y;}
void Shape :: setX(int newx){x= newx;}
void Shape :: setY(int newy){y= newy;}
void Shape :: draw(){
    {
        for(int i=0; i<x; i++)
        {
            for(int j=0; j<y; j++)
                cout << ".";
            cout<<endl;
        }
    }
}

class Rectangle : public Shape
{
private:
   int width;
   int height;
public:
   Rectangle();
   Rectangle(int newx, int newy, int widthR, int heightR);
   int getWidth();
   int getHeight();
   void setWidth(int widthR);
   void setHeight(int heightR);
   void draw();

};

Rectangle::Rectangle(int newx, int newy, int widthR, int heightR): Shape(newx, newy) {
   setWidth(widthR);
   setHeight(heightR);
}

int Rectangle :: getWidth(){return width;}
int Rectangle :: getHeight(){return height;}
void Rectangle::setWidth(int widthR) { width = widthR; }
void Rectangle::setHeight(int heightR) { height = heightR;}

void Rectangle::draw() {
    for(int i=0; i<height; i++)
        {
            for(int j=0; j<width; j++)
            cout << "B";
        cout<<endl;
        }

}

int main()
{
    int height = 0;
    int length= 0;
    int count = 0;

    Shape temp(0,0);
    ifstream source("source.txt");

    if (source.is_open())
    {
       while (source >>height >> length)
        {
           Shape s;
           s.setX(height);
           s.setY(length);
           s.draw();

           Rectangle r;
           r.setWidth(height);
           r.setHeight(length);
        }
    }
    else
      cout << "Source file failed to open.\n";
    source.close();

    return 0;
}


OUTPUT i should get :

.........................

.........................

.........................

.........................

.........................

.........................

BBBBBBB
BBBBBBB
BBBBBBB
BBBBBBB
I could be wrong but it looks to me like shape's constructor does not actually set x and y, leaving them as random values. This is not your crash problem, though.

you don't seem to draw the rectangle either, in the read file loop.

add this
cout << count++ << endl; in your while loop at the top of it
cout.flush();

also you read from the file weird.
it says this:
while (read height & length)
draw the shape of height/length
do nothing with the rectangle of height/length
loop again...
it should say this
while(…)
do shape stuff
read height and length again
do rectangle (including missing draw) stuff

Last edited on
closed account (iN8poG1T)
before the error : id returned 1 exit status, there is undefined reference Rectangle :: Rectangle (). before i put the value as a text file, it can run, but when i start to read from text file. it shows the error
closed account (iN8poG1T)
like this? i still get the same error. is there any problem with my inheritance? because i'm still learning about inheritance in this semester

| undefined reference to `Rectangle::Rectangle()'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 if (source.is_open())
    {
       cout.flush();
       while (source >>height >> length)
        {
           cout << count++ << endl;
           Shape s;
           s.setX(height);
           s.setY(length);
           s.draw();

           Rectangle r;
           r.setWidth(height);
           r.setHeight(length);
           r.draw();
        }
    }
    else
      cout << "Source file failed to open.\n";
    source.close();
Last edited on
closed account (iN8poG1T)
I edit my code because i forgot to put the constructor rectangle. the program can run but instead of getting my original output shown in my question,

i get

.........................

.........................

.........................

.........................

.........................

.........................

BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB

.......
.......
.......
.......

BBBBBBB
BBBBBBB
BBBBBBB
BBBBBBB

how should i make it stop? and why it prints twice of it



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
108
109
110
111
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

class Shape
{
private :
    int x;
    int y;

public :
    Shape();
    Shape(int newx, int newy);
    int getX ();
    int getY();
    void setX(int newx);
    void setY (int newy);
    virtual void draw();


};

Shape :: Shape (){}
Shape :: Shape(int newx, int newy){}
int Shape :: getX(){return x;}
int Shape :: getY(){return y;}
void Shape :: setX(int newx){x= newx;}
void Shape :: setY(int newy){y= newy;}
void Shape :: draw(){
    {
        for(int i=0; i<x; i++)
        {
            for(int j=0; j<y; j++)
                cout << ".";
            cout<<endl;
        }
    }
}

class Rectangle : public Shape
{
private:
   int width;
   int height;
public:
   Rectangle();
   Rectangle(int newx, int newy, int widthR, int heightR);
   int getWidth();
   int getHeight();
   void setWidth(int widthR);
   void setHeight(int heightR);
   void draw();

};

Rectangle :: Rectangle (){};
Rectangle::Rectangle(int newx, int newy, int widthR, int heightR): Shape(newx, newy) {
   setWidth(widthR);
   setHeight(heightR);
}

int Rectangle :: getWidth(){return width;}
int Rectangle :: getHeight(){return height;}
void Rectangle::setWidth(int widthR) { width = widthR; }
void Rectangle::setHeight(int heightR) { height = heightR;}

void Rectangle::draw() {
    for(int i=0; i<height; i++)
        {
            for(int j=0; j<width; j++)
            cout << "B";
        cout<<endl;
        }

}

int main()
{
    int height = 0;
    int length= 0;
    int count = 0;

    Shape temp(0,0);
    ifstream source("Batch.txt");

    if (source.is_open())
    {

       while (source >>height >> length)
        {
           Shape s;
           s.setX(height);
           s.setY(length);
           s.draw();

           Rectangle r;
           r.setWidth(length);
           r.setHeight(height);
           r.draw();

        }
    }
    else
      cout << "Source file failed to open.\n";
    source.close();

    return 0;
}
Last edited on
Topic archived. No new replies allowed.