| | |
How to take a line in a file and put it into an array?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
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
****************************************
Input File:
****************************************
LOAD BM SHEAR TORSION
STATION # (IN-LBS) (LBS) (IN-LBS)
******** ********** ********** *********
1 12345 23456 34567
2 12345 23456 34567
3 12345 23456 34567
****************************************
first create a structure that contains the four numbers that can be found on each line
now when you read a line, put the data in a structure, then add the structure to an array of structures.
C++ Syntax (Toggle Plain Text)
struct nums { int stations; int lbs1; int lbs2; int lbs3; };
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.
C++ Syntax (Toggle Plain Text)
int main() { vector<nums> theList; nums data; ifstream in("filename.txt"); while( in >> data.stations >> data.lbs1 >> data.lbs2 >> data.lbs3 ) { theList.push_back(data); } // now you have an array of those 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.
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <stdlib.h> #include <cmath> using namespace std; int main() { struct nums { int stations; int lbs1; int lbs2; int lbs3; }; int theList; int vector; vector<nums> theList; nums data; ifstream in("InputFile.txt"); while( in >> data.stations >> data.lbs1 >> data.lbs2 >> data.lbs3 ) { theList.push_back(data); } return 0; }
Last edited by Ancient Dragon; Sep 17th, 2008 at 11:18 pm. Reason: add code tags
>> 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.
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.
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
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?
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?
![]() |
Similar Threads
- Programming in VBA reading file into dynamic array (VB.NET)
- Deleting a line from a file (PHP)
- CSV file (C)
- Getting an array from a txt file (C)
- loop to create arrays when reading a file (Java)
- Help Reading Info in Text File Into an Array (C++)
- How can I read from a txt file? (C++)
- reading a file into code (Java)
Other Threads in the C++ Forum
- Previous Thread: how to call managed c++ from unmanaged c++ ?
- Next Thread: template function overloading
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






