Trouble with vectors

This is a discussion on Trouble with vectors within the C++ Programming forums, part of the General Programming Boards category; I am writing a program to read in a line of text and then replace all four letter words with ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    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;
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while (v[j] != '\n' || v[j] != ' ' || v[j] != '.' || v[j] != ',' || v[j] != "")
    a char cannot be nothing. remove that.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    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.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    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?

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    this should do
    Code:
    	for (int i = 0; i-1 < v.size(); i++)
    Kurt

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    True. could also be
    Code:
    for (int i = 0; i < v.size()-4; i++)
    othewise it can't be a 4 -letter word
    Kurt

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes, but the interior while loop still needs modification. It doesn't stop at 4 characters.

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    true again.
    Kurt

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.)

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    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'

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. trouble with assignment, vectors..
    By bluegoo06 in forum C++ Programming
    Replies: 6
    Last Post: 11-16-2005, 02:43 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21