Program closing, without any errors

This is a discussion on Program closing, without any errors within the C++ Programming forums, part of the General Programming Boards category; Hallo, I have a problem with my program. When I try to open it, it closes again, just like if ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Program closing, without any errors

    Hallo,

    I have a problem with my program. When I try to open it, it closes again, just like if I did not have system("PAUSE"); at the end. This did not happen until I tried to use my loadFromFile() function.

    Any ideas on what is causing the trouble? I also tried to add a cin >>, but same thing happen.

    Just so you know, the loadFileFunction is work in progress so please dont kill me for bad stuff in it.


    Here is my main.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    
    #include "Material.h"
    #include "Scene.h"
    #include "Raytracer.h"
    #include "Input.h"    // This is the only header I get info from now
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        cout << " OK raytracer " << endl;
        cout << " ---------------------------------------\n" << endl; 
        
        std::vector<Triangle> stuff;
        
        stuff = loadFromFile("c:\\testFile.txt");
        
        
        cout << "done" << endl;
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Input.cpp
    Code:
    #include "Input.h"
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <windows.h>
    #include <vector>
    #include <sstream> 
    
    
    std::vector<Triangle> loadFromFile(char* FileName)
    {     
          std::string currentLine;            // Variable that holds the current line
                                              // we are using.
          int nrLines = 0;                    // Variable for checking how many lines 
                                              // there are in the file      
          
          // Open file
          std::ifstream myFile (FileName);
          
          if (myFile.is_open())
          {
              // While the file is open, we read the info and construct triangles
              while (! myFile.eof())
              {
                  // Read line
                  getline(myFile, currentLine);
                  
                  // Check if this line holds vertex information. If the line starts 
                  // with V, we will use it.
                  
                  if (currentLine[0] == 'V')
                  {
                      // Break the line into different components, so that we can
                      // create 3 different vertexes for each cordinate in the file
                      // V: 0.0,0.0,0.0 - 1.0,1.0,1.0 - 2.0,2.0,2.0
                      
                      // An array of strings, for reading the 3 vertexes we will later
                      // use
                      std::string vertex[2];
    
                      // Variable for keeping track of which character in currentLine
                      // we are reading, it starts at 2, as the 3 first characters
                      // are not part of the number we are tring to read
                      int x = 2;
                      
                      // Make a loop for creating 3 vertexes
                      for (int i = 0; i < 3; i++)
                      {
                          // Make sure the vertex string we are writing to now is 
                          // empty
                          vertex[i].clear();
                          
                          // Make sure the current character is part of the number
                          while (currentLine[x] != '.' || currentLine[x] != ' ')
                          {
                             // If the current character is part of a number,
                             // append it to the vertex string
                             vertex[i].push_back(currentLine[x]);
                             x++;
                          }
                          
                          std::cout << vertex[i] << "  ";  // Temp line for printing
                          x++;
                      }
                      
                      std::cout << std::endl;              // Temp line for printing
                      
                      // Convert the vertex arrays into numbers, so we can use them
                      // to create triangles.
                      
                      
                  }
    
                  // Increment the line counter
                  nrLines++;
              }
              
              // Finished with reading the file, close it
              myFile.close();
          }
          
          
          
          // Read the cordinates of the three first vertexes
          // and pass them to a new triangle
          // Continiu this until file is finished
          // Return the vector array of triangles     
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> if (currentLine[0] == 'V')
    Are there any empty lines in the file? If so, this could cause a crash, since currentLine would be empty and the element at index 0 won't exist.

    >> std::string vertex[2];
    This array has a size of 2, not 3. When declaring an array, the value inside the brackets is the size.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Quote Originally Posted by Daved View Post
    >> if (currentLine[0] == 'V')
    Are there any empty lines in the file? If so, this could cause a crash, since currentLine would be empty and the element at index 0 won't exist.

    >> std::string vertex[2];
    This array has a size of 2, not 3. When declaring an array, the value inside the brackets is the size.
    No, there are no empty lines

    here is my file:
    random line 11 ra
    V: 2.020,0.121,12.1212 4.657,11.31,9.11 4.7,11.00,5.411
    V: 2.020,0.121,12.1212 4.657,11.31,9.11 4.7,11.00,5.411
    V: 2.020,0.121,12.1212 4.657,11.31,9.11 4.7,11.00,5.411
    V: 2.020,0.121,12.1212 4.657,11.31,9.11 4.7,11.00,5.411

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    have you tried running it in a debugger?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Well, I have tried, but I dont really know how to do debugging. No matter what I do I get told by my compiler that I have to activate debugging mode, to use any of the debugging tools. But activating it still gives me the same message.

    Using dev-c++

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Did fixing the string array help the problem?

    >> while (currentLine[x] != '.' || currentLine[x] != ' ')
    Also note that this will run forever (the expression is always true). Every character is either not equal to '.' or not equal to ' '.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Did fixing the string array help the problem?
    I tried to change it to two (assuming you mean in my for loop?), but it did not help. Also, I dont think I need to do so, as I have < 3, not <= 3, so I will count 3 as false and dont do the loop. Or?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    No, the problem is that your array only has room for 2 strings, not 3. I assume you wanted 3 since that is what your comments suggest, so you would want to change your array declaration to have a size of 3.

    Note that this code:
    Code:
    for (int i = 0; i < 3; i++)
    runs through 3 indexes even though you use <. It runs through the indexes 0, 1 and 2. These are valid indexes for an array of size 3, which again is what I assume you want. So leave the for loop the way it is and fix the size of the array.

    Also, I edited my previous post with another observation which will also certainly cause a crash in your program.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    runs through 3 indexes even though you use <. It runs through the indexes 0, 1 and 2. These are valid indexes for an array of size 3, which again is what I assume you want. So leave the for loop the way it is and fix the size of the array.

    Also, I edited my previous post with another observation which will also certainly cause a crash in your program.
    But vertex[2] is made up of [0],[1] and [2], or am I mixing something up here?

    I changed the || to && (think that will be right?) but same thing happens, but thanks for pointing out

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> But vertex[2] is made up of [0],[1] and [2], or am I mixing something up here?
    Yes, you are mixing up something here. This code creates an array of size 2:
    Code:
    std::string vertex[2];
    That means the array holds only two elements. Valid array indexes go from 0 to size-1. So in that case, only vertex[0] and vertex[1] are valid. That's what I meant about the number inside the brackets being the size in the declaration. If you want elements vertex[0], vertex[1] and vertex[2] to be available, that means you want 3 elements, so you should declare your array to have a size of 3:
    Code:
    std::string vertex[3];
    >> I changed the || to && (think that will be right?) but same thing happens
    Yes, I think && is correct. Both the string array issue and this issue could have caused crashes, so both need to be fixed regardless. If you fix them both and you still get a crash, then maybe there is another error that we haven't seen yet.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thanks for learning me that. I was so sure that the zero counted when creating an array, as it counts when you try to access elements of it. Better to learn it late than never.

    But it did not fix the closing problem =\

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could try adding a cin.get() to see if you can tell how far it reads. Like:
    Code:
                          std::cout << vertex[i] << "  ";  // Temp line for printing
                          x++;
                      }
                      
                      std::cout << std::endl;              // Temp line for printing
                      cin.get();
    Code:
    >          // While the file is open, we read the info and construct triangles
    >          while (! myFile.eof())
    >          {
    >              // Read line
    >              getline(myFile, currentLine);
    It's better to write the above as:
    Code:
              // While the file is open, we read the info and construct triangles
              while(getline(myFile, currentLine))
              {

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    The problem was caused by the function, as it did not return anything.

    Fixed now, thanks for the help
    Last edited by h3ro; 05-07-2007 at 04:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. mega compile errors for small program
    By s_UNI_ in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2005, 12:00 PM
  3. exiting and closing a program
    By major_small in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2003, 08:31 PM
  4. Average Rainfall Program Errors
    By JamesAnthony23 in forum C Programming
    Replies: 1
    Last Post: 09-11-2002, 10:44 PM
  5. I'm a newbie and I can't understand the errors in my program
    By iluvmyafboys in forum C++ Programming
    Replies: 19
    Last Post: 02-20-2002, 09:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21