| | |
need help with fstream input files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 12
Reputation:
Solved Threads: 0
my input file looks like this:
112;12
130;30
156;56
the first colum refers to keys, and the other one refers to quantity. im writin a program to search for the key and print out the quantity using hash algorithm. i used structure. so it looks like, lets say,
input.getline(structure.key, sizeof(structure.key),';');
input.getline(structure.quantity, sizeof(structure.quantity));
the problem is its just reading only the first line. and it doesnt read the rest lines at all. wat m i doing wrong???
the rest is the program if u wanna read, its too long.. sorry
//#include <fstream.h>
#include <iomanip>
#include <stdlib.h>
#include <string>
struct hashlist
{
char part [5];
char quantity[3];
};
const int MAX_SIZE = 16;
void buildList (hashlist partList[], int& last);
void searchList (hashlist *partList, int last);
int hashKey(char *key, int last);
int collision(int last, int loc);
int main (void)
{
hashlist partList[MAX_SIZE];
int last;
cout << "HASING ALGORITHM"<<endl;
cout<<"-------------------"<<endl<<endl;
last = MAX_SIZE - 1;
buildList (partList, last);
searchList(partList, last);
return 0;
}
void buildList (hashlist partList[], int& last)
{
ifstream partnumbers;
hashlist listing;
int loc;
int cntCol;
int end;
int i;
partnumbers.open ("hashlist.TXT");
if (!partnumbers)
{
cerr<<"File cant be opened!!!!"<<endl;
abort();
}
for(i=0;i<=last;i++)
partList[i].part[0]='\0';
while (!partnumbers.eof())
{
partnumbers.getline(listing.part, sizeof(listing.part),';');
partnumbers.getline(listing.quantity, sizeof(listing.quantity));
loc=hashKey(listing.part,last);
if(partList[loc].part[0]!='\0')
{
end=last;
cntCol=0;
while(partList[loc].part[0]!='\0'&&cntCol++<=last)
loc=collision(last,loc);
if(partList[loc].part[0]!='\0')
{
cout<<"list is full. not all numbers read";
return;
}
}
partList[loc]=listing;
}
return;
}
int hashKey(char *key, int last)
{
int addr; int i; int keyLen;
keyLen=strlen(key);
addr=0;
for(i=0;i<keyLen;i++)
if(key[i]!=' ')
addr+=key[i];
return(addr % last + 1 );
}
int collision(int last, int loc)
{
return loc<last? ++loc:0;
}
void searchList (hashlist *partList, int last)
{
char schpart[5];
char more;
int maxSrch;
int loc;
int cntCol;
do
{
cout << "Enter the Part number you wish to search: ";
cin >> schpart;
loc=hashKey(schpart,last);
/*if(strcmp(schpart, partList[loc].part)!=0)
{
maxSrch=last;
while(strcmp(schpart,partList[loc].part)!=0 && cntCol++<=maxSrch)
loc=collision(last,loc);
}*/
if(strcmp(schpart, partList[loc].part)==0)
{
cout<<"Inventory Item: "<<partList[loc].part<<" ("<<loc<<")"<<endl;
cout<<"Quantity sold: "<<partList[loc].quantity<<endl;
}
else
cout<<schpart<<" not found"<<endl;
cout<<"Would you like to try another part number <Y/N>???"<<endl;
cin>>more;
}while(more=='Y'||more=='y');
}
112;12
130;30
156;56
the first colum refers to keys, and the other one refers to quantity. im writin a program to search for the key and print out the quantity using hash algorithm. i used structure. so it looks like, lets say,
input.getline(structure.key, sizeof(structure.key),';');
input.getline(structure.quantity, sizeof(structure.quantity));
the problem is its just reading only the first line. and it doesnt read the rest lines at all. wat m i doing wrong???
the rest is the program if u wanna read, its too long.. sorry
//#include <fstream.h>
#include <iomanip>
#include <stdlib.h>
#include <string>
struct hashlist
{
char part [5];
char quantity[3];
};
const int MAX_SIZE = 16;
void buildList (hashlist partList[], int& last);
void searchList (hashlist *partList, int last);
int hashKey(char *key, int last);
int collision(int last, int loc);
int main (void)
{
hashlist partList[MAX_SIZE];
int last;
cout << "HASING ALGORITHM"<<endl;
cout<<"-------------------"<<endl<<endl;
last = MAX_SIZE - 1;
buildList (partList, last);
searchList(partList, last);
return 0;
}
void buildList (hashlist partList[], int& last)
{
ifstream partnumbers;
hashlist listing;
int loc;
int cntCol;
int end;
int i;
partnumbers.open ("hashlist.TXT");
if (!partnumbers)
{
cerr<<"File cant be opened!!!!"<<endl;
abort();
}
for(i=0;i<=last;i++)
partList[i].part[0]='\0';
while (!partnumbers.eof())
{
partnumbers.getline(listing.part, sizeof(listing.part),';');
partnumbers.getline(listing.quantity, sizeof(listing.quantity));
loc=hashKey(listing.part,last);
if(partList[loc].part[0]!='\0')
{
end=last;
cntCol=0;
while(partList[loc].part[0]!='\0'&&cntCol++<=last)
loc=collision(last,loc);
if(partList[loc].part[0]!='\0')
{
cout<<"list is full. not all numbers read";
return;
}
}
partList[loc]=listing;
}
return;
}
int hashKey(char *key, int last)
{
int addr; int i; int keyLen;
keyLen=strlen(key);
addr=0;
for(i=0;i<keyLen;i++)
if(key[i]!=' ')
addr+=key[i];
return(addr % last + 1 );
}
int collision(int last, int loc)
{
return loc<last? ++loc:0;
}
void searchList (hashlist *partList, int last)
{
char schpart[5];
char more;
int maxSrch;
int loc;
int cntCol;
do
{
cout << "Enter the Part number you wish to search: ";
cin >> schpart;
loc=hashKey(schpart,last);
/*if(strcmp(schpart, partList[loc].part)!=0)
{
maxSrch=last;
while(strcmp(schpart,partList[loc].part)!=0 && cntCol++<=maxSrch)
loc=collision(last,loc);
}*/
if(strcmp(schpart, partList[loc].part)==0)
{
cout<<"Inventory Item: "<<partList[loc].part<<" ("<<loc<<")"<<endl;
cout<<"Quantity sold: "<<partList[loc].quantity<<endl;
}
else
cout<<schpart<<" not found"<<endl;
cout<<"Would you like to try another part number <Y/N>???"<<endl;
cin>>more;
}while(more=='Y'||more=='y');
}
Check out the fstream tutorial which covers using structures to store info.It covers databases and a little more.
http://www.daniweb.com/techtalkforums/thread6542.html
http://www.daniweb.com/techtalkforums/thread6542.html
![]() |
Similar Threads
- Problem with getline with input files (C++)
- Reading 2 diffrent input files and outputting to 2 different output files (C++)
- Writing stuff inside files in C++ bloodshed (C++)
- I Little Help with reading Input files (C++)
- TSP read input file problem (C++)
- Merge Two Files (C++)
Other Threads in the C++ Forum
- Previous Thread: emergency!!!!
- Next Thread: Graphics In Pixel: Part II
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





