im currently busy with a lil asignment but need sum help
i need to input data from a textfile into a structure...
the textfile looks as follows:
hexagonal nut
12345
8.50
54321
23456
98765
circular bolt
54321
-22.00
45678
98765
12345
dart
223344
33.00
54321 |
my main file looks as follows:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include "parts.h"
#include <istream>
using namespace std;
int main () {
string line;
vector<parts> part;
ifstream myfile ("Input.txt");
int i = 0;
{
if (myfile.is_open())
{
while (! myfile.eof() )
{
if (i = 0){
getline (myfile,line);
parts.part_name = split(line);
++i;
}
else if (i = 1){
getline(myfile,line);
parts.part_number = 'line';
++i;
}
else if (i == 2){
getline(myfile,line);
parts.part_price = 'line';
++i;
}
else while (i == 3){
if (isdigit(myfile.peek(line))){
getline(myfile,line);
part.part_links.push_back(line);
}
else {
i = 0;
}
}
myfile.close();
}
else cout << "Unable to open file";
system("pause");
return 0;
}
}
|
my header looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <list>
#include <string>
#include <vector>
using namespace std;
//all the include function come here
struct parts {
string part_name;
long part_number;
double part_price;
vector<long> part_links;
}first;
string split(string);
#endif
|
and my functions are these.. i need some extra though:
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 36 37 38
|
#include "parts.h"
using namespace std;
bool space(char c)
{
//checking if the character is a space
return isspace(c);
}
bool not_space(char c)
{
//checking if the data is not a space
return !isspace(c);
}
//the split function for the strings
vector <string> split(const string& str)
{
typedef string::const_iterator iter;
vector<string> ret;
iter i = str.begin();
while(i != str.end()){
//ignoring the blanks
i = find_if(i, str.end(), not_space);
//finding the end of the next word
iter j = find_if(i, str.end(), space);
if(i != str.end()){
//copying the word
ret.push_back(string(i, j));
i = j;
}
}
return ret;
}
|
now the whole idea is to let the program read automaticly from the text file and input that data into the structure. it will then later have to put the structure into a list, but thats a problem for another day.
can som1 plz help me get this code to work so that it automaticly adds the code to the structure.
the reason for the while loop in the end is because it needs to input part links and there is no definite of how many there can be.
please, any help will be much appreciated