-
Trouble with vectors
I am writing a program to read in a line of text and then replace all four letter words with the word 'love'. I am getting the following compiler error:
love.cpp: In function ‘void replace(std::vector<char, std::allocator<char> >&)’:love.cpp:60: error: ISO C++ forbids comparison between pointer and integer
line 60 is the while statement in the replace function.
I am hoping someone could point me in the right direction. Thank you in advance.
Code:
#include <iostream>
#include <vector>
using namespace std;
void introduction();
//Explains what the program does
void get_input(vector<char>& v);
//Asks user to input text
void replace(vector<char>& v);
//Replaces all four letter words with the word love.
void output(vector<char> v);
//Outputs new sentence.
int main()
{
vector<char> v_input;
introduction();
get_input(v_input);
replace(v_input);
output(v_input);
return 0;
}
//uses iostream
void introduction()
{
cout << "This program will ask the user to input a sentence and\n"
<< "then will output the sentence with all four letter words\n"
<< "replaced with the word 'love'\n"
<< endl;
}
//uses iostream
//uses vector
void get_input(vector<char>& v)
{
char tmp;
cout << "Please type in a sentence:\n";
cin.get(tmp);
do
{
if (!cin.get(tmp)) //breaks if character is not able to be read in
break;
v.push_back(tmp);
}while (tmp != '\n');
}
//uses iostream
//uses vector
void replace(vector<char>& v)
{
int j, count;
for (int i = 0; i < v.size(); i++)
{
j = i + 1;
// now checking to see if the next word is = 4 letters
while (v[j] != '\n' || v[j] != ' ' || v[j] != '.' || v[j] != ',' || v[j] != "")
{
count++;
j++;
}
if (count == 4) //replaces 4 letter word with 'love'
{
v.push_back('l');
v.push_back('o');
v.push_back('v');
v.push_back('e');
}
i = i + j;
}
}
//uses iostream
//uses vector
void output(vector<char> v)
{
cout << endl
<< "The new sentence with the words replaced is:\n";
for (int i = 0; i < v.size(); i++)
{
cout << v[i];
}
cout << endl;
}
-
Code:
while (v[j] != '\n' || v[j] != ' ' || v[j] != '.' || v[j] != ',' || v[j] != "")
a char cannot be nothing. remove that.
Kurt
-
Well, it could be the null character. Almost nothing. ;)
"" is a string literal, which in C++ means it's a char*. Hence your illegal pointer comparison error. Change it to '\0' if you want to test for the null character.
-
Thank you, you guys are good, compiles now. I tried running it, was able to type in line of text but upon hitting return the reply was 'segmentation fault'. I put in 'cout << "1" ' in the do while loop to see if the loop iterated, which it did for about 40 times and then the segmentation fault. Do I need to declare a size for the vector?
-
this should do
Code:
for (int i = 0; i-1 < v.size(); i++)
Kurt
-
>> i-1 < v.size();
That should be i+1 or v.size()-1. However, it will take more than that. The while loop inside the for loop could go forever. You need to check that j < v.size() before using v[j]. I would just make it the first part of the while loop and && it with the rest of the condition.
-
True. could also be
Code:
for (int i = 0; i < v.size()-4; i++)
othewise it can't be a 4 -letter word
Kurt
-
Yes, but the interior while loop still needs modification. It doesn't stop at 4 characters.
-
-
How can you replace characters by doing push_backs?
(The whole thing would probably be simpler if you switched over to std::string, used its replace method to do the replacing, and possibly find_first_of and find_first_not_of to identify the beginning and ending of each word.)
-
Thank you all, I used everyone's tips. I switched from using a vector to using an array and changed the loops, now it works. I'm learnin'
-
>> I switched from using a vector to using an array
Really? How do you resize the array. I don't think anybody was advising that, in most cases that would be a step in the wrong direction.