Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-25-2004, 02:54 AM   #1
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 31
C++ question: can't figure out why this simple code does not work

Hi everyone,

I am trying to write a program which inputs words and then gives the count of how many times each word appeared at the input. The following code compiles, but gives

segmentation fault
meassage when I try to run it.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;

int main()

cout<<"Please input words followed by the end-of-file";
string x;
vector<string> word;
vector<int> word_count;

while (cin>>x)

bool a = false;

for (n=0; n!=word.size();++n)
if (x==word[n])

if (a==false) word.push_back(x);
for (n=0; n!=word.size();++n)
cout<<word[n]<<"appeared  "<<word_count[n]<<" times"<<endl;
return 0;
This is supposed to be fairly simple and I can think of couple of other ways of doing it but can't figure out why this particular code does not work.

BTW: How is GDB supposed to be used in the situation where I have to give program input?
Old 12-25-2004, 04:19 AM   #2
Registered: Jul 2003
Location: Malaysia
Distribution: Red Hat, Slackware 9.1
Posts: 76

Rep: Reputation: 15
I'm not particularly up to date with the Vector class (I use my own container class) so I could be wrong, but I believe your problem is that you are attempting to read a location of memory not yet defined by your software, that is:

++ word_count[n];
You really should execute:

the same time you execute:

That solved the problem for me. Hope this helps!
Old 12-25-2004, 05:14 AM   #3
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 31
Duh! Thanks a lot man. I am stupid. Of course I have to initialize the variable if I am gonna use its value.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
can't figure out why sound doesn't work mushmaster Linux - Software 2 03-12-2005 08:11 PM
I can't figure out how to get my soundcard to work AMArt79196 Linux - Hardware 1 07-03-2004 03:46 PM
can't figure out how to get xine to work Fascistchicken Slackware 1 11-12-2003 04:06 PM
very very simple identd question.. cant figure out myself.. need help!! kublador Linux - Networking 1 06-10-2003 04:17 AM
retard can't figure out simple things Karnak Linux - Hardware 13 10-05-2002 01:02 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:07 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration