Program with vectors fails

The program just fails when I run the program could someone point me in the right direction please? I'm a beginner and self teaching myself from a book so I really have no one to ask for help what am I doing wrong???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }
/*
to do list: make a vector of words I want to bleep out, write a loop that scans each word in the vector for the word that the user types in,
if the users word is not a bleeped word then write it if it is then write bleep.
program fails to start up as it is now.. 
*/
int main() {
	vector<string> bleep;
	bleep.push_back("bitch"); // would bleep[0] method work better?
	bleep.push_back("fuck");
	bleep.push_back("shit");
	bleep.push_back("damn");
	bleep.push_back("hell");
	string var;
	while (cin >> var){
		for (int i = 0; i < bleep.size(); ++i){
			if (bleep[i] == var){
				cout << " bleep ";
			}
			else {
				cout << " " << var;
			}
		}
	}
}
Very Nice Topic
Here you go I commented on everything to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// standard macro names
#include <iostream>
#include <map>
#include <vector>
using namespace std;



int main(){
    std::vector <std::string> bleep;
	bleep.push_back("bitch"); // Either one goes
	bleep.push_back("fuck");
	bleep.push_back("shit");
	bleep.push_back("damn");
	bleep.push_back("hell");
   std::vector<std::string>::iterator i; //Loops through everything
      std::string q; //The input that gets checked
      std::cin >> q;
     for(i = bleep.begin(); i != bleep.end(); i++){
      if(*i == q){ //i points to everything in the vector of bleep so when you type out a bad word and it equals a place in bleep it prints out if statement

        std::cout << "Bleep" << '\n'; //The bleep

      } //Everything else goes here. Maybe you can use a template to use your bleeps.


     }
}
> would bleep[0] method work better?

bleep[0] = "bitch" ; won't work at all. The vector is empty; there is no element at position 0.

bleep.push_back("bitch"); is fine; this adds a new string to the end of the vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <vector>

inline void keep_window_open() { char ch; std::cin >> ch; }

int main() {

    // http://www.stroustrup.com/C++11FAQ.html#init-list
    const std::vector<std::string> bleep { "bitch", "fuck", "shit", "damn", "hell" };

    std::string word ;
    while( std::cin >> word ) {

        bool bleepable = false ; // start with the premise that the word is not bleepable

        for( std::size_t i = 0; i < bleep.size() ; ++i ) { // for each position in the vector

            if( bleep[i] == word ) bleepable = true ; // if this word is the same as the one in the vector, it is bleepable
        }

        // or using an idiomatic range-based loop
        // http://www.stroustrup.com/C++11FAQ.html#for
        bleepable = false ;
        for( std::string bleep_this : bleep ) if( word == bleep_this ) bleepable = true ;

        if(bleepable) std::cout << "bleep " ;
        else std::cout << word << ' ' ;
    }

    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/798ee45ab27f0722
Last edited on
Topic archived. No new replies allowed.