-2

just a novice learning C++. It's a great language with a steep learning curve and I am climbing that mountain.

Currently, I want to compare two vector, the first vector already has predefined elements, while the other vector's elements are inputted on the go (i.e. "cin in a while loop"). The output of the vector comparison should be a string, "bleep", whenever similar strings are matched between the two sorted vectors. Else, the vector with the inputted words will print out it's elements.

Side Note: The code is a bit religious (I decided to use a standard text that I know - the Bible). So I am not trying to condemn anyone I just needed a standard text we all have access to. Also, I have searched online and some suggestions for comparing vectors required that I used set_difference, set_intersection etc. I have checked them out, but I realised that it will take a novice like me more time to understand the interator syntax. So that is why I am here.

So please I need your help because the code keeps giving this error from the implementation file: enter image description here

I know. I wish i could solve the error (Error's are actually the topic for the next chapter in the book I am reading)

So enough small talk here is the code I have been working on. Any pointers will be appreciated. Thanks in advance:)

#include "stdafx.h"
#include "Std_lib_facilities.h"
int main()
{
    // Introduction

    cout << "This program will compare the your input to already predefined works which are found in Galatians 5:19-24.";

    // Vectors of disliked by God in Galatians 5:19-24. These are the works of the flesh. And I copied them from the Bible.
    // Side comment:I know we are all sinners- I am not judging anyone. I  myself am working on some with the help of the Holy Spirit and my spirit is getting stronger everyday.

    vector <string> disliked; disliked[0]= "hatred"; disliked[1] = "envy"; disliked[2] = "adultery"; disliked[3] = "fornication"; disliked[4] = "anger"; disliked[5] = "malice"; 
    disliked[6] = "gluttony"; disliked[7] = "lying"; disliked[8] = "stealing"; disliked[9] = "greed"; disliked[10] = "uncleanliness"; disliked[11] = "lewdness"; disliked[12] = "idolatry";
    disliked[13] = "sorcery"; disliked[14] = "contentions"; disliked[15] = "jealousy"; disliked[16] = "selfish ambition"; disliked[17] = "dissensions"; disliked[18] = "heresies";
    disliked[19] = "murder"; disliked[20] = "drunkeness"; disliked[21] = "revelries";

    // Sorted the vectors so that they could be compared with what are liked.
    sort(disliked.begin(), disliked.end());

    cout << "Please enter the fruits of the Holy Spirit in Galatians 5:22-23, while purposely adding some of the works of the flesh which are found in Galatians 5:19-24: \n" <<
        "Also inset a Ctrl+Z and click enter when you have finished inserting the works"<< endl;

    string first_input; // String for the input of the likes. Please, you are to purposely insert some words that are not listed in Galatians 5:22-23. Rather include some from Galatians 5:19-24
    vector <string> liked; // Vector of liked qualities

    while (cin >> first_input) // cin iostream is used to insert the whitespace separated strings into first_input
        liked.push_back(first_input); // push_back inserts the strings from first_input into the vector liked
        sort(liked.begin(), liked.end()); // the vector liked is sorted.

        for (int i = 0; i < liked.size(); ++i) // Using the loop for the comparison of the sorted vectors in the next if statment
            if (disliked == liked) // if strings are similar give a bleep!
                cout << "Bleep!" << endl;
            else
                cout << liked[i]; // if strings are not similar cout the list of liked qualities.


}
JAA
  • 3
  • 2
  • 4
    Please include the error message as text in the question. The image quality is rather poor and anyhow errors and code should be text not images – 463035818_is_not_an_ai Mar 22 '18 at 16:11
  • 2
    a tip that helps you even before debugging: write one line, compile, fix errors and only then write the next line. The problem in your code is on line 2 of main, the rest is rather irrelevant... – 463035818_is_not_an_ai Mar 22 '18 at 16:14
  • 1
    aside: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – UKMonkey Mar 22 '18 at 16:16
  • 1
    more suggested reading: [mcve] – 463035818_is_not_an_ai Mar 22 '18 at 16:18
  • Thanks user463035818 for your advice. I will begin to test code line by line. I was able to initialise the vector element using Miles Budnek advice below. About the image quality I will also upload a better one next time-it looked fine on my PC so i didnt bother. And thanks UKMonkey for the link you posted on namespace. I will keep your advise when I am learning about that. Cheers. – JAA Mar 23 '18 at 10:31

1 Answers1

1

Your vector disliked is 0 elements long, so you're assigning to elements that don't exist.

std::vector::operator[] does not change the size of the container; it just gets a reference to an element that already exists. To add elements to the end of a vector, you need to use push_back:

vector<string> disliked;
disliked.push_back("hatred");
...

Alternatively you could create it with some number of default-constructed elements and assign to them:

vector<string> disliked(22);
disliked[0] = "hatred";
...

Since you know all of the elements of disliked at construction time you can use list-initialization:

vector<string> disliked = {
    "hatred",
    ...
};
Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • Thank you very much. Your advise really helped me with the vector initialisation. I am now working on the comparison. Cheers again and God bless. – JAA Mar 23 '18 at 10:26