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