input from file to array scrambling

I am trying to pull information from a file. A list of items and details about them.
The first item with details loads fine, the second item to the last becomes scrambled.
(The list of includes are what is running in my full program.)

style2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Blue Gift Card
.20 .25 .30 111 112 113 0 1 2
Purple Gift Card
.20 .25 .30 111 112 113 0 1 2
Brown Gift Card
.20 .25 .30 111 112 113 0 1 2
Aqua Gift Card
.20 .25 .30 111 112 113 0 1 2
Red Gift Card
.20 .25 .30 111 112 113 0 1 2
Green Gift Card
.20 .25 .30 111 112 113 0 1 2
Yellow Gift Card
.20 .25 .30 111 112 113 0 1 2
Tan Gift Card
.20 .25 .30 111 112 113 0 1 2
Grey Gift Card
.20 .25 .30 111 112 113 0 1 2
Black Gift Card
.20 .25 .30 111 112 113 0 1 2


The piece of code that is having the issue. I implemented cout's after each input from the file to show what it is reading as it works.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <ctime>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <time.h>
using namespace std;

int main ()
{
	string style2[10];
	double SWprice[10][3];
	int SWsold[10][3];
	int SWremaining[1][3];
	string SWinclude[10][2];
	int SWtoBuy[10];
	

	ifstream inSW;					
	inSW.open("style2.txt");		
	for (int x=0; x<10; x++) {
		getline(inSW, style2[x]);
		cout << style2[x] << endl;
		inSW >> SWprice[x][0];
		cout << SWprice[x][0] << endl;
		inSW >> SWprice[x][1];
		cout << SWprice[x][1] << endl;
		inSW >> SWprice[x][2];
		cout << SWprice[x][2] << endl;
		inSW >> SWremaining[x][0];
		cout << SWremaining[x][0] << endl;
		inSW >> SWremaining[x][1];
		cout << SWremaining[x][1] << endl;
		inSW >> SWremaining[x][2];
		cout << SWremaining[x][2] << endl;
		inSW >> SWsold[x][0];
		cout << SWsold[x][0] << endl;
		inSW >> SWsold[x][1];
		cout << SWsold[x][1] << endl;
		inSW >> SWsold[x][2];
		cout << SWsold[x][2] << endl;
		//SWinclude[x][0] = "(N)";
		//cout << SWinclude[x][0] << endl;
		//SWinclude[x][1] = "(N)";
		//cout << SWinclude[x][1] << endl;
		//SWtoBuy[x] = 0;
		//cout << SWtoBuy[x] << endl;
		}
	inSW.close();


	cout << "\n\nFin." << endl;
	system("pause");
	return 0;
}


I have tried putting the variables as strings, the programs works as far reading all of the first item with detail, then ends with:
Purple
Gift
Card
Then the program freezes and a message comes up to Break or Continue.

The output show like the style2.txt, but straight down. I have tried putting the information in the file on separate lines, same thing.
I copied all your code into VS, copied your text file, and I got the same result.

The problem is that you're not advancing to the next line in the file after you read the first set of item/details. All you have to do is a dummy 'getline' at the end of each loop iteration:

1
2
3
4
5
6
7
string temp;
for(...)
{
...
inSW >> SWsold[x][2];
getline(inSW, temp);
}


It worked fine for me after I added that. There's probably a way to do it without having to worry about the temporary string variable too...

Hope it helps.
It works, Thanks!
Topic archived. No new replies allowed.