|
-
Oct 27th, 2002, 05:12 PM
#1
Thread Starter
New Member
Getting data from TXT file...
This is the TXT file:
NORTH-SOUTH road
2 4 6 8 0 3 5 7 9 8 6 4 2 -4 -6 -8 -2 0 -6 -8 -3 1 3 5 7 0 0 11 8 4 1 -3 2 9 4 6 2 0 -7 -9 -5 -4 -2 3 8 7 1 -9 -5 -3 -8 -9 1 3 5 7 9 12 13 7 5 2 0 -2 -999
WEST-EAST road
3 5 7 0 0 11 8 4 1 -3 2 2 0 -7 -9 -5 -4 -2 3 8 7 1 -9 -5 9 8 6 4 -2 0 -6 -8 -3 1 3 5 7 0 0 14 8 4 1 -3 2 6 4 2 -4 -6 -8 -2 0 -6 -8 -999
I need to input the integers only into two arrays, let's call them RoadNS[?] and RoadWE[?].
When it reads the file it should 'skip' the letters 'NORTH-SOUTH road', input the integers {2, 4, 6, 8, 0.....} into array RoadNS[?], terminate when it sees the first '-999', then again 'skip' the letters 'WEST-EAST road', input the rest of the integers {3, 5, 7, 0, ......} into array RoadWE[?], and terminate at '-999'.
My problem is that I don't know how to skip the letters, then resume and start inputing the integers into an array, then terminate at -999 and again skip the letters and input the rest of the integers...
I CAN ONLY USE SIMPLE ARRAYS, FOR LOOPS, DO/WHILE LOOPS, FUNCTIONS AND CLASSES. NO SPECIAL ARRAYS ETC, I'M ONLY A BEGINNER 
Please help
-
Oct 27th, 2002, 05:38 PM
#2
Have you deleted the old thread? You should leave it there, for someone to search for information applicable to his/her own problem.
The exercise interested me, I have written a program that does just that. I will not post it, only use it for my own reference.
The code I used is quite complicated. I basically did the following:
1) Read one line of the text file into a string buffer.
2) Check if the line contains the word "road".
3) If yes, start over.
4) Else read in integers until you hit -999 or there are no more integers in the line.
5) Start over.
Just to give you a taste, here are the header files my app includes + the using declarations:
Code:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
using std::string; // <string>
using std::ifstream; // <iostream>
using std::istringstream; // <sstream>
using std::cout; // <iostream>
using std::endl; // <iostream>
using std::vector; // <vector>
using std::getline; // <string>
using std::copy; // <algorithm>
using std::max_element; // <algorithm>
using std::min_element; // <algorithm>
using std::count_if; // <algorithm>
using std::ostream_iterator; // <iterator>
using std::unary_function; // <functional>
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 27th, 2002, 07:37 PM
#3
Thread Starter
New Member
My 16-year old friend figured out how to do this. Here's the code he used:
{
ifstream fin("assign_07_in.txt");
if (!fin.is_open())
exit(1);
int RoadNS[70] = { 0 };
int RoadWE[70] = { 0 };
fin.ignore(100, '\n');
int temp_input;
int i = 0;
while (fin >> temp_input && temp_input != -999)
{
RoadNS[i] = temp_input;
i++;
}
fin.get();
i = 0;
fin.ignore(100, '\n');
while (fin >> temp_input && temp_input != -999)
{
RoadWE[i] = temp_input;
i++;
}
fin.close();
}
-
Oct 28th, 2002, 03:57 AM
#4
Monday Morning Lunatic
Code:
int RoadNS[70] = { 0 };
What happens when your teacher decides to test your program by giving more than 70 numbers?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 28th, 2002, 05:30 AM
#5
Ok, you have working code for reading in. Now here's mine:
Code:
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
using std::string;
using std::ostream;
using std::ifstream;
using std::istringstream;
using std::cout;
using std::endl;
using std::vector;
using std::getline;
const int NUM_ROADS = 2;
bool parseline(vector<int> &road, const string &line);
void main()
{
cout << "Reading file" << endl;
ifstream ifs("streets.txt");
string line;
int i=0;
vector<int> roads[NUM_ROADS];
while(!ifs.eof())
{
getline(ifs, line);
if(parseline(roads[i], line))
++i;
}
}
bool parseline(vector<int> &road, const string &line)
{
int temp=0, start;
if(line.find("road") == string::npos)
{
istringstream iss(line);
iss >> start;
road.push_back(start);
while(!iss.eof() && temp != -999)
{
iss >> temp;
road.push_back(start+temp);
}
if(temp == -999)
{
road.pop_back();
temp = 0;
return true;
}
}
return false;
}
Independent of the number of roads and of the number of points per road. And it automatically handles the relative heights.
For your code: how do you know how many elements were read for the arrays? You don't save that...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 28th, 2002, 07:41 AM
#6
Thread Starter
New Member
int RoadNS[70] = { 0 }
What happens when your teacher decides to test your program by giving more than 70 numbers?
__________________
why? what do you think would happen?
-
Oct 28th, 2002, 07:44 AM
#7
Monday Morning Lunatic
Well, what do you think? There's only space for 70 in there, so when you add the 70th item (indexes from 0, remember), where does it go?
The answer is, most likely on top of whatever's defined next. This can cause all sorts of amusing bugs.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 28th, 2002, 07:45 AM
#8
Thread Starter
New Member
For your code: how do you know how many elements were read for the arrays? You don't save that...
__________________
That's what I said so I've modified the code a little bit myself. Here's my final version:
int RoadNS[70] = { 0 };
int RoadWE[70] = { 0 };
fin.ignore(100, '\n');
int temp_input;
int x = 0, y = 0;
while (fin >> temp_input && temp_input != -999)
{
RoadNS[x] = temp_input;
x++;
}
fin.get();
fin.ignore(100, '\n');
while (fin >> temp_input && temp_input != -999)
{
RoadWE[y] = temp_input;
y++;
}
fin.close();
That will give me the size of both arrays in x and y.
-
Oct 28th, 2002, 07:48 AM
#9
Thread Starter
New Member
Well, what do you think? There's only space for 70 in there, so when you add the 70th item (indexes from 0, remember), where does it go?
The answer is, most likely on top of whatever's defined next. This can cause all sorts of amusing bugs.
__________________
Ohh, you're right, of course it would cause a problem. However, in my homework we don't have to worry about it so I just counted the numbers manually Remember, we're just starting to cover Arrays...
-
Oct 28th, 2002, 07:51 AM
#10
Monday Morning Lunatic
I'd say go with CB's method of using a vector. That's what they're there for, and if your teacher/course says different they're an imbecile (and yes, you can tell them I said that and where to find me... )
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 28th, 2002, 08:01 AM
#11
Thread Starter
New Member
I'd say go with CB's method of using a vector. That's what they're there for, and if your teacher/course says different they're an imbecile (and yes, you can tell them I said that and where to find me... )
______________
You don't know how much I agree with you parksie...
Just in case you're interested, my teacher assistant (not the professor) finally decided to give me a hint... This is how they want us to do it:
int RoadNS[70], RoadWE[70], a, b;
a = 0;
b = 0;
Declare a character variable here.
fin.get(character variable);
while(character != newline character)
fin.get(character variable);
fin >> RoadNS[a];
while (RoadNS[a] != -999)
{
a++;
fin >> RoadNS[a];
}
fin.get(character variable);
while(character != newline character)
fin.get(character variable);
fin >> RoadWE[b];
while (RoadWE[b] != -999)
{
b++;
fin >> RoadWE[b];
}
I haven't tried it yet, but from just looking at it do you think it would work?
-
Oct 28th, 2002, 08:40 AM
#12
Yes, except that it doesn't take into account the relative heights.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|