Hello, I'm new to these forums. I've been working my way through Accelerated C++ and have hit a snag with one of my exercise programs. I'm trying to create a program that will take sequential string inputs, put them in a vector, and then count how many times each string appears in the vector.
I can get my basic counting program to run fine, but when I tried to upgrade it by checking for string repeats in my counting loop i get a segmentation fault. From experimentation I think the segmentation fault happens when I increment vec_sz c in the nested while loop. But from the little I understand c should be within the scope of the while loop.
If I can't increment c in the while loop, are there any more elegant solutions?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using std::cin; using std::cout;
using std::endl; using std::vector;
using std::string;
int main()
{
//ask for input and store in a vector
cout << "Please input a sequence of words ending with end-of-file: ";
string x;
vector<string> list;
while (cin >> x)
list.push_back(x);
//check for input into the vector
typedef vector<string>::size_type vec_sz;
vec_sz size = list.size();
if (size == 0) {
cout << endl << "Error! No words were entered. ""Please try again." << endl;
return 1;
}
//read a list element into a string
//and count the number of times that string occurs in list
for (vec_sz r = 0; r!= size; ++r) {
string holder = list[r];
int count = 0;
for(vec_sz c = 0; c != size; ++c) {
vec_sz d = 0;
vec_sz dc = c; //initializes counting variable dc
//to the current value of c
//checks the current vector member against prev
//members for repeats, and increments skips if found
while(d != dc) {
if (list[d] == list[c])
++c;
++d;
}
//if the holder is found in list count is incremented
if (holder == list[c])
++count;
}
cout << holder << " appears " << count << " times." << endl;
}
return 0;
}
Back in the old days we used to have to trace programs and show the value of the variables as we went thru the program. Have you tried doing anything like this?
If you have an integrated debugger, you can step thru the program quite easily and watch the values. If not, inserting std::cout in suitable places will do it.
Thanks for the replies. I'm pretty sure I know what went wrong now, even though I'm still not sure why I'm getting a segmentation fault I see that I cannot increment c in the nested if and still expect proper program function.
lol... Hmm I just tried it without and it does work :S i'm sure one time I tried using getline without std:: and I got compiler errors :s ahh well! Cheers.
I hate getting SIGSEGV, I don't know how to solve it except for not accessing memory I shouldn't. The thing is, I get it by trying to access registers with embedded ASM :l
while(d != dc) {
if (list[d] == list[c])
++c;
++d;
}
//if the holder is found in list count is incremented
if (holder == list[c])
++count;
If c is the last entry in your vector your incrementing it within the while loop to 1-past end of the vector. Then your comparing an invalid value against holder, more than likely causing your fault. :)
he next issue is that if c is the last in your vector, it gets incremented to size (line 49) then when it gets incremented again past size (line 38) and creates an infinite loop.
line 38 should read: for(vec_sz c = 0; c < size; ++c). Using != is always dangerous when your modifying your index variable within the loop.