I am working on an assignment that requires me to read in a text file with text that looks like this

974 Headlight_LoBeam 6 12.87 5 3

I am able to open the file successfully and just for testing purposes I am able to use getline() to get the first line of the file. My issue is that when I am trying to get each line and add the data into my struct I am not getting the proper data. I am sure it is something simple that I am missing but I have yet to figure it out. To clarify I cannot use getline() to get the full line and then parse out the info I want into my struct. I have to use the "myfile >>" way of doing this according to my assignment. I do not get any compile errors. Here is the code currently. Currently the while loop is set only to run once to avoid a long list of incorrect data. Once I can get the data into the struct I will change the while loop to run for all the lines in the text file.

/*
 * 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())
  { getline (MyFile,line);
      cout << line << endl;
    cout << "File successfully opened" <<endl;
  }
  else
  {
    cout << "Error opening file" << endl;
  }
     counter = 0;
     
     while(counter <= 0){
        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 <= 0){
     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;
}

Here is the current output:

974 Headlight_LoBeam 6 12.87 5 3
File successfully opened
Counter = 0
Part # 5
Descript 027
Number in stock 2
Value Each 2.8026e-45
Reorder Level 2673832
Reorder Number 1975982290
Counter1 is 0
Press [Enter] to close the terminal ...

Thanks for any help in advance.
-Rob

after you read a line from getline(), you have to reset your file position to
zero back before you reading from the file.
That's why data is not read correctly.

use this std::istream::seekg() function to reset it to zero and try again.
http://www.cplusplus.com/reference/iostream/istream/seekg/

--sandun--

Well I removed the getline() code since I was only using it to make sure I was opening the right file and that I could pull info from the file and the first line gets read in perfectly. However after that the next lines go back to doing the same thing they were before. Here is the new output followed by the edited code.

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 # 6.89798e-308
Descript
Number in stock 1628496224
Value Each 1.73395e+20
Reorder Level 1629546280
Reorder Number 2673928
Counter1 is 1
Part # 4.24531e-314
Descript
Number in stock 2673932
Value Each 3.74693e-39
Reorder Level 1628857696
Reorder Number 1629546280
Counter1 is 2
Part # 1.06646e+256
Descript
Number in stock 1628558736
Value Each 1.68107e+20
Reorder Level 0
Reorder Number 0
Counter1 is 3
Press [Enter] to close the terminal ...
/*
 * 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++;
                
         }

     
     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++;

    }
    MyFile.close();
     return 0;
}

why happening this is when you read one full record , you have to
ignore newline character. Try some code like this.

#define NEWLINE '\n'
static void go_to_next_line(istream &MyFile)
{
  char ch;
  do{
    MyFile.getc(&ch);
  }while ( ch != NEWLINE );
}

and do not call this function when you read the last line.

Like I said the getline() was not actually being used it was just to test to make sure I was pulling info from the correct file and to see that I could pull info from the file. After removing the getline() portion I realized that MyFile.close(); was in the while loop and was closing after the first iteration of the loop. After moving this out of the loop the code works great. Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.