Read in double type from txt file - C++
- by Greenhouse Gases
Hi there
I'm in the midst of a university project and have decided to implement a method that can accept information from a text file (in this instance called "locations.txt"). input from the text file will look like this:
London
345
456
Madrid
234
345
Beinjing
345
456
Frankfurt
456
567
The function looks like this currently (and you will notice I am missing the While condition to finish adding input when reaches end of text in locations.txt, i tried using eof but this didnt work?!). Also get function expects a char and so cant accept input thats a double which is what the latitude and longitude are defined as...
void populateList(){
ifstream inputFile;
inputFile.open ("locations.txt");
temp = new locationNode; // declare the space for a pointer item and assign a temporary pointer to it
while(HASNT REACHED END OF TEXT FILE!!)
{
inputFile.getline(temp-nodeCityName, MAX_LENGTH);
// inputFile.get(temp-nodeLati, MAX_LENGTH);
// inputFile.get(temp-nodeLongi, MAX_LENGTH);
temp-Next = NULL; //set to NULL as when one is added it is currently the last in the list and so can not point to the next
if(start_ptr == NULL){ // if list is currently empty, start_ptr will point to this node
start_ptr = temp;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2-Next != NULL)
{
temp2 = temp2-Next; // Move to next link in chain until reach end of list
}
temp2->Next = temp;
}
}
inputFile.close();
}
Any help you can provide would be most useful. If I need to provide anymore detail I will do, I'm in a bustling canteen atm and concentrating is hard!!