trouble reading file into an array

This is a struct array and when I try to print out the data stored in it I get the first bookArray[i].item_name and the rest is just repeating numbers. it is TAB delim and since I have to do calculations on price and quantity I had to make those int and double types. When they were string types they read in fine and the output was good. This problem started when I changed them from string to numbers. The function that reads the information into the array is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 int i = 0;
	  while(!infile.eof() && i < MAX_BOOKS)
		 {
				getline(infile,bookArray[i].item_name, '\t');
				getline(infile,bookArray[i].listing_id, '\t');
				getline(infile,bookArray[i].seller_sku), '\t';
				infile >> bookArray[i].price, '\t';
				infile >> bookArray[i].quantity, '\t';
				getline(infile,bookArray[i].open_date, '\t');
				getline(infile,bookArray[i].item_note, '\t' );
				getline(infile,bookArray[i].item_condition, '\t');
				getline(infile,bookArray[i].product_id,'\t');
				getline(infile,bookArray[i].market, '\n');

				i++;
		 }


a sample of the file being read is:

1
2
3
4
5
item-name	listing-id	seller-sku	price	quantity	open-date	item-note	item-condition	product-id	market
"The Voices of Marrakesh [LARGE PRINT]  by Canetti, Eilas"	0327R058051	14-501099	299.38	2	2004-03-27 10:33:21 PST		11	1852900040	amazon
Towards Prolongation of the Healthy Life Span: Practical Approaches to...	0327R604577	14-311708	216.65	1	2004-03-27 10:30:51 PST		11	157331109X	alibris
Career Information Center  by	0327J610011	17-503207	205.6	2	2004-03-27 11:14:31 PST		11	28974522	half
"Heads of State and Government [Hardcover]  by Da Graca, John"	0624R310573	74-500122	164.13	1	2004-06-24 06:40:18 PST	Bamm! This Is A Great Book!	11	1561592692	ebay


and a sample of the output looks like this:

1
2
3
4
5
6
7
8
"The Voices of Marrakesh [LARGE PRINT]  by Canetti, Eilas"	                            -9.25596e+061	-858993460	                    	                  	
	                                                                                       -9.25596e+061	-858993460	                    	                  	
	                                                                                       -9.25596e+061	-858993460	                    	                  	
	                                                                                       -9.25596e+061	-858993460	                    	                  	
	                                                                                       -9.25596e+061	-858993460	                    	                  	
	                                                                                       -9.25596e+061	-858993460	                    	                  	
	                                                                                       -9.25596e+061	-858993460	                    	                  	
	                                                                                       -9.25596e+061	-858993460	


output is supposed to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
market                     name                     street-address                     city          state   country
_______________________________________________________________________________________________________________________________________________________________________________
amazon       Amazon Corporation                       345 Bezo Ave               Sioux City             IA       USA



item-name                                                                                     price       quantity    product-id       market
_______________________________________________________________________________________________________________________________________________________________________________

"The Voices of Marrakesh [LARGE PRINT]  by Canetti, Eilas"                                   299.38        2         1852900040         amazon
"College Basketball's National Championships [Hardcover]  by Brenner, Morgan G."              138.52        1         081083474X         amazon
"Littleton Waller Tazewell  by Peterson, Norma Lois"                                          104.62        5         081390983X         amazon
The British Marxist Historians: An Introductory Analysis [Hardcover]  by Kaye...               94.49         3          312127332          amazon
Ancient China [Hardcover]  by Scarpari                                                         76.52         2         8880954474         amazon
McSe Windows 2000 Server Training Pack: (Exam 70-215) [Paperback]  by Syngress                 72.96         2         007222245X         amazon


and the output function looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
	  for (int i = 0; i < 100; i++)
		 {

				outfile << bookArray[i].item_name << '\t'
						<< setw(100 - bookArray[i].item_name.size()) << '\t'
						<< bookArray[i].price << '\t'
						<< setw(10)  << '\t'    
						<< bookArray[i].quantity << '\t'
						<< bookArray[i].product_id << '\t'
						<< bookArray[i].market<< '\t' << endl;
				
				
		 }
Last edited on
If reading from the file as a string worked, could you read from the file as string and then convert the string to int/double before calculation?

x = atoi(line.c_str()); // for int, atof(line.c_str()) for double
infile >> bookArray[i].price, '\t'; It doesn do what you think, and it doesn't skips \t symbol. All getlines after that will be messed up. It is better to not mix up stream extraction and getlines unless you exactly know how it is works.
Carm. I'm going to try to do what you are sugesting now with this code:

1
2
3
4
5
6
7
8
9
string Text = "456"; // string containing the number

int Result;          //number which will contain the result

istringstream convert(Text); // stringstream used for the conversion constructed with the contents of 'Text' 
                             // ie: the stream will start containing the characters of 'Text'

if ( !(convert >> Result) ) //give the value to 'Result' using the characters in the stream
    Result = 0; 


MiiNiPaa. I am interested in how you would code it?
Probably get it to stringstream and convert from it
Topic archived. No new replies allowed.