Archived

This topic is now archived and is closed to further replies.

Could you tell me what's wrong with this code?(134 lines of code)

This topic is 5520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Could you tell me what''s wrong with this code(it''s C++), i''m trying to load a mesh, but and it seems to read perfectly, but some times it gives me a critical error while running. Sorry if it''s to long but i don''t now which part is the weak chain. //-->BEGIN CODE-->BEGIN CODE-->BEGIN CODE-->BEGIN CODE typedef struct sVERTEX{ float x,y,z; }VERTEX; typedef struct sTRIANGLE{ VERTEX Vertex[4]; }TRIANGLE; class MESH{ public: int TrianglesNumber; TRIANGLE* Triangles;//Pointer to array bool MESH::LoadModel(char* FileName) { int VertexNumber;//Temporary, because faces can be of 3+ vertex(later tri*3 = vertex numb) VERTEX* Vertices;//Temporary, for vertex sorting ifstream File(FileName); string Wordz = ""; char Word[50] = ""; if(File.fail())//NO FILE { File.close(); return(false); } //----------NOW TO READ! while(!File.eof()) { File >> Wordz; if(Wordz == "Mesh") break; } if(File.eof())//No mesh found { File.close(); return(false); } //GET MESH VERTEX NUMBER File.getline(Word,50,''\n'');//Jump Meshobject File.getline(Word,50,'';''); VertexNumber = atoi (Word); //Make the pointer array to VERTEX Vertices= new VERTEX[VertexNumber - 1]; //LETS GET THE VERTEX for(int Temp = 0; Temp < VertexNumber; Temp++) { File.getline(Word,50,'';''); Vertices[Temp].x = atof (Word); File.getline(Word,50,'';''); Vertices[Temp].y = atof (Word); if(Temp == VertexNumber) File.getline(Word,50,'','');//EOL else File.getline(Word,50,'';'');//LAST EOL Vertices[Temp].z = atof (Word); if(Temp != (VertexNumber - 1))//If not the end of the vertices info, check for the comma File.getline(Word,1,'','');//JUMP , } //GOT VERTEX, NOW LETS GET TRIANGLES File.getline(Word,50,'';'');//Jump ; File.getline(Word,50,'';''); TrianglesNumber = atoi (Word);//RIGHT NOW, TRIANGLE NUMB = FACES NUMB including quads //Make the pointer array to TRIANGLES Triangles = new TRIANGLE[TrianglesNumber*2];//Triangles number * 2 = Max number of triangles int VertexID[TrianglesNumber*2][3];//First holds which tri and 2nd which vertex //READ FACES!!! for(int Temp = 0; Temp < TrianglesNumber; Temp++) { File.getline(Word,50,'';'');//QUAD OR TRIANGLE? if((atoi(Word) == 4)) {//QUAD Temp++;TrianglesNumber++;//IT''s 2 triangles(raise both to keep the loop unaffected) File.getline(Word,50,'',''); VertexID[Temp - 1][1] = atoi(Word);//1st tri, 1st vertex VertexID[Temp][3] = atoi(Word);//2nd tri 3rd vertex Triangles[Temp-1].Vertex[1].x = Vertices[VertexID[Temp-1][1]].x; File.getline(Word,50,'',''); VertexID[Temp - 1][2] = atoi(Word);//1st tri, 2nd vertex File.getline(Word,50,'',''); VertexID[Temp - 1][3] = atoi(Word);//1st tri, 3rd vertex VertexID[Temp][1] = atoi(Word);//2nd tri, 1st vertex File.getline(Word,50,'',''); VertexID[Temp][2] = atoi(Word);//2nd tri, 2nd, vertex } else {//TRIANGLE File.getline(Word,50,'',''); VertexID[Temp][1] = atoi(Word);//1st vertex File.getline(Word,50,'',''); VertexID[Temp][2] = atoi(Word);//2nd vertex File.getline(Word,50,'',''); VertexID[Temp][3] = atoi(Word);//3rd vertex } //FINISHED READING ALL THE DAMN FACES } for(int Temp = 0; Temp < TrianglesNumber; Temp++) { //First VERTEX Triangles[Temp].Vertex[1].x = Vertices[VertexID[Temp][1]].x; Triangles[Temp].Vertex[1].y = Vertices[VertexID[Temp][1]].y; Triangles[Temp].Vertex[1].z = Vertices[VertexID[Temp][1]].z; //Second VERTEX Triangles[Temp].Vertex[2].x = Vertices[VertexID[Temp][2]].x; Triangles[Temp].Vertex[2].y = Vertices[VertexID[Temp][2]].y; Triangles[Temp].Vertex[2].z = Vertices[VertexID[Temp][2]].z; //Third VERTEX Triangles[Temp].Vertex[3].x = Vertices[VertexID[Temp][3]].x; Triangles[Temp].Vertex[3].y = Vertices[VertexID[Temp][3]].y; Triangles[Temp].Vertex[3].z = Vertices[VertexID[Temp][3]].z; } File.close(); delete Vertices; delete Triangles; return(true); } };

Share this post


Link to post
Share on other sites
you really need to find out where the error is.
Use a messagebox or something.

MessageBox(NULL, "This is where the error is!","Debug", MB_OK);

take that line and move it around until you get near the error. Or even better use a debugger.

- Zorak - Neat Fella.

Share this post


Link to post
Share on other sites
The problem is that everything seems to work well, and i check so many times what was wrong, it works fine with some files, and others give me an error or just close the program. If i knew where the problem is i would not be posting in the forums, so if at least you can point something that might go wrong, but is not evident.

A hint: something weird happens also with this code; if you weren''t going to render the vertex, just loading info in a console window, and you try to use system("pause"), and i suppose that it happens with any system() call, if you run inside the Dev-C++(that''s what i''m using), it doesn''t run, and if you run it outside the IDE it gives a run-time error just like the one it is giving me right now. I didn''t mind it too much because i''m going to use system() in the final program, but i already thought that something weird was going on with this code.

Share this post


Link to post
Share on other sites