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
}