Weird function seems to change vector values - c++

I have to program something that filters email by checking if the source of the email is found in a blacklist or if any word in the email's content is found in a list of blocked words.
Here's the function that filters the email.
void filterEmail(vector<email>& amis, vector<email>& corbeille,
vector<email>& rejete, vector<email>& emails,
vector<string> b, vector<string> w,
vector<string> spam)
{
bool isListed(email, vector<string>);
bool isSpam(email, vector<string>);
for(vector<email>::size_type i = 0; i < emails.size();)
{
if(isListed(emails[i], b))
{
corbeille.push_back(emails[i]);
emails.erase(emails.begin() + i);
}
else i++;
}
for(vector<email>::size_type i = 0; i < emails.size();)
{
if(isListed(emails[i], w))
{
amis.push_back(emails[i]);
emails.erase(emails.begin() + i);
}
else i++;
}
for(vector<email>::size_type i = 0; i < emails.size();)
{
if(isSpam(emails[i], spam))
{
cout << emails[i].source << " " << emails[i].content[0];
rejete.push_back(emails[i]);
emails.erase(emails.begin() + i);
}
else i++;
}
return;
}
I have a really weird problem with the part where it checks for spam words in the content (3rd for loop)
after a few test cout's I realised after the first time isSpam returns true, the content of the email changes to the line where it first returns true, and all the next emails return true.
bool isSpam(const email e, const vector<string> motsinterdis)
{
for(vector<string>::size_type i = 0; i < e.content.size(); ++i)
{
for(vector<string>::size_type j = 0; j < motsinterdis.size(); ++j)
{
if(string::npos != e.content[i].find(motsinterdis[j]))
{
cout << e.source << endl;
cout << motsinterdis[j] << " found in " << e.content[i] << endl;
return true;
}
}
}
return false;
}
I have an Struct Email, which contains a vector content. Sorry for the french variables in there :P
Hope I've been clear enough,
Thanks a lot.

Related

Return struct element from vector c++

I'm new to C++ and I'm trying to return a struct from a vector of structs by using 2 search criteria.
The function find_city is returning me everything from the defined range, regardless of whether it exists inside the vector of struct.
Here's my code:
struct cityLoc
{
int hRange;
int vRange;
int cityCode;
string cityName;
};
vector<cityLoc> cl1;
// the vector has already been preloaded with data
// function to return my struct from the vector
cityLoc find_city(int hRange, int vRange)
{
for (size_t i = 0; i < cl1.size(); i++)
{
if ((cl1[i].hRange = hRange) && (cl1[i].vRange = vRange))
{
return cl1[i];
}
}
}
int main()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j <= 8; j++)
{
cityLoc this_city;
this_city = find_city(i, j);
cout << this_city.hRange << ", " << this_city.vRange << endl;
}
}
return 0;
}
Also, aside from this question, I was previously looking into std::find_if and didn't understand it. If I have the following code, what is the output? How do I modify it such that it returns a struct?
auto it = find_if(cl1.begin(), cl1.end(), [](cityLoc& cl) { return cl.hRange == 1; } );
You have a bug here:
if ((cl1[i].hRange = hRange) && (cl1[i].vRange = vRange))
Those = are assignments, not comparisons! Please enable compiler warnings and you won't be hurt by such obvious typos in future.
std::find_if will return the iterator to the found struct entry if it is successful, std::vector::end() otherwise. So, you should first validate the returning iterator if it is valid or not.
For example:
auto it = std::find_if( cl1.begin(), cl1.end(),
[](const cityLoc& cl) { return cl.hRange == 1; } );
if ( it == cl1.end() )
{
// ERROR: Not found! Return error code etc.
return -1;
}
// And, if found, process it here...
std::cout << it->hRange << '\n';
std::cout << it->vRange << '\n';
The criteria (predicate) part in std::find_if is a lambda expression.

c++ String returning with an extra char

I have tested my program and am certain right before being returned the string in my function equals "card001". But the returned value equals "card0011". I have no idea how this even happens. Help me before I lose my mind. ;)
std::string function_cardTexture(int card) {
//removes the last 1
card = card - 10000;
int ctr = 0;
card = floor(card / 10);
std::cout << card << std::endl;
//turn int card into a string
std::string a = static_cast<std::ostringstream*>(&(std::ostringstream() << card))->str();
//combines card and string a into one string
std::string nametext = "card00" + a;
std::cout << nametext << std::endl;
return (nametext);
}
void function_Battle(tempPlayer &Player, tempCard &card001) {
if (Player.Start == true) {
//Draw hand
for (int i = 0; i < Player.numDrawn; i++) {
int x = rand() % Player.deckSize + 0; ;
Player.Hand[i] = Player.Deck[x];
Player.Discarded[x] = 1;
}
Player.Start = false;
}
std::map<std::string, tempCard> Vars;
//draw hand
for (int i = 0; i < Player.handMax;i++) {
if (Player.Hand[i] != 0) {
sf::RectangleShape Card(sf::Vector2f(80.0f, 128.0f));
std::string nametext = function_cardTexture(Player.Hand[i]);
std::cout << nametext;
sf::Texture texture = Vars[nametext].Art;
Card.setTexture(&texture);
window.draw(Card);
}
}
}
Your problem is how you're printing things out without a newline in the function_Battle() function, so you're likely "smashing together" your new value with an old one. If you replace your main function with just a loop with clearer printing of values, you can see you don't have a problem:
http://coliru.stacked-crooked.com/a/8d1e4f51643b84b9
That link will go to an online compiler where I just replaced the calling function with a loop that makes numbers. It even supplies a negative one.

C++ for loop on 2d array

This is my 2d array:
int pay_scale[15][10] =
{ { 18526, 19146, 19762, 20375, 20991, 21351, 21960, 22575, 22599, 23171},
{ 20829, 21325, 22015, 22599, 22853, 23525, 24197, 24869, 25541, 26213 },
{ 22727, 23485, 24243, 25001, 25759, 26517, 27275, 28033, 28791, 29549},
{ 25514, 26364, 27214, 28064, 28914, 29764, 30614, 31464, 32314, 33164},
{ 28545, 29497, 30449, 31401, 32353, 33305, 34257, 35209, 36161, 37113},
{ 31819, 32880, 33941, 35002, 36063, 37124, 38185, 39246, 40307, 41368},
{ 35359, 36538, 37717, 38896, 40075, 41254, 42433, 43612, 44791, 45970},
{ 39159, 40464, 41769, 43074, 44379, 45684, 46989, 48294, 49599, 50904},
{ 43251, 44693, 46135, 47577, 49019, 50461, 51903, 53345, 54787, 56229},
{ 47630, 49218, 50806, 52394, 53982, 55570, 57158, 58746, 60334, 61922},
{ 52329, 54073, 55817, 57561, 59305, 61049, 62793, 64537, 66281, 68025},
{ 62722, 64813, 66904, 68995, 71086, 73177, 75268, 77359, 79450, 81541},
{ 74584, 77070, 79556, 82042, 84528, 87014, 89500, 91986, 94472, 96958},
{ 88136, 91074, 94012, 96950, 99888, 102826, 105764, 108702,11640,114578},
{ 103672, 107128, 110584, 114040, 117496, 120952, 124408,127864, 1, 2}};
My goal is to std::cout in the same order it is displayed above so it prints each value in the first array pay_scale[0][n++]
then moves onto pay_scale[1][[n++]
then moves onto pay_scale[2][n++] etc. until it reaches pay_scale[14]
As of right now I just have:
for (int i = 0; i < 10; i++) {
std::cout << "[" << pay_scale[n][i] << "]" << "\t";
}
And n = 0
I am having trouble indexing it without it jumping to the next array before it finishes the first one.
I must say I am new to cpp as I am sure you can see, any guidance is appreciated.
Thank you!
Here's the way the cool kids do it. :-)
for (const auto &row : pay_scale) {
for (auto col : row) {
std::cout << col << " ";
}
std::cout << "\n";
}
Why not use something more like this which loops through all the array:
for(int i = 0; i<15; i++)
{
for(int n = 0; n<10; n++) cout<< "["<< pay_scale[i][n]<<"]\t";
cout<<endl;
}
Hope this helps.

How can I trace back the error

I was assigned to create an array check (to see if the array is increasing, decreasing, or neither [then exiting if neither]) and a recursive binary search for one of my assignments. I was able to do these things after some help from my peers, but I need help in finding what seems to be causing the error
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Aborted
when running the code. I Googled this error and this error seems to be vague or I just am not understanding. It compiles without errors, but I need help in what finding what I did wrong. It is able to run without the binarySearchR function and its associating code, as the array check on its own was the previous assignment. Below is the code, and I thank you so much in advance!
#include <iosteam>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
int checkArraySort (string *fileLines, int numberOfLines);
int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax);
int main ()
{
int numberOfLines = 0;
string searchKey = 0;
cout << "Input search key: ";
cin >> searchKey;
ifstream fileIn;
fileIn.open("words_in.txt");
string line;
if (fileIn.eof()) /* Checks file to see if it is blank before proceeding */
{
exit (EXIT_SUCCESS);
}
else
{
while(!(fileIn.eof()))
{
fileIn >> line;
numberOfLines++;
}
fileIn.close(); /* closes fileIn, need to reopen to reset the line location */
fileIn.open("words_in.txt");
string *fileInLines;
fileInLines = new string[numberOfLines];
for (int i = 0; i < numberOfLines; i++)
{
fileIn >> line;
fileInLines[i] = line;
}
fileIn.close(); /* closes fileIn */
int resultingCheck = checkArraySort(fileInLines, numberOfLines);
if (resultingCheck == -1)
{
cout << "The array is sorted in descending order." << endl;
}
else if (resultingCheck == 1)
{
cout << "The array is sorted in ascending order." << endl;
}
else
{
cerr << "ERROR: Array not sorted!" << endl;
exit (EXIT_FAILURE);
}
int searchResult = binarySearchR (fileInLines, searchKey, 0, numberOfLines);
if (!searchResult == -1)
{
cout << "Key found at index " << searchResult << "." << endl;
}
else
{
cout << "Key not found at any index." << endl;
}
exit (EXIT_SUCCESS);
}
}
int checkArraySort (string *fileLines, int numberOfLines)
{
int result = 1; /* Ascending by default */
for (int i = 1; i < numberOfLines; i++) /* Checks if decending */
{
if (fileLines[i] < fileLines[i-1])
{
result = -1;
}
}
if (result == -1) /* Makes sure it is descending (or if it is neither) */
{
for (int i = 1; i < numberOfLines; i++)
{
if (fileLines[i] > fileLines[i-1])
{
result = 0;
}
}
}
return result;
}
int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax)
{
// so, its gotta look at the center value and each times, it discards half of the remaining list.
if (iMax < iMin) /* If the minimum is greater than the maximum */
{
return -1;
}
else
{
int iMid = (iMin + iMax) / 2;
if (fileLines[iMid] > searchKey) /* If the key is in the lower subset */
{
return binarySearchR (fileLines, searchKey, iMin, iMid - 1);
}
else if (fileLines[iMid] < searchKey) /*If the key is in the upper subset */
{
return binarySearchR (fileLines, searchKey, iMin, iMid + 1);
}
else /*If anything else besides the two */
{
return iMid;
}
}
}
The easy way: add a bunch of cout s to see where you program goes and what the values are.
Pros
Easy to do
Cons
Requires a recompile each time you want to add more info
The hard way: Learn to use a debugger
Pros
Can inspect "on the fly"
Don't need to rebuild
Can use what you learn in every other C++ program
Cons
Requires a bit of research to learn how to do it.

How to correctly create a ofstream of a struct vector? (abort() is called)

I'm currently building a game and in this part my goal is to update a previously created file of High Scores ("rank.txt")
To do so, I've created a couple of functions so they can read what's in the txt file and so it can update it. Though the reading is ok, while adding the update function it gives me an error and tells me abort() has been called.
void highScoresUpdate(vector<PlayerInfo> player)
{
// This function is responsible for updating the High Scores Table present in the RANK.TXT file
vector<HighScoresStruct> highScores_temp;
HighScoresStruct temp;
ofstream out_file_3("rank.txt");
highScores_temp = readHighScores("rank.txt");
for (int k = 0; k < player.size(); k++)
{
player[k].name = temp.name;
player[k].score = temp.score;
player[k].time = temp.time;
highScores_temp.push_back(temp);
}
sort(highScores_temp.begin(), highScores_temp.end(), compareByScore);
for (int i = 0; i < highScores_temp.size(); i++)
{
out_file_3 << highScores_temp[i].name << endl << highScores_temp[i].score << endl << highScores_temp[i].time << endl;
}
out_file_3.close();
}
For background information, so i don't spam this thread with code all you need to know is that readHighScores is the function responsible for extracting information from the txt file and placing it on a vector .
The structs and comparebyScore funtion are listed below.
bool compareByScore(const HighScoresStruct &a, const HighScoresStruct &b)
{
if (a.score < b.score)
return true;
else if (a.score == b.score)
{
if (a.time > b.time)
return true;
else
return false;
}
else
return false;
}
struct PlayerInfo
{
string name;
unsigned int score;
vector<char> hand;
bool inGame;
unsigned int time;
};
struct HighScoresStruct
{
string name;
unsigned int score;
unsigned int time;
};
So... Can anyone help me?