Stream functions

I'm trying to write a function that acts as a phonebook; it looks up a person's first and last names and email address when given either the first or last name. Also, one can add entries to the phonebook by using a function called add. Here are more detailed specifications:

411: You are given a file named phonebook that consists of many lines containing three strings:
           lastname firstname emailaddress.

Write a utility program that reads commands (from stdin). Each command has one of two possible forms:
           lookup string
           add lastname firstname emailaddress

In the case of the first command (lookup), the program looks for a line in the phonebook file where either the firstname or lastname matches the string given. If it finds such a line it prints out all three parts of the line, separated by spaces:
           lastname firstname emailaddress

If it does not find a match in the file it prints out the string it was looking for, followed by a colon followed by a space followed by the message "not found":
           string: not found

In the case of the second command (add), the program appends the three strings given to the file phonebook.


Hints and suggestions:

(1) Define and use two functions named lookup and add. When your program reads the string lookup, your lookup function is called; when the program reads the string add, your add function is called. When either of these two functions are called, they then read whatever else is necessary for their command (one string for lookup and three strings -- lastname, firstname, and emailaddress-- for add). And then these functions do... whatever it takes to carry out the command.

(2) When doing a lookup or an add, open up the file phonedir ... carry out the operation .. and then close up the file.






EXAMPLE:

suppose the phonedir file looked like this:
           arnow david arnow@panix.com
           bush george president@whitehouse.gov
           gates bill bill@microsoft.com

here then is a sample session with the program (program output is in bold):
           lookup david
           arnow david arnow@panix.com
           lookup joe
           joe: not found
           add theplumber joe joetheplumber@nowhere.com
           lookup arnow
           arnow david arnow@panix.com
           lookup joe
           theplumber joe joetheplumber@nowhere.com

and the phonedir file would now be:
           arnow david arnow@panix.com
           bush george president@whitehouse.gov
           gates bill bill@microsoft.com
           theplumber joe joetheplumber@nowhere.com

And here's the code I've been using:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void lookup();
void add();
int main(){
string type;
while (cin >> type){
if (type == "lookup")
    lookup();
else
    add();
}
return 0;
}
void lookup()
{ifstream fin;
bool check = false;
string name,firstName,lastName,email;
cin >> name;
fin.open("phonebook");
while (fin >> firstName >> lastName >> email){
if (firstName.compare(name)==0 || lastName.compare(name)==0){
     cout<<firstName<<" "<<lastName<<" "<<email<<endl;
     check=true;
    }
}
if(!check)
     cout << name << ": not found" << endl;
fin.close();
}
void add()
{ofstream fout;
string name;
getline(cin, name);
fout.open("phonebook", ios::app);
fout << name << endl;
fout.close();
}


I keep getting the "not found" result, even if the person's name is in the phonebook. My intention is to have the string comparison part decide whether or not the phonebook has the entry, but somehow it isn't working how I want it to. I know it's kind of a lot of code, but any suggestions are welcome. Thank you in advance.
Last edited on
After line 22 and before line 23, check to see if fin is in a good state.

Not found could just as easily be a result given if the file wasn't able to be opened, which I suspect is the case.
Last edited on
Thank you very much for your response.

I'm trying to use

bool good (fin);

to see if fin is in a good state, and I'm still getting the result as before (not found). I'm pretty sure I'm just not checking whether fin is in a good state correctly, but I'm don't know how else to do that. I guess I just don't understand why the file wouldn't open correctly... I mean, if I open it using

fin.open("phonebook");

then why would it not open?

Thank you again for your help.
1
2
3
4
if ( !fin )
{
       std::cerr << "Unable to open phonebook.\n" ;
}


bool good (fin);... defines a variable of type bool called good and sets it to the value of fin. Merely defining a variable and setting it to the information you want to see isn't enough.
Topic archived. No new replies allowed.