Assigning file input into variables.

Hey guys,

I've been writing a program as I learn to test things which I've learned both in ways described, and to try things that were not stated in a tutorial.

Keep in mind, I'm rather a newbie to programming.

This is a particular function I've been having troubles with.

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
int inventory::loadinv()
{
    ifstream wfile;
    wfile.open("weapons.txt");
    x = 1;
    do
        {

            if(wslt == 1)
                {
                    wfile >> wslt01 >> wclass01 >> wlvl01;
                }
            if(wslt == 2)
                {
                    wfile >> wslt02 >> wclass02 >> wlvl02;
                }
            if(wslt == 3)
                {
                    wfile >> wslt03 >> wclass03 >> wlvl03;
                }
            if(wslt == 4)
                {
                    wfile >> wslt04 >> wclass04 >> wlvl04;
                }
            if(wslt == 5)
                {
                    wfile >> wslt05 >> wclass05 >> wlvl05;
                }
            if(wslt == 6)
                {
                    wfile >> wslt06 >> wclass06 >> wlvl06;
                }
            if(wslt == 7)
                {
                    wfile >> wslt07 >> wclass07 >> wlvl07;
                }
            if(wslt == 8)
                {
                    wfile >> wslt08 >> wclass08 >> wlvl08;
                }
            if(wslt == 9)
                {
                    wfile >> wslt09 >> wclass09 >> wlvl09;
                }
            if(wslt == 10)
                {
                    wfile >> wslt10 >> wclass10 >> wlvl10;
                }
            else
                {
                    wfile.close();
                }
            x++;
        }
    while(x > 0 && x <= 10);

    cout << "Weapons loaded." << endl;
        }


First off, let me state that yes... I know the else statement is redundant. It's merely there to control any errors with the x var.

All variables have been declared in the header file under protected.

All wsltxx variables are int. All wclassxx variables are strings. All wlvlxx variables are int.

The data from weapons.txt -

1
2
3
4
5
6
7
8
9
10
31 dagger  1
31 dagger  2
35 dagger  1
30 dagger  0
30 dagger  0
30 dagger  0
30 dagger  0
30 dagger  0
30 dagger  0
30 dagger  0


The issue :

After running this function, I would run another function that would output the variables in cout :
cout << wslt01 << ' ' << wclass01 << ' ' << wlvl01 << endl;
and the results in the console window would be :
31 0.

Why aren't my other variables being set? Is this a syntax error, or is my rudimentary knowledge of file input and output flawed? If the latter, please elaborate.

Edit : Was mucking around with the code before I posted this, and copy pasted some mistakes. Edited for clarity.
Last edited on
The else statement it is not redundant. In fact it is breaking the input
1
2
3
4
5
6
7
8
            if(wslt == 10)
                {
                    wfile >> wslt10 >> wclass10 >> wlvl10;
                }
            else //1, 2, 3, ..., 9 will enter here
                {
                    wfile.close(); 
                }
So you close the file after the first read.

Check out arrays.
This fixed everything... Thanks!

I actually haven't gotten around to messing with arrays just yet... IIRC Arrays only handle 1 datatype no? (eg. int arrayname[int,int]

where as this particular line of code handles both strings and integers. I mean I guess i could create an array for wsltxx and wlvlx.
Yep, only one type. However you could do something like
1
2
3
4
5
6
struct weapon{
  std::string name;
  int damage;
};

weapon inventory[10];


Or you could handle 3 different arrays.
Topic archived. No new replies allowed.