Entries impossible in a Stoustrup code example

25 views
Skip to first unread message

aubertin.sylvain

unread,
May 24, 2016, 2:24:44 AM5/24/16
to vim_use
The mame of this code is "list". It seems to be incomplete (nowhere a cin instruction). It runs but it doesn't work. May any one help me (I am a beginner).
Here is the 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

Christian Brabandt

unread,
May 24, 2016, 3:22:50 AM5/24/16
to vim...@googlegroups.com
Am 2016-05-24 08:24, schrieb aubertin.sylvain:
> The mame of this code is "list". It seems to be incomplete (nowhere a
> cin instruction). It runs but it doesn't work. May any one help me (I
> am a beginner).

And your problem with Vim is exactly what?


Best,
Christian

Dominique Pellé

unread,
May 24, 2016, 3:28:51 AM5/24/16
to Vim List
aubertin.sylvain <aubertin...@sfr.fr> wrote:

> The mame of this code is "list". It seems to be incomplete (nowhere a cin instruction). It runs but it doesn't work. May any one help me (I am a beginner).
> Here is the code:

This question is completely unrelated to Vim, so please do not
post questions unrelated to vim in this mailing list. You should
instead post c++ questions to a c++ mailing list.

Anyway, your code compiles fine and runs.
It's just a code example. There is no need for std::cin.

$ g++ foo.cpp
$ ./a.out
{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

Regards
Dominique

aubertin.sylvain

unread,
May 24, 2016, 11:30:40 AM5/24/16
to vim_use
Hi Dominique. I tried to do something with the result of the object file, without any success. I should like to have your opinion: is it possible with a new function (2 cin inside) to make this program complete ??
I tried many times, but the compiler returns me a lot of errors. Thanks again.

Ben Fritz

unread,
May 24, 2016, 11:53:15 AM5/24/16
to vim_use
On Tuesday, May 24, 2016 at 10:30:40 AM UTC-5, aubertin.sylvain wrote:
> Le mardi 24 mai 2016 08:24:44 UTC+2, aubertin.sylvain a écrit :
> > The mame of this code is "list". It seems to be incomplete (nowhere a cin instruction). It runs but it doesn't work. May any one help me (I am a beginner).
> > Here is the code:
>
>
> Hi Dominique. I tried to do something with the result of the object file, without any success. I should like to have your opinion: is it possible with a new function (2 cin inside) to make this program complete ??
> I tried many times, but the compiler returns me a lot of errors. Thanks again.

This has nothing to do with Vim (the text editor) and should not be discussed further here on the Vim mailing list. Please take your question to a more appropriate forum, perhaps stackoverflow or another stackexchange site, perhaps a c++ mailing list somewhere.

Before you do that, you really really need to come up with a better problem statement. Don't respond here, but when you find an appropriate forum, make sure to define what you want the program to do. As others have pointed out, your program "works" just fine, as in, it does exactly what we'd expect from the code, and prints out a list with no user input.

See http://www.catb.org/~esr/faqs/smart-questions.html for advice on asking questions online. You've violated most of the basics already by posting to the wrong forum without a clear problem statement and ignoring the advice and questions you've already received.
Reply all
Reply to author
Forward
0 new messages