Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4053534
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:27:33+00:00 2026-05-20T14:27:33+00:00

I was writing a program that tell the user to input a random string,

  • 0

I was writing a program that tell the user to input a random string, and then print out all the duplicates and the number of time each of them repeats. I was running it through gdb and this is the output:

Here is the program:

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

using namespace std;

int main()
{
  //Read a string word by word and put into a vector
  //loop through the vector:
  //  If there are two words duplicate:
  //    loop through another vector (used to store duplicate word)
  //    compare if these two words are the same as the duplicate word stored
  //      If the same: ++count[i]
  //      If not: push_back(count) ; ++count[i]

  string word;
  vector<string> sentence;
  vector<string> duplicate;
  vector<int> times;
  int count = 1;

  while (cin >> word) {
    if (word == "ctrlz") {
      break;
    }
  sentence.push_back(word);    
  }

  vector<string>::size_type i = 0;
  vector<string>::size_type j = 0;

  while (i != sentence.size()) {
    if (sentence[i] == sentence[i+1]) {
      while (j != sentence.size()) {
        if (duplicate.size() == 0) {
          duplicate.push_back(sentence[i]);
          times.push_back(count);
          ++times[0];
        }
        else { 
          if (sentence[i] != duplicate[j]) {
            duplicate.push_back(sentence[i]);
            times.push_back(count);
            ++times[j+1];          
          }
          else {
            ++times[j];
          }
        }
      ++j;
      }
    }
  ++i;  
  }

  while (i != duplicate.size()) {
    cout << duplicate[i] << ' ';
    ++i;
  }

  return 0;
}

I got this after running gdb:

(gdb) run 
Starting program: /home/phongcao/C++/6.12
phong phong phong phong phong phong 
ctrlz

Program received signal SIGSEGV, Segmentation fault.
0x001c58d9 in std::string::size() const () from /usr/lib/libstdc++.so.6 
(gdb) 

What does this output mean? How can I fix this segmentation fault?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:27:34+00:00Added an answer on May 20, 2026 at 2:27 pm

    Some bugs:

    if (sentence[i] == sentence[i+1]) {
    

    Your loop permits i to be size()-1 – So you’re reading one past the end of the vector sentence.

    while (i != sentence.size()) {
        while (j != sentence.size()) {
         ...
         ++j;
       }
       ++i;
    }
    

    j is never reset – next iteration of the outer loop it’ll start at sentence.size() – You likely don’t want that.

    You should solve this with a std::map<std::string, int>:

    std::map<std::string, int> words;
    while (cin >> word) {
        if (word == "ctrlz") {
             break;
        }
        words[word] += 1;
    }
    for (std::map<std::string>::const_iterator it = words.begin(); it != words.end(); ++it) { 
         if (it->second > 1) {
             cout << it->second << " copies of " << it->first << endl;
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that lets the user input 6 temperature readings, and then
I'm writing a C program that accepts input one line at a time from
Im writing a program that should read input via stdin, so I have the
I am writing a program that downloads images to the hard drive and then
I am currently writing a program which takes user input and creates rows of
I am writing a program that checks the number of parameters of a methods
I'm writing a string tokenization program for a homework assignment in C++, that uses
I am writing a program that finds a user's ideal weight by asking for
I'm writing a program that handles DBs and writes any changes into ListView for
I'm writing a program that's parsing an XML file to java objects using smooks.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.