GET NEIL DEGRASSES TYSON, I ripped a hole with this one...


Joined
Nov 10, 2022
Messages
1
Reaction score
0
Hey all. First post in this entire forum. Please redirect me if this post is better suited somewhere else.

Anyway contrary to the title, I probably have something very dumb that I just need a second set of eyes on. I'm going to be incredibly honest my IDE is Bloodshed DEV C++ on an old del laptop with a an XP install. (It's not worth explaining...) But I say that to say I'm still happy/capable to provide source, outputs, screens whatever. I'm super casual however user, nowhere near even regular practice fyi. So, there are probably much more efficient ways for me to do things.

Ok.... screenshot 1 (fruitFine) is the output of a simple program I wrote in a test environment before implementing it as an "equip weapon" method for a verrrrry simple console RPG I'm making. The super high level is take a vector of structs, iterate through / and or cout the results, cin a selection, iterate the vector again, see if the cin matches any of the string names of the structs in iteration, if yes great equip it, else cout "I don't have that" - they either don't have the item, or more likely user made a typo...

The first program I'm calling Fruit Stand and it does all the above.

(You'll see there are a few if/else scenarios broken up within/out of for loops. I did this because otherwise it would display/run the unintended Else cases every iteration - usually resulting in a cout of "I don't have that item. I don't have that item. You equip Mace. I don't have that item." - depending on the iteration and variables at the time).

So the fruit programs works EXACTLY as intended... I can pick any fruit at any point in the array with a getline string, add it to my "cart" (vector).

The second screen (lol title is NSFW), is the output from the RPG in progress where the Fruit Stand concept was implemented. As you can see, the FIRST time I make a selection to equip weapons, it almost seems to skip the first for loop. After that it works fine!!!! I can swap weapons in and out no problem, then view my stats in another method to verify the weapon change! It's just the FIRST damn time for some reason and it's driving me crazy!

Below is the source code for the ENTIRE Fruit Stand Program. (There's a few additional libraries - just kinda my default. Same in the RPG proper). Aside from some arbitrary variable/container name changes, different couts/pauses - as far as I can tell it's logically the same. It even functions EXACTLY the same - just after the first time!?!

I seem to remember something in the case of getline(); function where you had to set a wait period? So that it just doesn't run the method with no valid data? But then why does the same issue not occur with Fruit Stand? And why does the error NOT continue after one run of the method's operation?

Here's the entire Fruit Stand program. Below is the bad method it's emulating in the big RPG project. Not sure if it would help, but below the bad method in question, is the method that's calling it... It's an Inventory in progress. I apologize for lack of comments... It's a bad habit of mine, but everything is relatively simple/as intended.

// FRUIT STAND -------------------------------------------------------------------------------------

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <conio.h>

using namespace std;


struct fruit
{
string name;
int value;
int weight;
};



int main(int argc, char *argv[])
{


fruit apple;
fruit orange;
fruit grape;
fruit notFruit;

apple.name = "Apple";
apple.value = 30;
apple.weight = 10;

orange.name = "Orange";
orange.value = 40;
orange.weight = 20;

grape.name = "Grape";
grape.value = 5;
grape.weight = 1;

notFruit.name = "Not Fruit"; // I wanted a struct that would let me test getline(); for input outside just string.
notFruit.value = 5;
notFruit.weight = 1;

vector<fruit> stand;

vector<fruit> cart;

stand.push_back(apple);
stand.push_back(orange);
stand.push_back(grape);
stand.push_back(notFruit);

bool choiceMade = false;
while (choiceMade == false)
{
for(int i=0; i<stand.size(); i++)
{
cout << stand.name << "\n";
};

string answer;
cout << "\nwhat fruit do you want?\n\n";
getline(cin, answer);

for(int i=0; i<stand.size(); i++)
{
if(answer == stand.name)
{
cout << "\nYou picked " << stand.name << "\n\n";
cart.push_back(stand);
cout << cart[0].name << "\n\n";
choiceMade = true;
};
};
if (choiceMade != true)
{
cout << "\nI don't have that.\n";
};



};


system("PAUSE");
return EXIT_SUCCESS;
}


// BAD METHOD IN QUESTION -------------------------------------------------------------------------------------

void equipWeapon (hero &player, vector<weapon> myWeapons)
{

bool choiceMade = false;
while (choiceMade == false)
{
string answer;
cout << "\nWhich weapon?\n\n";
getline(cin, answer);

for(int i=0; i<myWeapons.size(); i++)
{
if(answer == myWeapons.name)
{
player.myWeapon = myWeapons;
cout << "\nYou equiped " << myWeapons.name << "\n\n";
choiceMade = true;
system("PAUSE");
};
};

if (choiceMade != true)
{
cout << "\nI don't have that.\n";
system("PAUSE");
};

};

};


// INVENTORY METHOD THAT'S CALLING THE ABOVE BAD METHOD IN QUESTION ----------------------------------------------------------

void inventory(hero &player, vector<weapon> myWeapons, vector<armor> myArmor, vector<consumable> myConsumable) // inventory;
{
bool leave = false;

while(leave == false) // while we dont want to leave the method
{

system("CLS"); // clear the screen
cout << "Weapons:\n";
for(int i=0; i<myWeapons.size(); i++)
{
cout << myWeapons.name << "\n";
};
cout << "\nArmor:\n";
for(int i=0; i<myArmor.size(); i++)
{
cout << myArmor.name << "\n";
};
cout << "\nConsumable:\n";
for(int i=0; i<myConsumable.size(); i++)
{
cout << myConsumable.name << "\n";
};
cout << endl;

int choice = -1;

while(choice < 0 | choice > 3)
{
cout << "(0) Do nothing (1) Equip Weapon (2) Equip Armpr (3) Use consumable \n\n";
choice = getResponce(); // get the responce from the user
switch (choice)
{
case (0):
leave = true;
break;
case (1):
equipWeapon (player, myWeapons);
break;
case (2):
// equipArmor
break;
case (3):
// useConsumable
break;

};

};

}
};
 

Attachments

  • fruitFine.JPG
    fruitFine.JPG
    19.5 KB · Views: 2
  • weapons!Fine.jpg
    weapons!Fine.jpg
    70.3 KB · Views: 3
Last edited:
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

I need help 1
How to keep count of right answer and wrong answers in C++? 0
Codeforces problem 0
Crossword 14
TF-IDF 1
Crossword 2
What am I doing wrong? 1
sorting char array 13

Top