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"
/*
* File: main.cpp
* Author: Rob
*
* Created on September 1, 2011, 5:19 PM
*/
#include <cstdlib>
#include <iostream>
#include <fstream>
usingnamespace std;
/*
*
*/
int main() {
intconst 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 ...
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.
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:
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.
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 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.