LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 30
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

Code:
segmentation fault
meassage when I try to run it.

Code:
#include <iostream>
#include<vector>
#include<string>
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])
{
++word_count[n];
a=true;
}

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, 03:19 AM   #2
deveraux83
Member
 
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:

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

Code:
word_count.push_back(0);
the same time you execute:

Code:
word.push_back(x);
That solved the problem for me. Hope this helps!
 
Old 12-25-2004, 04:14 AM   #3
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

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


Reply


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 07:11 PM
I can't figure out how to get my soundcard to work AMArt79196 Linux - Hardware 1 07-03-2004 02:46 PM
can't figure out how to get xine to work Fascistchicken Slackware 1 11-12-2003 03:06 PM
very very simple identd question.. cant figure out myself.. need help!! kublador Linux - Networking 1 06-10-2003 03:17 AM
retard can't figure out simple things Karnak Linux - Hardware 13 10-05-2002 12:02 PM


All times are GMT -5. The time now is 04:29 AM.

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