Structs
Apr 28, 2012 at 6:54pm UTC
Hello , I'm having trouble outputting my text file. The program does function properly, but I'm trying to output the data exactly the way it is in the text file.
This Is cpp file
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
#include<iostream>
#include "header.h"
const int SIZE=20;
void getFile(itemStruct inventory[], const int size);
void printFile(itemStruct inventory[], const int size);
using namespace std;
int main ()
{
itemStruct inventoryArray[SIZE];
getFile(inventoryArray, SIZE);
printFile(inventoryArray, SIZE);
cout<<endl;
system("pause" );
return 0;
}
void getFile(itemStruct inventory[], const int size)
{
int i , j; // counters
int tmpInt;
ifstream inFile;
inFile.open("numbers.txt" );
if (!inFile)
{
cout<<"(getFile) Error opening " << endl;
}
for ( i = 0; i < size; i++)
{
inFile>>inventory[i].numItems;
inFile>>inventory[i].idString;
inFile>>inventory[i].unitSold;
inFile>>inventory[i].unitsRemain;
}
inFile.close();
}
// header
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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct itemStruct
{
int numItems;
string idString;
int unitSold;
int unitsRemain;
};
void printFile(itemStruct inventory[], const int size)
{
for ( int i = 0; i < size; i++)
{
cout<<inventory[i].numItems<<" " ;
cout<<inventory[i].idString<<" " ;
cout<<inventory[i].unitSold<<" " ;
cout<<inventory[i].unitsRemain<<endl;
}
}
Apr 28, 2012 at 6:56pm UTC
this is my txt file.
20
619847GBE 641 998
418712IMB 107 867
227451GEM 789 181
981836KEA 747 171
986516IGU 303 71
501024BMU 895 743
455559SKK 764 234
706756EUU 687 730
500635MIS 841 670
341955IUS 32 907
791357GEK 391 284
933292AMK 581 901
929002IKA 20 177
474556IBK 532 537
342188MKI 649 367
359989USA 934 972
529875KIB 653 655
706784GGS 596 345
764956SEB 839 304
105170KKU 613 130
Topic archived. No new replies allowed.