Can't validate player's input.

Dec 14, 2014 at 10:24pm
So I'm trying to create a mini inventory shop and I'm trying to validate if what's the user inputs is actually in the shop. What do you think I could or what should I modify in my program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>

using namespace std;

const int items = 5;
const string ShopInventory[items] = {"Helms", "Boots", "Swords", "Axes", "Leather Armors"};
const int ItemPrices[items]= {10, 5, 20, 30, 50};

void PrintShop();
string PlayerInput(string [], int&);

int main()
{
    string PlayerInventory[items];
    int playerMoney = 100;
    char Response;

    cout << "\t\tWelcome traveler to my lovely shop. \n";

    cout << "\nFeel free to browse many of the wonderful items within";
    cout << " this store. " << endl;

    PrintShop();

    while(toupper(Response) != 'N')
    {
        cout << PlayerInput(PlayerInventory, playerMoney);

        cout << "\nIs there anything else that you would like to buy? " << endl;
        cout << "\nEnter n or N to quit. Else enter y or Y   ";
        cin >> Response;

        while(toupper(Response) != 'Y' && toupper(Response) != 'N')
        {
            cerr << "\nSorry, wrong input. Please try again.  ";
            cin >> Response;
        }
    }

    return 0;
}

void PrintShop()
{
    cout << endl;

    cout << "     ***** Shop Inventory ***** " << endl << endl;

    cout << "Shop items " << "\t\tPrice Per Item. " << endl << endl;

    for(int i=0; i<items; i++)
    {
        cout << i+1 << ".) " << left << setw(22) << ShopInventory[i];
        cout << left << setw(3) << ItemPrices[i] << " Gold" << endl;
    }
}

string PlayerInput(string PlayerInventory[], int &playerMoney)
{
    string Input;

    cout << "\nEnter what you would like to buy. ";
    getline(cin ,Input, '\n');

    for(int i=0; i<items; i++)
    {
        while(Input != ShopInventory[i])
        {
            cout << "\nThe item that you enter isn't in my inventory. ";
            cin >> Input;
        }
    }
}
Last edited on Dec 14, 2014 at 11:22pm
Dec 14, 2014 at 11:24pm
All right now I'm able to validate the user's input but the user's input is wrong no matter what. Any advice?
Dec 14, 2014 at 11:28pm
1
2
3
4
5
6
7
8
9
std::string input;
std::cout << "\nEnter what you would like to buy.\n";
std::getline(std::cin >> std::ws, input);
while(std::find(std::begin(ShopInventory), std::end(ShopInventory),
                input) == std::end(ShopInventory)) {
    std::cout << "\nThe item that you enter isn't in my inventory.\n";
    std::getline(std::cin >> std::ws, input);
}
return input;
Dec 15, 2014 at 12:09am
Still getting an error. It's no matching function call.
Last edited on Dec 15, 2014 at 12:10am
Dec 15, 2014 at 12:29am
Post complete error message and corresponding code (or, better, whole code). I can tell at least 5 reasons why this error can happen.

Did you #include <algorithm>
Dec 15, 2014 at 12:32am
oh I didn't include it and the error message was related to the code that you gave me(line5). Though it was probably because I didn't include the algorithm file.
Dec 15, 2014 at 12:35am
Alright it works. What I found weird was that I thought you were using the find member function from the string header file. I didn't know the algorithm had this member function. I also want to know if there is another way to do what you just did.
Last edited on Dec 15, 2014 at 12:38am
Dec 15, 2014 at 12:43am
if there is another way to do what you just did.
Basic algorithm is the same:
1) Get input
2) Check if input is in array of possible inputs
3) If so, return it
4) Else repeat from (1)
Topic archived. No new replies allowed.