Desperate for help

Got an assignment due in a few days and program is almost complete, however I can't get it to take the input without bugging out. Eclipse debugging crashes and I have no idea how to debug in other IDE's. I have spent this entire day trying to find ONE bug, but its still here. Please someone find it and tell me.

Thanks

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
while (!file.eof()) {

		//std::getline(file, current);

		// std::cout << "Current " + current << std::endl;

		//The text can either start with "P" or " P"
		//create new Polygon when "P" is detected
		char current[5]; //there will be at most 5 digits
		file.get(current[0]);

		bool checkforp = true;

		if (checkforp == true) {
			std::cout << "found a p " << std::endl;

			checkforp = false;
			//remove "P" and whitespaces so we can get the points

			char current[5]; //there will be at most 5 digits
			file.get(current[0]);
			if (current[0] == ' ') {
				file.get(current[0]);
			}

			int sides;
			sides = (int) current[0] - '0';

			//skip whitespace
			file.get(current[0]);
			std::cout << "number of sides: " << sides << std::endl;

			poly = new Polygon(sides);

			//now add the vertices to the polygon
			for (int i = 0; i < sides; i++) {

				//get the x co-ord
				for (int i = 0; i < 5; i++) {
					file.get(current[i]);
					std::cout << "new x char with i: " << i <<
						"and current: "<< current[i] << std::endl;
					if (current[i] == ' ') {
						//if we hit a whitespace then we have read the point co-ord
						i = 5;
					}
				}
				float x = (float) atof(current);

				//get the y co-ord
				for (int i = 0; i < 5; i++) {
					file.get(current[i]);
					std::cout << "new y char with i: " << i <<
						"and current: "<< current << std::endl;
					if (current[i] == ' ') {
						//if we hit a whitespace then we have read the point co-ord
						std::cout << current << std::endl;
						i = 5;
					}
					if (current[i] == 'P') {

						checkforp = true;
						current[i] = '0';
						i = 6666;
					}
				}
				float y = (float) atof(current);

				std::cout << "adding point: " << x << "," << y << std::endl;
				Point *a = new Point(x, y);
				poly->addPoint(a);
				std::cout << "polygon: " << *poly << std::endl;
			}

			std::cout << "adding poly" << *poly << std::endl;
			polygons->append(poly);

		}
	}

	file.close();


Example input would be in the form of:
P 6 4 0 4 8 7 8 7 3 9 0 7 1
P 4 0 0 0 4 4 4 4 0.01
P 4 0 0 0 4 4 4 4 0

Everytime you see a "P" a new shape will be created. The next number is number of points. The points can be only a few decimal places, which is why i limit all the loops to 5. I really have no idea what the bug is other then the fact that it has something to do with the 0.01, after that it doesn't work.
Hello EyeFive,

Welcome to the forum.

The first problem I see is line 1.

while(!inFile.eof())

I do not know why people teahch this. This is a bad idea as it does not work the way you think it will. Generally by the time the while condition determines that "eof" has been reached you will have processed the last read twice and wondering why it shows up twice.

A more acceptable way of using the while loop is:

1
2
3
4
5
6
7
while (infile >> someVariable)
{
    infile >> additionalVariables;

    //  Additional code.

}


I did write this for someone else, but the concept is the same. Today sounds like a good day to rewrite this.

The other thing I see as I read your code is that you create a character array of size 5. Then at some point you will be trying to put a floating point number into a "char". A "char" is one character and "0.01" is 4 "chars".

I will have to see if I can figure something out to see if I can test this. Then I might find something else.

Hope that helps,

Andy
Hello EyeFive,

What does Polygon and Point look like? I think I could figure out Point, bur have no idea what you did for Polygon. Are they classed or structs?

A little stuck at this point on testing.

Andy
EyeFive wrote:
ONE bug
!!!

I don't think char is a sensible type in which to hold numerical data. I'm afraid that I also agree with @HandyAndy's comment:
Today sounds like a good day to rewrite this.


We have no way of testing anything without definitions of your classes. However, it looks like you could try something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   std::string line;
   char c;
   int sides;
   double x, y;

   while ( std::getline( file, line ) )
   {
      std::stringstream ss( line );
      ss >> c;
      if ( c == 'P' || c == 'p' )
      {
         ss >> sides;
         Polygon *poly = new Polygon( sides )

         for ( int i = 1; i <= sides; i++ )
         {
            ss >> x >> y;
            Point *a = new Point( x, y );
            poly->addPoint( a );
         }
         polygons->append( poly );
      }
   }
Last edited on
1
2
3
4
5
6
7
8
         Polygon *poly = new Polygon( sides )

         for ( int i = 1; i <= sides; i++ )
         {
            ss >> x >> y;
            Point *a = new Point( x, y );
            poly->addPoint( a );
         }

Still gives me creeps.

1. new. Could the Polygon have a std::vector<Point> rather than a list of pointers? I bet the delete is not where it should.

2. Sides? The data is points. I presume that there is a "side" between consecutive points + last point connects to first point. Why should the constructor of Polygon know about "sides", when points will be added later?

3. Overloading operator>> for a simple type, like Point, is trivial:
1
2
3
4
5
6
7
8
9
10
11
ss >> sides;
if ( 1 < sides ) {
  Polygon poly;
  int count = 0;
  Point p;
  while ( count < sides and ss >> p ) {
    poly.addPoint( p );
    ++count;
  }
  polygons->append( poly );
}


Oh, if you can do it for Point ...
1
2
3
4
5
6
if ( c == 'P' || c == 'p' )
{
  Polygon poly;
  ss >> poly;
  polygons->append( poly );
}



A real benefit of something like overloading operator is that we can chop larger algorithms into tiny tasks that can each be tested separately, and the code of the algorithm itself will become shorter and more to the point.
Last edited on
Wow! Thanks for the replies everyone. Yeah the sides is pointless, no pun intended, and the char array was always going to be a bad idea. stringstream is a million times better and worked first time. The bug is not present now that I'm not converting char arrays into floats. Thanks for everyone for taking your time for looking at my code as now I'm stressing a lot less!
Topic archived. No new replies allowed.