I have this program that is supposed to read part numbers from a text file.
101-110, 301-310, 501-510 and put them into a hash table. It will prompt for one of four algorithms to be used and then continue to load the part numbers. Everything seems to be working okay, but it seems to repeat the last part number 510. Is there something that I need to put at the end of the text file to indicate it is the end of file and to stop. Please be nice - I am just a beginner - had to do quite a bit of reading to get this far
Any suggestions are appreciated.
The best way to read from a file is to use the input gathering function itself as your loop condition. That way when it returns a failure code (supposedly for reaching end-of-file), you break the loop:
I removed your comments because they do not add anything. Comments that say the same thing as the code being commented only serve to make the program harder to read. There's also a good chance that any changes to the code will not be reflected in the comment and the two will disagree. When code and comments disagree, it's customary to see both as incorrect.
After the loop, it's also a good idea to make sure that it was end-of-file that caused it to terminate. That way you can handle real errors:
thanks - I was thinking about it after - and I am only entering 30 part numbers - and the array holds 41 elements...I know its not the best way to do it...but I just changed the value to 30. I think there must be a fancy way to put something in there to know its at the end of the file. But I will worry about that another time.
I have this program that is supposed to read part numbers from a text file.
101-110, 301-310, 501-510 and put them into a hash table. It will prompt for one of four algorithms to be used and then continue to load the part numbers. Everything seems to be working okay, but it seems to repeat the last part number 510. Is there something that I need to put at the end of the text file to indicate it is the end of file and to stop. Please be nice - I am just a beginner - had to do quite a bit of reading to get this far
Any suggestions are appreciated.
This is because when you are reading the numbers from the file you are trying to read 41 characters..but only 31 characters are present in the file.. so it is reading the last cahracter 10 more times.Try to decrease the value of i in the for loop and you will get your desired result.
new to c++
i just need to know what does this line mean?
and why its not working with my compiler?
it is giving an error that
type qualifier 'std' must be a struct or class name
std::ifstream inputfile("part.txt",std::ios::in |std::ios::binary);
if(!inputfile)
{
std::cout<<"Could not open file"<<std::endl;
return 1;
The recommended scheme for File I/O is to create an fstream object, attempt to open a file, perform error checking, and load the file.
The code in question is performing error checking to see if the file was actually opened. It is quite possible that the file may have been moved, deleted, renamed.. or was just never created:
for(i = 0; i < 41; i++) { // as you read each part number in the file inputfile >> PartNumber; // HashAddress function returns array index and it is assigned to variable Index
I am getting an error on this line. Isn't inputfile the part of any of the headers?
C:\Program Files\Microsoft Visual Studio\VC98\Bin\dont.cpp(52) : error C2065: 'inputfile' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\VC98\Bin\dont.cpp(52) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.