C++ After Function Call and Function Completion, Game Crashes Entirely - c++

I've been having an issue with a game I've been making in my C++ game programming class for school. For some reason, after calling a function which I'm using to manage the inventory based stuff, the function seems to complete and work (I think this because I put in cout commands at the end of it and they printed correctly, also the function runs twice in a row, and they both run), my entire game crashes and doesn't reach the next line. I tried commenting out all the code in the function and it still crashed. I commented out the function calls and it worked, but I still can't tell what is wrong with it. I'll put the code for the function and the section were I make the calls:
string inventoryFunction(int h, string ab)
{
if(h == 1)
inventory.push_back(ab);
else
if(h == 2)
{
for(int i=0; i < inventory.size(); i++)
{
if(inventory[i] == ab)
inventory[i].erase();
}
}
else
if(h == 3)
{
cout << inventory[0];
for(int i=1; i < inventory.size(); i++)
cout << ", " << inventory[i];
}
}
The function call:
if(answer.find("village") != string::npos)
{
cout << endl;
cout << "While looking around your village,\nyou found a stone sword and a cracked wooden shield!" << endl;
inventoryFunction(1, "stone sword");
inventoryFunction(1, "cracked wooden shield");
cout << "Would you like to set off on your adventure now?" << endl;
cin >> answer2;
capitalizeLower(answer2);

Not sure there's anything there likely to cause a crash, my advice would be to single-step your code in the debugger to see where it's falling over. It's quite possible the bug is somewhere totally different and it's just being exacerbated by the function calls modifying the vector.
That's the nature of bugs unfortunately, you can never really tell where they're actually coming from without looking closely :-)
However, there are a couple of issues with the code that I'd like to point out.
First, with regard to:
inventory[i].erase();
That doesn't do what you think it does. inventory[i] is the string inside your vector so it's simply erasing the string contents.
If you want to remove the string from the vector, you need something like:
inventory.erase (inventory.begin() + i);
Second, I'd tend to have three separate functions for addToInventory, removeFromInventory and listInventory.
It seems a little ... unintuitive ... to have to remember the magic values for h to achieve what you want to do, and there's no real commonality in the three use cases other than access to the inventory vector (and that's not really reason enough to combine them into the same member function).
On top of that, your function appears to be returning a string but you have no actual return statements and, in fact, none of the three use cases of your function require anything to be passed back.
The signature is better off as:
void inventoryFunction(int h, string ab)
In terms of the second and third points above, I'd probably start with something like:
void addToInventory (string item) {
inventory.push_back(ab);
}
void removeFromInventory (string item) {
for (int i = 0; i < inventory.size(); i++) {
if (inventory[i] == ab) {
inventory.erase (inventory.begin() + i);
break;
}
}
void listInventory () {
cout << inventory[0];
for (int i = 1; i < inventory.size(); i++)
cout << ", " << inventory[i];
}
You may also want to look into using iterators exclusively for the second and third functions rather than manually iterating over the collection with i.
It'll save you some code and be more "C++ic", a C++ version of the "Pythonic" concept, a meme that I hope will catch on and make me famous :-)

So by changing the inventoryFunction to a void function like #Retired Ninja said, the crash has stopped occurring and now the program is working great.
Also, #paxdiablo pointed out that I was using the inventory[i].erase() thing incorrectly, so thanks a bunch to him, because now I won't have to come back on here later to try to fix that :D

string inventoryFunction(int h, string ab)
should return a string but does not have any return statements. Of course it works, after you change it to a void function, which correctly does not return anything. Interesting is, that you are able co compile this code without an error - normally a compiler would show you this problem.

Related

C++ - Pointer to local variable within the function

I know this can look like a rookie question already asked a thousand time. But I searched for the exact answer and I haven't found one...
I'm working on a code that, to sum up, fill an XML with different data.
I'm trying to optimize a part of it. The "naïve" code is the following:
xml << "<Node>";
for(auto& input : object.m_vec)
{
if(input == "Something")
{
xml << input;
}
}
xml << "</Node>";
for(auto& input : object.m_vec)
{
if(input == "SomethingElse")
{
xml << "<OtherNode>";
xml << input;
xml << "</OtherNode>";
break;
}
}
The important thing is, while more than one input fit in <Node></Node>, only one fit in <OtherNode></OtherNode> (explaining the break;) and it may not exist either (explaining the xml << in-between the if statement).
I think I could optimize it such like:
std::vector<std::string>* VecPointer;
xml << "<Node>";
for(auto& input : object.m_vec)
{
if(input == "Something")
{
xml << input;
}
else if(input == "SomethingElse")
{
VecPointer = &input;
}
}
xml << "</Node>";
if(!VecPointer->empty())
{
xml << "<OtherNode>"
<< *VecPointer
<< "</OtherNode>";
}
The point for me here is that there is no extra memory needed and no extra loop. But the pointer to the local variable bothers me. With my beginner's eyes I can't see a case where it can lead to something wrong.
Is this okay? Why? Do you see a better way to do it?
You need to make sure your compairson also looks for an existing value within the VecPointer, since your original second loop only cares about the first value it comes across.
else if(VecPointer && input == "SomethingElse")
Don't look for ->empty(), as that's accessing the pointer and asking whether the pointed to vector is empty. If there's nothing to point to in the first place, you're going to have a bad time at the -> stage of the statement. Instead, if against it, since it's a pointer.
if(VecPointer)
Finally, you're using a Vector to save that one value from m_vec, which from other code I'm assuming is not a vector<vector<string>> but a vector<string> - in the latter case, your VecPointer should be std::string*
std::string* VecPointer = nullptr;
I'm trying to optimize a part of it.
...
Is this okay?
Maybe not! This may already be a poor use of your time. Are you sure that this is what's hurting your performance? Or that there's a performance problem at all?
Remember Don Knuth's old adage: Premature optimization is the root of all evil...
Do you see a better way to do it?
Consider profiling your program to see which parts actually take up the most time.
On an unrelated note, you could use standard library algorithms to simplify your (unoptimized) code. For example:
if (std::ranges::find(std::begin(object.m_vec) std::end(object.m_vec), "SomethingElse"s )
!= std::end(object.m_vec))
{
xml << "<OtherNode>" << whatever << "</OtherNode>";
}

Why are my variables giving a different value when they are printed outside of the class I set them in?

I am making fairly new to C++ and I am using it to make a text based game for a school project. However during the first section of the game the player answers questions by entering the number shown beside the answer they choose. However when I tested the variables the input going to using std::cout they return different values depending on where they are outputted. If I outputted them in the class I am using to set them (Introduction) the they return the correct value such as 1 or 3 etc. However when I output them in any file other than Introduction.cpp, the value displayed is -858993460 for all of the values. I get the same result from Main.cpp when I call them in my main function and if I call them from another function in a different class to Introduction.
This is an example of some of the code used to get input from the user:
void Introduction::CharacterCreation()
{
Universal universal;
std::fstream creation("Introduction_CharacterCreation.txt");
universal.line = 5;
for (int i = 0; i < universal.line; i++)
{
if (i > 0)
{
std::getline(creation, universal.displayText);
std::cout << universal.displayText << std::endl;
}
if (i == 4)
{
std::cout << std::endl;
std::cin >> universal.gender;
while (universal.gender <= 0 || universal.gender >= 3)
{
std::cout << "Please make a valid choice" << std::endl;
std::cin >> universal.gender;
}
}
}
// Code cut out here
}
The gender variable is an int declared in the Universal class, and the user is prompted to enter 1 for male or 2 for female by text pulled from a separate file. If the input is not 1 or 2 then a while loop forces the player to keep re-answering the question until they enter 1 or 2. The line variable is also an int however that is used for the for loops to ensure the right lines are read by the program.
To output the gender variable this is the code I use:
std::cout << gender << std::endl;
There is no universal. as it is being called within the Universal class itself.
This has confused me massively and I can't get my head around what is causing the problem. Any help or explanation would be great, thanks in advance.
Short answer: you're declaring a Universal object in your CharacterCreation() method. When this function exits since the scope of the universal variable was local so the entire object is destroyed.
Whatever you are outputting is just uninitialized garbage. It could really be any number depending on what system is compiling / running the program. To test this right after you input the gender, while still inside the function, try running
std::cout << universal.gender << std::endl;
This should output just fine.
There are a lot of ways you can go about fixing this. Since you didn't post where you call this method or your Universal class I can't say for sure. But I can speculate one such solution which is to declare the Universal object outside the method and then pass it in as a parameter:
Universal universal = Universal();
Introduction::CharacterCreation(universal);
std::cout << universal.gender << std::endl;
And just declare your function header to accept a Universal object:
void Introduction::CharacterCreation(Universal & universal)
{
//code here
}

How do you remove a object from a list in C++?

This is where I'm having the problem. I need to remove a object from the array once the creature dies.
for each(BaseObject monster in MonsterList)
{
if(MonsterSelect == monster.GetName())
{
Damage = Player.GetATK() - monster.GetDEF();
monster.SetHP(monster.GetHP() - Damage);
cout << monster.GetName() << " has taken " << Damage << "." << endl;
if(monster.GetAliveFlag() == false)
{
cout << monster.GetName() << " has died." << endl;
MonsterList.remove(monster);//This is where the object needs to be removed.
int sdf = 234;
}
}
}
You should use an ordinary for loop with iterators and apply method erase for the list.
Something like this
for ( auto it = MonsterList.begin(); it != MonsterList.end(); )
{
//...
if( MonsterSelect == it->GetName())
{
//,,,
if( it->GetAliveFlag() == false)
{
it = MonsterList.erase( it );
//...
}
else
{
++it;
}
}
else
{
++it;
}
}
You can reformat the loop that it would be more readable.
I suppose that each time when the current monster satisfies some condition you have to remove only this one monster in the list.
As for method remove then it removes all elements of the list that are equal to the given monster.
I'm going to interpret this as asking how you'd accomplish this basic task in decently written C++.
First, I'd get rid of the for loop. Second, I'd move most of the logic into one class or the other. Right now, these look like quasi-classes to me--i.e., all the real logic is in separate code operating on a couple of what are basically dumb data objects, disguised as classes by using accessors to get at the raw data.
So, if you were doing this in actual C++, you might end up with a map (or multimap) to let you find monsters by name. So, you'd have something like this:
auto monster = monsters.find(monsterSelect);
if (monster == monsters.end())
// Not found
if (monster.kill(player.attack())) {
cout << monsterSelect << " has died.\n";
monsters.erase(monster);
}
For the moment, I'm assuming there's really only a single monster by any one name. If this might really be an attack against multiple monsters, you have a couple of choices.
The preferred one (at least IMO) is to decouple the "battle the monster" part from the "remove dead monster(s) from the list" part. I'd probably use a Boost filter-iterator to iterate across all the monsters by a given name, then the remove/erase idiom to remove all the dead monsters from the list. (Note, however, that the remove/erase idiom is specific to sequential collections, and not suitable for associative containers).

Resetting a loop in a loop

I am very curious to learn why the below code does not run in a continuous loop. And I'm also looking for some ways to achieve what I want to achieve--which is resetting a loop inside of the loop. I need to do this because I need to account for each element in a container. The reason why this is because I might start off in the middle, and need to loop back around to check the others / and need to recheck other information too. So on with my little test example:
for ( int i = 0; i != 10; i++ ) {
std::cout << std::endl << "TEST: " << i << std::endl;
if ( i++ == 10 ) {
i = 0;
} else {
i--;
}
}
Is there any particular reason why the above does not work? I am very interested in knowing why, so I can learn how everything works. This also leads into a much bigger problem I am facing. Which is the below code. I am using MSVC++ 2010 Express. Also, this is one thread, so other data is not accessing it. It is an unordered_map using STL. its size if 2 (i checked).
for (game_player_client_map::const_iterator it = gpc_map_ptr->begin(); it != gpc_map_ptr->end(); ++it) {
if ( it++ == gpc_map_ptr->end() ) {
cout << endl << "IT == gpc_map_ptr->end()" << endl;
it = gpc_map_ptr->begin();
} else {
it--;
}
}
I appreciate any feedback SO has to offer, and any new things to learn :-) If further information is needed I will provide. Thank you for your time.
Because the condition is checked before the body of the loop is entered. When i == 10, the loop is broken, before your code can execute at the time that i++ would evaluate to 10.
Remember that postincrement increments the variable and returns the old value. So if i is 9, i++ evaluates to 9 also, but the next time you use i, it will be 10.
If you want the variable to be incremented and use the new value in an expression, use preincrement:
if (++i == 10) // changes i to i + 1 and checks if the new value of i is 10
You could completely ditch the increment however, and just use i + 1. That way you don't have to de-increment i in the else block.
Your misunderstanding of postincrement is probably also the source of the bug in the second block of code you posted. You can change it to preincrement, or if it is a random-access iterator, you can do the same thing as mentioned above and check if it + 1 == gpc_map_ptr->end() and not have to de-increment it in the else block.

Program Crashes when I try to compare two strings in c++?

int removeContact(Contact *newPtr, int runningTotal)
{
//define variables
string remove;
int next;
//prompt user for name they wish to remove
cout << "Enter the name you would like to delete (Last name first): ";
cin.ignore();
getline(cin, remove);
for (int t=0; t<=runningTotal; t++)
{
if (remove.compare(newPtr[t].name) == 0)
{
//calls function moveArrayElements function
moveArrayElements(newPtr, runningTotal, t);
//decrement runningTotal
runningTotal--;
//prompt user contact was found
cout << "Contact was found and deleted!";
next=1;
}
}
if(next!=1)
{
cout<< "ERROR: Contact was not found!";
}
return runningTotal;
}
This function is apart of a larger c++ program that is designed to manage a persons contact information. This function is suppose to remove a contact.
The problem I'm have is with the if (remove.compare(newPtr[t].name) == 0) statement. When my program gets to this part of the code it will crash without giving any errors. I tried straight up comparing both the stings with == operator, but this still results in a crash of my program...
Also, what make this so strange is that this code works perfectly when the function is called while my program is running with the contact that I'm trying to remove not stored in a text file.
However, when I close my program, and load my contact information from the text file, my program will crash... I know that my program is reading the file into the proper string array because I have a print function, so I know that all of my contacts are being transferred into the proper structure arrays...
Any ideas on how I can fix this? Any help would be appreciated! Thanks
UPDATE: I took the suggestions in the comments and changed my for loop to
t<runningTotal;
However, when I do this my program doesn't crash, but it wont's compare the strings...
If runningTotal is the size of your array, then the range of valid elements is [0, runningTotal). In the below code, you're looping up to runningTotal inclusive, when there isn't a valid Contact object there:
for (int t=0; t<=runningTotal; t++)
{
if (remove.compare(newPtr[t].name) == 0)
Therefore when you go and dereference newPtr[t], for t = runningTotal and then try and get the name element, you'll be causing undefined behaviour and may cause a crash.
Try changing t<=runningTotal to t < runningTotal to see if the problem goes away.
Also, is there a reason why you're using arrays as opposed to an std::vector?
i guess the for statement should be:
for (int t=0; t<runningTotal; t++)