How do I compare a user input to items stored in an array

In this lab task, you have to create a program which simulates the process of Aruba Vending Machine placed in PAF-IAST Main block. First, set/initialize both coins of 5 rupees and 10 rupees in your program (i.e, how many coins are there in the machine at first that can be used for returning bills). Then take a user input in rupees, it should be in a multiple of 5 or 10 and not less than 0 and not greater than 100. Then provide with a menu of items, your machine has.

ID Name Price
100 Lays Rs. 20
101 Pepsi Rs. 50
.
.
So on

You must create an array of structure items (id, name, price, quantity).

Then when a user enters a correct id against an item it should decrement the quantity of that item and deduct the price from the user entered price. And asks whether user wants to buy more otherwise return remaining amount.

In case, user enter a wrong id, display an error message and say try again.

There must be an option for exiting, if user don’t want to buy anything.


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
#include <iostream>
#include <stdlib.h>
using namespace std;

struct Machine
{
	int id[5] = { 100,101,102,103,104 };
	string name[5] = {"Lays", "Pepsi","Fanta","Oreo","Sooper" };
	float price[5] = { 20,50,50,15,15 };
	float quantity[5] = { 10,10,10,10,10 };
};

void compareID()
{
	Machine p1;
	int ID;

	if (ID != p1.id[0] || ID != p1.id[1] || ID != p1.id[2] || ID != p1.id[3] || ID != p1.id[4])
	{
		cout << "ID not available. Please insert correct ID." << endl;
		
	}
	
}

int main()
{
	Machine p1;
	int Rs5coin = 100;
	int Rs10coin = 50;
	int bill;
	cout << "Welcome to Aruba vending machine."<<endl;
	cout << "ID  Name  Price  Quantity" << endl;
	cout << "100 Lays   20       10" << endl;
	cout << "101 Pepsi  50       10" << endl;
	cout << "102 Fanta  50       10" << endl;
	cout << "103 Oreo   15       10" << endl;
	cout << "104 Sooper 15       10" << endl;
	cout << "Insert Bill Amount: ";
	cin >> bill;

	if (bill > 100 || bill < 0)
	{
		cout << "Wrong bill inserted!!!(must be less than 100 and greater than 0)" << endl;
	}

	if (bill % 5 != 0 || bill % 10 != 0)

	{
		cout << "wrong bill inserted!!(must be multiple of 5 or 10)" << endl;
	}
}
Last edited on
Hello usedlube69,

Something you can work on while I look at your code:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Andy
Might be better to have an array of struct - rather than a struct of arrays.

To check if an id is valid, consider the revised compareID() which checks the provided it and returns true if valid and false if not. This can then be checked by the calling function.

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
#include <iostream>
using namespace std;

struct Machine {
	int id {};
	string name;
	float price {};
	int quantity {};
};

const Machine p1[] {{100, "Lays", 20, 10}, {101, "Pepsi", 50, 10}, {102, "Fanta", 50, 10}, {103, "Oreo", 15, 10}, {104, "Sooper", 15, 10}};

void display()
{
	for (const auto& m : p1)
		cout << m.id << ' ' << m.name << ' ' << m.price << ' ' << m.quantity << '\n';
}

bool compareID(int id)
{
	for (const auto& m : p1)
		if (m.id == id)
			return true;

	return false;
}

int main()
{
	int Rs5coin {100};
	int Rs10coin {50};
	int bill {};

	cout << "Welcome to Aruba vending machine." << endl;
	cout << "ID Name Price Quantity" << endl;
	display();

	cout << "Insert Bill Amount: ";
	cin >> bill;

	if (bill > 100 || bill < 0)
		cout << "Wrong bill inserted!!! (must be less than 100 and greater than 0)\n";
	else
		if (bill % 5 != 0 || bill % 10 != 0)
			cout << "Wrong bill inserted!! (must be multiple of 5 or 10)\n";
		else {
			// Amount OK
		}
}


is it fine now?
I updated the question
Hello usedlube69,

You say "is it fine now?". Does that mean it is working the way that you want or that you are not sure?

Working with the first code that you posted I have made some changes although it is still not the best way to deal with what you want.

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
#include <iostream>
#include <string>      // <--- Added.
//#include <stdlib.h>  // <--- Should be "<cstdlib>" and it is not used or needed here.

using namespace std;

constexpr int MAXSIZE{ 5 };

struct Machine
{
    int id[MAXSIZE] = { 100, 101, 102, 103, 104 };
    string name[MAXSIZE] = { "Lays", "Pepsi", "Fanta", "Oreo", "Sooper" };
    double price[MAXSIZE] = { 20, 50, 50, 15, 15 };  // <--- Changed. Should be an "int" though.
    double quantity[MAXSIZE] = { 10, 10, 10, 10, 10 };
};

void compareID()
{
    Machine p1;  // <--- Local variable to the function. It knows nothing about what was defined in "main".
    int ID{};    // <--- Needs a valid value B4 the if statement.

    if (ID != p1.id[0] && ID != p1.id[1] && ID != p1.id[2] && ID != p1.id[3] && ID != p1.id[4])
    {
        cerr << "\n     ID not available. Please insert correct ID.\n";
    }
}

int main()
{
    Machine p1;  // <--- Needs a better name. Never used in "main".
    int Rs5coin{ 100 };
    int Rs10coin{ 50 };
    int bill{};

    cout <<
        "\n"
        "Welcome to Aruba vending machine.\n\n"
        "ID  Name Price Quantity\n"
        << std::string(23, '-') << '\n' <<
        "100 Lays   20     10\n"
        "101 Pepsi  50     10\n"
        "102 Fanta  50     10\n"
        "103 Oreo   15     10\n"
        "104 Sooper 15     10\n"
        " Insert Bill Amount: ";
    cin >> bill;

    if (bill > 100 || bill < 1)  // <--- Changed.
    {
        cerr << "\n     Wrong bill inserted!!!(must be less than 100 and greater than 0)\n\n";
    }

    //if (bill % 5 != 0 || bill % 10 != 0)
    if (bill % 5 || bill % 10 && bill % 10 % 5)  // <--- Changed.
    {
        cerr << "\n     Wrong bill inserted!!(must be multiple of 5 or 10)\n\n";
    }

    compareID();

    return 0;  // <--- Not required, but makes a good break point.
}

The first thing I want you to notice is that I removed some extra blank lines and added a few.

When adding the blank lines this jumped out at me. In "main" youdefine an object of "Machine", but never use it then in the "compareID" function you define another object of "Machine" to check the ID number. This works because the struct is arrays, so it really does not matter right now.

After getting a better idea of the program I noticed that the struct defines a std::string, but you forgot or missed including the header file "<string>". The definition of string name[MAXSIZE] may work for now, but later something like std::cout >> products.name[1]; will be a problem when the "cout" does not know how to handle the "Std::string".

The suggestion that seeplus made about creating an array of structs is a better idea and easier to work with in a function like "display".

In your struct you create 2 arrays of "float"s. "double"s would be the better choice, but either is more than what you need because all your numbers are whole numbers.

In the "compareID" function the if condition does not work this way. When the compares are (!=) the logical AND (&&) is a most often needed over the logical OR (||).

When you get into "main" lines 30 - 33 are OK, but after that you are out of order based on the instructions.

Then take a user input in rupees, it should be in a multiple of 5 or 10 and not less than 0 and not greater than 100.

Then provide with a menu of items, your machine has.

ID Name Price
100 Lays Rs. 20
101 Pepsi Rs. 50

Also your list includes the quantity, which appears not to be needed.

As I thought about the code you ask the user to enter an amount. The problem is that is either or both error messages print put the program continues as if nothing is wrong. There is no way to back and enter a proper amount.

When you get to displaying the list it is missing the choice to cancel the transaction or exit the program. I am thinking that you would cancel the transaction, return any money entered and clear variables that are needed to be ready for the next transaction. This may just be my way of thinking about how the program should work.

After entering an ID number you would need to check if that number is valid. Also you should check it that product has a quantity greater than (0) zero. If you check the quantity at this point it will be less work dealing with it later.

Andy
Registered users can post here. Sign in or register to post.