Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

output

0 views
Skip to first unread message

arunix

unread,
Dec 31, 2009, 10:25:07 PM12/31/09
to
hello
here is code from "The C++ Programming langauge" by Bjarne Stroustrup.
the code is working but not showing the output when press ctrl+c it
shows only interrupt and first input.
wht's wrong with this code.
thnaks .
here is the code


#include<iostream>
#include<string>
#include<vector>

using namespace std;

struct Pair{ string name; double val;};
vector<Pair> pairs;
double& value(const string& s)
{
for(unsigned int i=0; i < pairs.size(); i++)
if(s==pairs[i].name) return pairs[i].val;
Pair p={s,0};
pairs.push_back(p);
return pairs[pairs.size()-1].val;
}

int main()
{
string buf;
while(cin>>buf) value(buf)++;
for(vector<Pair> :: const_iterator p= pairs.begin(); p != pairs.end
();++p)
cout <<p->name << " : "<<p->val <<"\n";

return 0;
}

Sam

unread,
Dec 31, 2009, 10:54:52 PM12/31/09
to
arunix writes:

> hello
> here is code from "The C++ Programming langauge" by Bjarne Stroustrup.
> the code is working but not showing the output when press ctrl+c it
> shows only interrupt and first input.
> wht's wrong with this code.

There's nothing wrong with it. Press CTRL-D instead of CTRL-C.

arunix

unread,
Jan 1, 2010, 6:14:09 AM1/1/10
to
Thanks Sam for reply..
but again its not working
it shows like this.
for complie i use this command......

~/data/CPP/SSC++ $ g++ -Wall -Wextra -ansi -pedantic ex-5-4-1.cpp
~/data/CPP/SSC++ $ ./a.out
aa
bb
aa
bb
aa
aa
bb // press Ctrl+D
interrupt
is there something wrong
and i am confused with


while(cin>>buf) value(buf)++;
why increment ?

.........

domachine

unread,
Jan 1, 2010, 7:56:44 AM1/1/10
to

I think you should first understand some basic things of C/C++ before
moving on to topics like vector etc.
The question "why increment ?" tells me that you're a beginner. So
move on slowly.
Start your book from the beginning and try again.

Greetings

Jonathan Lee

unread,
Jan 1, 2010, 2:53:16 PM1/1/10
to
On Jan 1, 6:14 am, arunix <arru...@gmail.com> wrote:
> for complie i use this command......
> ~/data/CPP/SSC++ $ g++ -Wall -Wextra -ansi -pedantic  ex-5-4-1.cpp

That seems ok. Worked for me.

> aa
> bb
> aa
> bb
> aa
> aa
> bb      // press Ctrl+D
> interrupt

Do you mean that you don't get the list of frequencies at
the end? i.e.,
aa: 4
bb: 3

That seems odd... but I would think it has less to do with
your program than your OS, or the particular terminal
application you're using.

> and i am confused with
>
> while(cin>>buf) value(buf)++;
> why increment ?

It's counting the number of times each input string is
typed. "value()" returns a reference to "number of times
'buf' has been typed". Then that number is incremented.

--Jonathan

arunix

unread,
Jan 2, 2010, 12:11:48 AM1/2/10
to
On Jan 1, 5:56 pm, domachine <dominik.burgdoer...@googlemail.com>
wrote:

yes i did it....

arunix

unread,
Jan 2, 2010, 12:14:21 AM1/2/10
to

> Do you mean that you don't get the list of frequencies at
> the end? i.e.,
> aa: 4
> bb: 3

yes

> That seems odd... but I would think it has less to do with
> your program than your OS, or the particular terminal
> application you're using.

using gcc 4.4.2

> It's counting the number of times each input string is
> typed. "value()" returns a reference to "number of times
> 'buf' has been typed". Then that number is incremented.

Thanks...

James Kanze

unread,
Jan 2, 2010, 1:55:32 PM1/2/10
to
On Jan 1, 3:54 am, Sam <s...@email-scan.com> wrote:
> arunix writes:

Or some other system specific sequence. (Under Windows, it's
CTRL-Z.) Fundamentally, end of file doesn't exist for a
keyboard: Unix sort of simulates it if you enter CTRL-D at the
beginning of the line (supposing no ones been playing around
with stty); traditionally, Windows library software considers
CTRL-Z an end of file in any text file.

And both agree about CTRL-C: terminate the program doing the
input, or all programs in the "terminal group" under Unix. (But
the program can catch the signal, and not die, and of course, if
you've let someone else get at your terminal while you're logged
in, and they've played around with stty, who knows what may
happen.)

--
James Kanze

0 new messages