Putting text file into an array of structures
Feb 14, 2015 at 1:16am UTC
I am at a loss with an assignment. I am supposed to read from a text file, with an input of something like this: alphaproleone,stroke,42 1 and Store it into an array of structures and then output it with each word/number starting on a new line. I was hoping anyone could help me out with where to go next. My current code prints out only the first part, and the "a" in alphaproleone is the actual number "21".
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <iostream>
#include <string>
#include<fstream>
#include<iomanip>
using namespace std;
typedef struct drugtype
{
string name, target;
int effectiveness, toxicity;
};
const int MAX = 25;
typedef drugtype drug[25];
void file_input(int & numitems, drugtype drug);
int main()
{
int numitems = 0;
drugtype drug;
file_input(numitems, drug);
;return 0;
}
void file_input(int & numitems, drugtype drug)
{
ifstream f1;
string txt, junk;
numitems = 0;
cout << "Enter the name of the file:" ;
getline(cin, txt);
f1.open(txt);
if (f1.fail())
{
cout << "File load fail." ;
exit(1);
}
getline(f1, drug.name, ',' );
while ((!f1.fail()) && (numitems < MAX))
{
f1 >> drug.name[numitems];
}
getline(f1, drug.target, ',' );
while ((!f1.fail()) && (numitems < MAX))
{
f1 >> drug.target[numitems];
}
cout << drug.name << endl;
}
Last edited on Feb 14, 2015 at 2:17am UTC
Feb 14, 2015 at 1:17am UTC
You forgot to ask a question.
Please edit your post and make sure your code is [co de]between code tags[/code] so that it has syntax highlighting, indentation, and line numbers.
Topic archived. No new replies allowed.