My assignment this week deals with using a struct. I think I can do that part. What I'm having trouble with is reading the file into each variable.
The first item in the file is text with spaces. The next items are numeric and need to go into other variables, but the entire text (with spaces between words & not necessarily the spaces between the text & the first number) has to go into one string variable (for each line). Then the digits on that line are read into the rest of the variables.
I can get it to work except that the first digit isn't read into the first numeric variable.
Can you tell me a better (or the correct) way of doing this?
int i = 0;
while (inFile.good() ){
//read a line...but we don't want the whole line, just the non-digit part
char ch = inFile.get();
int j = 0; //on each line... NO NO NO THIS IS WRONG!!!!
while(isalpha(ch)|| ch ==' '){
items[i].name[j] = ch;
j++;
ch = inFile.get();
}
//NOPE! Doesn't add 1st digit... because it's stuck in 'ch' b/c of inFile.get(); ?
inFile >> items[i].itemNo >> items[i].qty >> items[i].price >> items[i].safe;
i++;
}
//I tried these things & it did not work:
// getline(inFile, items[i].name); //NO!!! Don't want the whole line, just the alpha part!
// inFile >> items[i].name; // This only inputs ONE word, not the whole name
The contents of the text file:
Wong Batts 98723 243 6.45 200
Widgets No Two 83209 990 17.50 800
HumpBack Whale Songs 74329 42 23.70 50
Frozen Ice Cubes 73922 100 0.15 150
Canned Solar Winds 23923 12 850.00 6
Sented Toe Jamm 18492 14 0.50 20
Backwards Left Turns 87293 5 14.95 12
Meals for Up Chuck 62042 20 16.50 24