I am trying to read from a text file (attached) i want to read each value into a structure but for some reason when i read in the first value of the text file,an integer ID number, it doesnt read it in correctly it has some other number. The rest of the file reads in correctly apart from this ID number.
If someone could have alook at the code and point out any errors i would be greatful!
Thanks
The Program Code
Code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
const int hashSIZE = 50;
const int emptyFLAG = 0;
const int delStock = -1;
const int notFound = -1;
struct StockDetails
{
int intCatNumber;
char strDescription[30];
int intPrice;
int intquantity;
};
struct StockTable
{
int count;
StockDetails list[hashSIZE];
};
void createTable(StockTable&);
void fillTable(StockTable&);
void GetOption(StockTable&,int);
inline int hashKey(int key){ return key % hashSIZE;}
void DisplayMenu();
void ClearScreen();
void ClearCIN();
void main()
{
int intSelection;
StockTable p;
createTable(p);
fillTable(p);
DisplayMenu();
cin>>intSelection;
// Check to see if the number is right
while (intSelection > 6)
{
ClearCIN();
cout<<"Incorrect Selection Number, Please Select Again: ";
cin>>intSelection;
}
GetOption(p,intSelection);
}
void DisplayMenu()
{
ClearScreen();
cout<<"\n\t1:\tView Stock Table\n\t2:\tSearch Stock Table\n";
cout<<"\t3:\tAdd to Stock Table\n\t4:\tEdit Stock Item";
cout<<"\n\t5:\tView Stock Item\n\t6:\tQuit";
cout<<"\n\nPlease select one of the above options: ";
};
void GetOption(StockTable& p, int intSelection)
{
// Get the option the user has selected and go to
// the right function
switch (intSelection)
{
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
break;
case 6 :
break;
default :
// OK, if they see this message it means i have really
// messed up in my coding somewhere
cout<<"\n\nWell there is a serious F**K up somewhere because you\n";
cout<<"got passed all my error checking :p\n\n";
}
}
void createTable(StockTable& p)
{
p.count = 0;
for(int i=0;i<hashSIZE;i++)
{
p.list[i].intCatNumber = 0;
}
}
void fillTable(StockTable& p)
{
FILE* StockData;
int tmpAmount,i;
int tmpID;
char tmpDes[30];
float tmpPrice;
i = 0;
StockData = fopen("stock.txt","rt");
if(!StockData)
{
cout<<"Error opening file...";
exit(1);
}
cout<<"Creating table...\n";
while (fscanf(StockData, "%d\t%s\t%f\t%d", &tmpID, &tmpDes,&tmpPrice,&tmpAmount) == 4)
{
p.list[i].intCatNumber = tmpID;
p.list[i].strDescription = tmpDes;
p.list[i].intPrice = tmpPrice;
p.list[i].intquantity = tmpAmount;
i++;
}
fclose(StockData);
}
void ClearScreen()
{
for (int i=0;i<30;i++)
{
cout<<'\n';
}
}
void ClearCIN()
{
cin.clear();
cin.ignore(1000,'\n');
}