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.
#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'
}
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;
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'
}