Function Help

Hello! So here are the code instructions:

In this exercise, you will write a function getIdFromFile that takes three parameters (a C++ string representing a filename, an istream, and an ostream) and returns an int. Your function should open the file with that name, which will contain up to 1000 unique usernames and ID numbers in this text-based format:
jblack 179
kdavid7 230
sclaus 12
...
Your program should repeatedly read a username from the istream parameter, look up the corresponding ID number in the file, and print that ID number to the ostream on a line by itself (with no other whitespace). If there is no such username, print "error" on a line by itself. Keep reading usernames until there are none left (that is, until the istream has reached eof), and return 0. If you can not open the file, return -1.


I understand the parameter part, but from there on it gets a bit confusing for me. How would I implement the specific format for the 1000 unique usernames? And how would I get it to read the username then look up the corresponding ID number?
How would I implement the specific format for the 1000 unique usernames?


I am bit confused as to what you are asking here. Could you expand on this a bit more?

And how would I get it to read the username then look up the corresponding ID number?


Ok lets walk through this step by step.

The username will be repeatedly entered through istream object that gets passed into the function.

So once you have a username from the istream object you will want to search the entire document until your find that specified username (Or print error if you don't).

This is quite easy to do since all usernames are on their own seperate lines and are right at the front of each line. So all you need to do is read up to the first white space of the line into a variable to get the username.

Then we need to test that variable against the username you got from the istream object.

Finally if they match you print the corresponding ID number this will be just like with the username all you need to do is read up to the next white space.

So if that username on the line matches the istream username you would repeat the whole process again for the next istream username until you hit eof.

And if it doesn't match you just move onto the next line of the document and so on until you either find a match or hit the end of the document (Which you would print"Error").

Now remember this is just a broad overview of how it might be done. That is the beauty of programming there are many ways to accomplish a task each with it's pros and cons. In my mind non of them are wrong so to speak.

For example you could read the file line by line or word by word to get the usernames to test and the corresponding ID's. Or a better way would be to read the whole file in the beginning of the function and store it in a container that you can then use to test usernames. The choice is up to you. Hope this helps a bit.
Last edited on
Topic archived. No new replies allowed.