Entries impossible (names & phone numbers)

Write your question here.I don't find my question in the beginners forum. Why ? I had a reply but it was not of any importance. I repeat here my question. After the Stroustrup's code example was ran, I see this on my screen:
{one 1}
{two 2}
{three 3}
{four 4}
{five 5}
{six 6}
{six 6}
{two 2}
{three 3}
{four 4}
{five 5}
{six 6}
four 4
three 3
But here I am unable to enter names or phone numbers. Why ? Is a second file missing ? Many thanks.

Please show the code for your program. It's impossible to answer unless you give more information.
Here is the source code:

/*phone_book.cpp*/

#include<list>
#include<iostream>
#include<string>
using namespace std;

struct Entry {
string name;
int number;
Entry(const string& n, int i) :name(n), number(i) { }
};

list<Entry> phone_book;

void print_entries()
/*
this kind of function should use a parameter,
rather then a global name
*/
{
typedef list<Entry>::const_iterator LI;

for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
const Entry& e = *i; // reference used as shorthand
cout << '{' << e.name << ' ' << e.number << "}\n";
}
}

void print_entry(const string& s)
/*
Is this the right treatment of a string not found?
*/
{
typedef list<Entry>::const_iterator LI;
// 13/
for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
const Entry& e = *i; // reference used as shorthand
if (s == e.name) {
cout << e.name << ' ' << e.number << '\n';
return;
}
}
}

void f(const Entry& e, list<Entry>::iterator i, list<Entry>::iterator p)
/*
just some nonsense code
*/
{
phone_book.push_front(e); // add at beginning
phone_book.push_back(e); // add at end
phone_book.insert(i,e); // add before the element referred to by `i'

phone_book.erase(p); // remove the element referred to by `p'
}

int main()
{

phone_book.push_back(Entry("one",1));
phone_book.push_back(Entry("two",2));
phone_book.push_back(Entry("three",3));
phone_book.push_back(Entry("four",4));
phone_book.push_back(Entry("five",5));
Entry six("six",6);
print_entries();
f(six,phone_book.begin(),phone_book.begin());
print_entries();
print_entry("four");
print_entry("seven");
print_entry("three");
} Thanks again
Entering phone numbers was probably left as an exercise for the reader.
Why don't you implement it?
Hi, I think you are right. Thank you very much. Now I'll try to complete the "just some nonsense code". Almost a month later I can give the new code that accepts leading zero and 13 digit phone numbers. Here is the new code: /*phone_book.cpp*/
#include<list>
#include<iostream>
#include<string>
using namespace std;

struct Entry {
string name;
string number;
int num;
Entry(const string& n, const string& j,int i) :name(n), number(j),num(i) { }
};

list<Entry> phone_book;

void print_entries()
/*
this kind of function should use a parameter,
rather then a global name
*/
{
typedef list<Entry>::const_iterator LI;

for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {
const Entry& e = *i; // reference used as shorthand
cout << '{' << e.name << ' ' << e.number << "}\n";
}
cout <<endl;
}
//
void print_entry(const string& s)
/*
Is this the right treatment of a string not found?
*/
{
typedef list<Entry>::const_iterator LI; // 13/

for (LI i = phone_book.begin(); i != phone_book.end(); ++i) {

const Entry& e = *i; // reference used as shorthand
if (s == e.name) {
cout << e.name << ' ' << e.number << '\n';
return;
}
cout <<endl;
}
}

void f(const Entry& e, list<Entry>::iterator i, list<Entry>::iterator p)
/*
just some nonsense code
*/
{
phone_book.push_front(e); // add at beginning
phone_book.push_back(e); // add at end
phone_book.insert(i,e); // add before the element referred to by `i'

phone_book.erase(p); // remove the element referred to by `p'
}

int main()
{

phone_book.push_back(Entry("DUMAS","0659065731",1));
phone_book.push_back(Entry("aubertin","+33659065731",2));
phone_book.push_back(Entry("CCCCCC","+335434763",3));
phone_book.push_back(Entry("DDDDD","045438976",4));
phone_book.push_back(Entry("five","0654387655",5));
Entry six("six","6",6);
print_entries();
f(six,phone_book.begin(),phone_book.begin());
print_entries();
print_entry("DDDDD");
print_entry("seven");
print_entry("three");
}
Last edited on
Topic archived. No new replies allowed.