Darius
Darius

Reputation: 5259

Read in double type from txt file - C++

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!!

Upvotes: 1

Views: 2710

Answers (5)

akaltar
akaltar

Reputation: 1097

I think you missed the NOT with eof. it should look like this:

while( !File.eof() ); //eof is set when reached end of file.
// and you are looking for while NOT reached end of file..

Upvotes: 0

David Thornley
David Thornley

Reputation: 57036

The getline() is important, since something like cin >> str; will read only the first word of city names like "New York" or "Ho Chi Minh City". Put it in the while loop condition, as Kristo suggests.

After that, you have two possibilities. You can use standard streams to read in a double, in which case you should make sure you're reading all appropriate newlines. (file >> double_var; leaves the following newline to read, so a following getline() will get the rest of the line, which is empty, rather than the next one.) This sort of thing works when you're sure the input is well-formed, which is a lot more common in homework problems than the real world.

Alternately, you can use getline() to read in each line, and then convert the text to double yourself. There are, of course, different ways. The C function atod() is the easiest, but will return 0 either if the number is 0 or it isn't a number. The C function strtod() is more complicated to set up, but will distinguish between those two cases. You can use the C++ solution istringstream, but in this case it looks like far more trouble than it's worth.

Upvotes: 2

anon
anon

Reputation:

std::string city;
double lat, lgt;

while( (inputFile >> city ) &&  (inputFile >> lat) && (inputFile >> lgt ) )   {
    // do something with values
}

Upvotes: 4

codymanix
codymanix

Reputation: 29468

You could use fstream and then do:

double d;
fs>>d;

Upvotes: 1

Michael Kristofik
Michael Kristofik

Reputation: 35188

You can put the getline() call as the condition for the while loop. Its return value will evaluate to false once you reach EOF.

Upvotes: 2

Related Questions