-1

Hey Basically i have 2 functions:

void Inventory:: showInventory()
{
    char input[80];
    cin >> input;
    char inventoryRequest[] = "i";
    //compare the player input to inventoryRequest (i) to see if they want to
    //look at inventory.
    int invent = strcmp (input,inventoryRequest);

    if(invent == 0) {
        //vector<string> inventory;
        cout << "You have " << inventory.size() << " items.\n";
        cout << "\n******Inventory******";
        cout << "\nYour items:\n";

        for (int i= 0; i< inventory.size(); ++i) {
            cout<< inventory[i] << endl;
        }
    }

}

void Inventory :: displayInventory(const string str) {
    char input = 0;
    do
    {
        cout << str << endl;
        cin >> input;
    }
    while((input != 'i') && (input != 'I') && (input != 'n') && (input != 'N'));
    showInventory();
    //return input;
}

showInventory compares the player input to i. display inventory only lets the user press i or n. i to view the inventory and n to skip. But when i is pressed. It causes a double line.

Meaning i has to be pressed twice to view the inventory.

I have tried numerous things to stop this from occuring. But i have not succeeded and most of the time the inventory cannot be viewed at all.

Can anybody help me with this. Thanks in advance.

andre
  • 7,018
  • 4
  • 43
  • 75
Pendo826
  • 1,002
  • 18
  • 47
  • 2
    C++ doesn't work like that. You want to use `getline`. There are *hundreds* of posts here on SO pertaining to this problem. – Kerrek SB Nov 29 '12 at 21:53
  • 1
    It seems that the user has to press it once on displayInventory and again in showInventory. – imreal Nov 29 '12 at 21:55
  • @KerrekSB you mean use getline instead of cin? Nick I cant figure out a way to do it without the too inputs. – Pendo826 Nov 29 '12 at 21:57

1 Answers1

1

Try using a parameter for input on void Inventory::showInventory(), and eliminating the second cin, something like this:

void Inventory:: showInventory(char input)
{
    //char input[80];
    //cin >> input;
    //char inventoryRequest[] = "i";
    //int invent = strcmp (input,inventoryRequest);
    //compare the player input to inventoryRequest (i) to see if they want to look at inventory.
    //if(invent == 0)  // REPLACE THIS WITH THE LINE BELOW
    if(input == 'i')

And then when you call it, do it like this:

    showInventory(input);
imreal
  • 10,178
  • 2
  • 32
  • 48
  • Suprisingly it worked. I dont understand how because i had void Inventory:: showInventory(char input) { //char input[80]; //cin >> input; char inventoryRequest[] = "i"; //int invent = strcmp (input,inventoryRequest); //compare the player input to inventoryRequest (i) to see if they want to look at inventory. if(inventoryRequest == 'i') { //vector inventory; cout<< "You have " << inventory.size() << " items.\n"; cout << "\n******Inventory******"; cout<< "\nYour items:\n"; for (int i= 0; i< inventory.size(); ++i) cout<< inventory[i] << endl; } } and it didnt work. – Pendo826 Nov 29 '12 at 22:08
  • @Pendo826: That's because you claim your C is C++, give it some more care and this won't happen. – Tamara Wijsman Nov 29 '12 at 22:09
  • 1
    @TomWijsman Im only new to C++ so my way of doing things is still developing. – Pendo826 Nov 29 '12 at 22:10
  • 1
    @Pendo826 Tom is correct, you should try to read some modern C++ books to gain a better understanding of the language because doing things your way will do you no favors in the long run. Check out this on SO: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – peacemaker Nov 29 '12 at 22:21
  • 1
    @Pendo826: Avoid online learning resources at all costs; use well-known modern C++ books (as linked by peacemaker), as to not fall into the bad practice of writing C code in a C++ world that just makes things harder on you. It actually can distract you later on from paying attention to the actual program you're writing... You can search for things at the top right of http://en.cppreference.com/w/ and see whether they are C or C++. – Tamara Wijsman Nov 29 '12 at 22:25
  • Thanks for the advice guys. I have a book from murach's but its application based rather than games based. Games is the one i want. – Pendo826 Nov 29 '12 at 23:10