Issue getting information from line into array of structs

I will say up front this an assignment for my second C++ class. Basically I have to open a text file in the program and read each line that is in the following format.The text file is then read into an array of structs. I am able to successfully open the file and use getline() to get the first line as a test. However I am not able to read the pieces into the struct properly. I am sure it is something simple I am missing. I have edited my code to only attempt the first line so the loops don't actually run past the first time. Once I can get the first line read in I can move on to using the loop properly.The output is also for testing purposes only. Thanks for any help you can provide in advance.

Sample text file first line.
"974 Headlight_LoBeam 6 12.87 5 3"

My code:
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
67
68
69
70
71
72
73
74
75
/*
 * File:   main.cpp
 * Author: Rob
 *
 * Created on September 1, 2011, 5:19 PM
 */

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;


/*
 *
 */
int main() {
 int const MAXINV = 5;
 string line;
   struct inventory
    {
        double partnumber; //part number
        string descript;
        int numinstock;
        float value;
        int totalvalue;
        int reordernum;
    }inventory[MAXINV]; //declares the array of structs with the max # allowed

   //variable declarations
    int counter = 0;
         //declare and open the file containing
     //inventory information
     ifstream myfile;
     myfile.open("inv.txt");
  if (myfile.is_open())
  { getline (myfile,line);
      cout << line << endl;
    cout << "File successfully opened" <<endl;
  }
  else
  {
    cout << "Error opening file" << endl;
  }

     while(counter <=0){
        myfile>> inventory[counter].partnumber;
        myfile>> inventory[counter].descript;
        myfile>> inventory[counter].numinstock;
        myfile>> inventory[counter].value;
        myfile>> inventory[counter].totalvalue;
        myfile>> inventory[counter].reordernum;
        cout <<"Counter = "<<counter<<endl;
                counter++;
                 myfile.close();
     }

     
     int counter1 = 0;
     while(counter1 <= 0){
     cout <<"Part # "<<inventory[counter].partnumber<< endl;
     cout <<"Descript "<< inventory[counter].descript<< endl;
     cout <<"Number in stock "<< inventory[counter].numinstock<< endl;
     cout <<"Value "<< inventory[counter].value<< endl;
     cout <<"Total Value "<< inventory[counter].totalvalue<< endl;
     cout <<"Reorder Number "<< inventory[counter].reordernum<< endl;
     cout <<"Counter1 is "<<counter1<<endl;
     counter1++;

    }
    
     return 0;
}


And here is the current output
1
2
3
4
5
6
7
8
9
10
11
12
974 Headlight_LoBeam 6 12.87 5 3
File successfully opened
Counter = 0
Part # 6.89798e-308
Descript
Number in stock 1628496224
Value 1.73395e+20
Total Value 1629546280
Reorder Number 2673928
Counter1 is 0
Press [Enter] to close the terminal ...
I think you're close. I typically read a line with getline:

1
2
3
4
5

char buffer[ 100 ];

myfile.getline( buffer, sizeof( buffer ) - 1 );


Then I parse each line into its proper location within the array. I think you'll have better control over the type of data you're putting in.

Good luck!
That would be ideal but unfortunately I am required to do it this way for the assignment. I don't get to choose to use getline. I only used it for testing purposes to make sure I was actually reading the text file properly and was able to get the info.
Well I found that part of my problem was that I was using counter instead of counter1 in the second loop that cout's the values. I also was using getline() and then not zeroing getline() out so that it would reset. I removed the getline() portion and fixed the counter to be counter1. I am not able to read the first line of the text file into the first struct in the array but after that the input is again looking like it was in the previous example. The following is the code followed by the current ouput. Thanks for any help in advance.

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
67
68
69
70
71
72
73
74
75
/*
 * File:   main.cpp
 * Author: Rob
 *
 * Created on September 1, 2011, 5:19 PM
 */

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;


/*
 *
 */
int main() {
 int const MAXINV = 5;
 struct inventoryinfo
    {
        double partnumber; //part number
        string descript;
        int numinstock;
        float valueeach;
        int reorderlev;
        int reordernum;
    };
    inventoryinfo inventory[MAXINV];//declares the array of structs with the max # allowed

   //variable declarations
    int counter;
             //declare and open the file containing
     //inventory information
     ifstream MyFile;
     MyFile.open("inv.txt");
  if (MyFile.is_open())
  { 
    cout << "File successfully opened" <<endl;
  }
  else
  {
    cout << "Error opening file" << endl;
  }
     counter = 0;
     
     while(counter <= 1){
        MyFile >> inventory[counter].partnumber;
        MyFile >> inventory[counter].descript;
        MyFile >> inventory[counter].numinstock;
        MyFile >> inventory[counter].valueeach;
        MyFile >> inventory[counter].reorderlev;
        MyFile >> inventory[counter].reordernum;
        cout <<"Counter = "<<counter<<endl;
                counter++;
                MyFile.close();
         }

     
     int counter1 = 0;
     while(counter1 <= 3){
     cout <<"Part # "<< inventory[counter1].partnumber<< endl;
     cout <<"Descript "<< inventory[counter1].descript<< endl;
     cout <<"Number in stock "<< inventory[counter1].numinstock<< endl;
     cout <<"Value Each "<< inventory[counter1].valueeach<< endl;
     cout <<"Reorder Level "<< inventory[counter1].reorderlev<< endl;
     cout <<"Reorder Number "<< inventory[counter1].reordernum<< endl;
     cout <<"Counter1 is "<< counter1<<endl;
     counter1++;

    }
    
     return 0;
}


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
File successfully opened
Counter = 0
Counter = 1
Part # 974
Descript Headlight_LoBeam
Number in stock 6
Value Each 12.87
Reorder Level 5
Reorder Number 3
Counter1 is 0
Part # 8.48798e-314
Descript
Number in stock 1629343944
Value Each 8.96831e-44
Reorder Level 2
Reorder Number 2673976
Counter1 is 1
Part # 6.54969e+159
Descript
Number in stock 1627536137
Value Each 1.78191e+20
Reorder Level 2673892
Reorder Number 120
Counter1 is 2
Part # 1.27333e-313
Descript
Number in stock 1628480096
Value Each 0
Reorder Level 1629130311
Reorder Number -2
Counter1 is 3
Press [Enter] to close the terminal ...


I think the problem is that you close the file after only reading one item in the loop. Also you only attempt to read in 2 items but you are trying to display 4 items:
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
    while(counter <= 1){ // This will only read 2 items...
        MyFile >> inventory[counter].partnumber;
        MyFile >> inventory[counter].descript;
        MyFile >> inventory[counter].numinstock;
        MyFile >> inventory[counter].valueeach;
        MyFile >> inventory[counter].reorderlev;
        MyFile >> inventory[counter].reordernum;
        cout <<"Counter = "<<counter<<endl;
                counter++;
               // Why do you close the file before you finish reading all the items?
               // MyFile.close();
         }
      MyFile.close(); // Now close the file after you have finished with it


     int counter1 = 0;
     while(counter1 <= 3){ // you are displaying 4 items but you only read in 2 items
     cout <<"Part # "<< inventory[counter1].partnumber<< endl;
     cout <<"Descript "<< inventory[counter1].descript<< endl;
     cout <<"Number in stock "<< inventory[counter1].numinstock<< endl;
     cout <<"Value Each "<< inventory[counter1].valueeach<< endl;
     cout <<"Reorder Level "<< inventory[counter1].reorderlev<< endl;
     cout <<"Reorder Number "<< inventory[counter1].reordernum<< endl;
     cout <<"Counter1 is "<< counter1<<endl;
     counter1++;

    }
Hmm I apologize I have two different machines I am working on and I failed to update the second one to the newest version of code. the new version has the file being closed after the loop is run and also has me reading in 4 items and outputting 4 as well. Here is the code with the output.

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
67
68
69
70
71
72
73
74
75
76
/*
 * File:   main.cpp
 * Author: Rob
 *
 * Created on September 1, 2011, 5:19 PM
 */

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;


/*
 *
 */
int main() {
 int const MAXINV = 5;
 string line;
 struct inventoryinfo
    {
        double partnumber; //part number
        string descript;
        int numinstock;
        float valueeach;
        int reorderlev;
        int reordernum;
    };
    inventoryinfo inventory[MAXINV];//declares the array of structs with the max # allowed

   //variable declarations
    int counter;
             //declare and open the file containing
     //inventory information
     ifstream MyFile;
     MyFile.open("inv.txt");
  if (MyFile.is_open())
  { 
    cout << "File successfully opened" <<endl;
  }
  else
  {
    cout << "Error opening file" << endl;
  }
     counter = 0;
     
     while(counter <= 3){
        MyFile >> inventory[counter].partnumber;
        MyFile >> inventory[counter].descript;
        MyFile >> inventory[counter].numinstock;
        MyFile >> inventory[counter].valueeach;
        MyFile >> inventory[counter].reorderlev;
        MyFile >> inventory[counter].reordernum;
        cout <<"Counter = "<<counter<<endl;
                counter++;
                
         }
         MyFile.close();
     
     int counter1 = 0;
     while(counter1 <= 3){
     cout <<"Part # "<< inventory[counter1].partnumber<< endl;
     cout <<"Descript "<< inventory[counter1].descript<< endl;
     cout <<"Number in stock "<< inventory[counter1].numinstock<< endl;
     cout <<"Value Each "<< inventory[counter1].valueeach<< endl;
     cout <<"Reorder Level "<< inventory[counter1].reorderlev<< endl;
     cout <<"Reorder Number "<< inventory[counter1].reordernum<< endl;
     cout <<"Counter1 is "<< counter1<<endl;
     counter1++;

    }
    
     return 0;
}


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
File successfully opened
Counter = 0
Counter = 1
Counter = 2
Counter = 3
Part # 974
Descript Headlight_LoBeam
Number in stock 6
Value Each 12.87
Reorder Level 5
Reorder Number 3
Counter1 is 0
Part # 5
Descript 027
Number in stock 1628438957
Value Each 0
Reorder Level 4
Reorder Number 1629130428
Counter1 is 1
Part # 4.24399e-314
Descript 
Number in stock 1628419308
Value Each 1.71483e+20
Reorder Level 1629343944
Reorder Number 2
Counter1 is 2
Part # 6.89783e-308
Descript 
Number in stock 2673928
Value Each 3.74805e-39
Reorder Level 6
Reorder Number 1628481664
Counter1 is 3
Well your new version works fine for me. Are you sure you re-compiled it properly?
Well I was able to run it on a unix machine just fine after finding that my text file had some erroneous numbers on extra lines. I am still unable to get it to work in netbeans on my windows machine but that is fine. Thanks for your help.
Topic archived. No new replies allowed.