How to take a line in a file and put it into an array?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 12
Reputation: swbuko is an unknown quantity at this point 
Solved Threads: 0
swbuko swbuko is offline Offline
Newbie Poster

How to take a line in a file and put it into an array?

 
0
  #1
Sep 16th, 2008
I have to write this program that opens a file then sorts all these numbers in different ways. I plan on using the insertion method cause its one of the fastest, so that shouldn't be too difficult. But when you look at the text file its got spaces and numbers all over the place. I need the numbers of each category and a way to know which ones they belong to so I can sort them? My teacher has thrown in all kinds of ways to mess us up. I haven't taken C++ programming since freshman year and am completely lost. Heres a piece of my input file. Can anyone please help me?


Input File:

****************************************
LOAD BM SHEAR TORSION
STATION # (IN-LBS) (LBS) (IN-LBS)
******** ********** ********** *********
1 12345 23456 34567
2 12345 23456 34567
3 12345 23456 34567
****************************************
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to take a line in a file and put it into an array?

 
0
  #2
Sep 16th, 2008
first create a structure that contains the four numbers that can be found on each line
  1. struct nums
  2. {
  3. int stations;
  4. int lbs1;
  5. int lbs2;
  6. int lbs3;
  7. };

now when you read a line, put the data in a structure, then add the structure to an array of structures.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: swbuko is an unknown quantity at this point 
Solved Threads: 0
swbuko swbuko is offline Offline
Newbie Poster

Re: How to take a line in a file and put it into an array?

 
0
  #3
Sep 17th, 2008
How do you add the data to a structure?
First I open my File in the program, then?



int main ()

{
ofstream myFile;
myFile.open ("InputFile.txt");

}



So I would have to put it in some sort of a structure then into a 2D array.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to take a line in a file and put it into an array?

 
0
  #4
Sep 17th, 2008
  1. int main()
  2. {
  3. vector<nums> theList;
  4. nums data;
  5. ifstream in("filename.txt");
  6. while( in >> data.stations >> data.lbs1 >> data.lbs2 >> data.lbs3 )
  7. {
  8. theList.push_back(data);
  9. }
  10.  
  11. // now you have an array of those structures.
  12. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: swbuko is an unknown quantity at this point 
Solved Threads: 0
swbuko swbuko is offline Offline
Newbie Poster

Re: How to take a line in a file and put it into an array?

 
0
  #5
Sep 17th, 2008
I get an error on the line that says pushback, im not too sure what push_back does though? I keep wanting to test it but i cant figure out these last two errors.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. int main()
  10. {
  11.  
  12. struct nums
  13. {
  14. int stations;
  15. int lbs1;
  16. int lbs2;
  17. int lbs3;
  18. };
  19. int theList;
  20. int vector;
  21. vector<nums> theList;
  22. nums data;
  23. ifstream in("InputFile.txt");
  24. while( in >> data.stations >> data.lbs1 >> data.lbs2 >> data.lbs3 )
  25. {
  26. theList.push_back(data);
  27. }
  28. return 0;
  29. }
Last edited by Ancient Dragon; Sep 17th, 2008 at 11:18 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to take a line in a file and put it into an array?

 
0
  #6
Sep 17th, 2008
>> im not too sure what push_back does though
Did you bother to look it up? When you don't know what something does then you need to do some research and read abo9ut it.

You have to include the header file <vector>
lines 12-18: declare structures outside all functions. Move that up above main();

line 20: delete that line because vector is the name of a c++ class.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: swbuko is an unknown quantity at this point 
Solved Threads: 0
swbuko swbuko is offline Offline
Newbie Poster

Re: How to take a line in a file and put it into an array?

 
0
  #7
Sep 17th, 2008
I just got it running, thats such a relief it seems like. Thanks so much. Its hard to follow what your doing though, just because my C++ class didn't cover some of this stuff. But my teacher, of course, could care less. Its an Aerospace engineering class that uses C++ and the instructor doesn't even know C++? He just assigns us random stuff and expects us to know how to do it haha. By the way I go to Alabama. Getting back on topic, the input file (the grader supplies it) will have a random number of sets of load stations such as:

set of Load Stations:

****************************************
LOAD BM SHEAR TORSION
STATION # (IN-LBS) (LBS) (IN-LBS)
******** ********** ********** *********
1 12345 23456 34567
2 12345 23456 34567
3 12345 23456 34567
****************************************

And each set will have a random number of Load Stations. Like they will all have 2 LS, or 3LS, or 4 LS, etc. If I'm understanding the code correctly it shouldn't a problem but I just thought I'd clear it up with you?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC