I got infinite loop
Jan 13, 2019 at 5:28pm UTC
Hello everyone, I try to read the text file which contains length and width for some area. I can read the file but i got infinite loop. may i know which part i did wrong?
my file contains
6 25
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
#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;
}
}
}
int main()
{
int height = 0;
int length= 0;
int count = 0;
Shape temp(0,0);
ifstream source("Shape.txt" );
if (source.is_open())
{
while (source >>height >> length)
{
temp.setX(height);
temp.setY(length);
Shape s;
s.draw();
}
}
else
cout << "Source file failed to open.\n" ;
source.close();
return 0;
}
OUTPUT that i should get:
.........................
.........................
.........................
.........................
.........................
.........................
Last edited on Jan 13, 2019 at 5:31pm UTC
Jan 13, 2019 at 5:53pm UTC
> Shape s;
> s.draw();
Perhaps you could try temp.draw() instead, which is what you just set up.
You should make your constructors actually initialise the object properly, so you don't see weird crap when you try to use a broken object.
Jan 13, 2019 at 5:54pm UTC
It isn't an infinite loop, per se, but it could definitely be a very long loop (of arbitrary length). You aren't setting the x and y values of s. Presumably the while block should be
1 2 3 4
Shape s;
s.setX(height);
s.setY(length);
s.draw();
Last edited on Jan 13, 2019 at 5:55pm UTC
Jan 13, 2019 at 5:58pm UTC
Thank you so much!!! @salem c @ducth I just realized i didnt set the values of x and y
Topic archived. No new replies allowed.