Reading from file, strings and integers.
I have a file containing a city, then degrees and minutes for north, followed by degrees and minutes for west. Here is an example:
Birmingham AP
33 34 86 45
I would like to put each city in a string array then the other values into four integer arrays. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <fstream>
using namespace std;
#include <string>
int numcities = 744;
int main ()
{
int northdegree[numcities], northminute[numcities], westdegree[numcities], westminute[numcities];
string name[numcities], string;
for (int i = 0; i < numcities; i++)
{
northdegree[i] = 0;
northminute[i] = 0;
westdegree[i] = 0;
westminute[i] = 0;
name[i] = " ";
}
ifstream in ("Cities N W Degrees Minutes.txt");
int counter = 0;
while (!in.eof() && counter < 744)
{
getline(in,string);
name[counter] = string;
cin.in >> northdegree[counter] >> northminute[counter] >> westdegree[counter] >> westminute[counter];
cout << name[counter] << " " << northdegree[counter] << " " <<
northminute[counter] << " " <<
westdegree[counter] << " " <<
westminute[counter] << endl;
counter++;
}
in.close();
return 0;
}
|
Which gives me output:
0 0 0 0 AP
0 0 0 048 37 112 22
Which confuses me. By the way the values "48 37 112 22"
are on the 744th line of the text file. There are 1488 lines in total.
Last edited on
on line 26: you're using cin
in place of in
So you might read more than 744 entries which leads to a crash.
You should introduce an if that breaks the loop if count
has reached numcities
I've updated what I've got at this point. I think I need it to read in the next line for the ints but I'm having difficulty figuring out how to do so.
Topic archived. No new replies allowed.