Hello mercedx,
1) I am not sure how to have the array print out, instead, I am just using cout for a menu option |
The quick answer is tell it to. You pass the array to the "getChoice" function, but never use it. You could do something like this:
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
|
int getChoice(Drink machine[])
{
int choice;
std::cout << std::left; // <--- Added. You may not need this line.
do
{
cout << "\n Please select a drink or press 6 to exit\n";
for (size_t lc = 1; lc < NUM_DRINKS; lc++)
{
std::cout << ' ' << lc << ". " << std::setw(13) << machine[lc].name << '$' << machine[lc].price << '\n';
}
std::cout << " Enter selection: "; // <--- Added.
cin >> choice;
if (choice < 1 || choice > 6)
{
cout << "\n Please make a valid selection" << endl;
}
} while (choice < 1 || choice > 6);
return choice;
}
|
On line 13 notice how I used the array to get the name and price.
therefore The number of specific drinks in the "machine" is not being adjusted. |
For this part of problem one this would be taken care of in the "transaction" function.
2) To exit the program you have to enter "6" which is not working either, so im not sure if the total earnings is working either. |
As I mentioned earlier this has to do with line 40. When I changed "NUM_DRINKS" to 6 it works the way it should.
Now that I have had a chance to work with the program I have found:
The header files "algorithm" and "cctype" are never used in the program and are not needed. Removing them will make the program smaller. And will have no adverse affect on the program.
line 7 is best not to use. There have been many posts about this in the "beginners Forum". Just do a search. And there may even be an article or two in the "Articles Forum". You can try a search there also. This is always a good read
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
I rearranged the beginning of the program. It now looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std; // <--- Best not to use.
const int NUM_DRINKS = 6;
constexpr size_t MAXDRINKS{ 20 };
struct Drink
{
string name;
double price;
int quantity;
double qtySold{};
double amtReceived{};
};
int getChoice(const Drink machine[]);
void transaction(Drink (&machine)[NUM_DRINKS], int choice, double &totalMoneyReceived);
|
I moved the constant variables to the top so the entire file can use them.
On line 19 I made the array constant because you just need to use the array and there is no reason to make any changes here.
On line 20 I passed the array by reference. Passing the array by value the array will degrade to a pointer and the only part is is seen is the first element of the array. Passing by reference during testing has the advantage of being able to see the whole array. Passing the array by reference is not necessary and you can keep what you have, its just a help when developing the program.
In the struct the two variables I added the "qtySold" can be achieved through simple math, but "amountReceived" could be very useful in the final output.
In "main" the array machine now looks like this:
1 2 3 4 5 6 7 8 9
|
Drink machine[NUM_DRINKS] =
{
{ "Unused", 0.0, 0 },
{ "Cola", 0.65, MAXDRINKS },
{ "Root Beer ", 0.70, MAXDRINKS },
{ "Grape Soda", 0.75, MAXDRINKS },
{ "Lemon Lime", 0.85, MAXDRINKS },
{ "Water", 0.90, MAXDRINKS }
};
|
Doing this line 3 becomes a place holder for element zero which is not used. This way when you receive a value for "choice" it will become the correct subscript to the array.
After defining the array I changed the next line to
cout << fixed << std::showpoint << setprecision(2);
. I found that the "std::endl" was giving an extra blank line at the beginning of the output.
After the do/while loop I changed the next line to
cout << "\n\n Total money received is: $" << totalMoneyReceived << '\n' << endl;
. Notice the use of the "\n"s and that "totalMoneyReceived" is more descriptive than "earnings" as you have not earned anything yet just received money. You may be stuck with the variable name "earnings" and if that is what you have to use then use it.
After this I created a for loop that displays this.
Total money received is: $2.80
Cola sold 2 for $0.65 each for a total of $1.30
Root Beer sold 0 for $0.70 each for a total of $0.00
Grape Soda sold 2 for $0.75 each for a total of $1.50
Lemon Lime sold 0 for $0.85 each for a total of $0.00
Water sold 0 for $0.90 each for a total of $0.00
|
This my be more than what you want, but sounded good at the time.
In the function "transaction" I added
bool good{ false };
and a do/while loop. The loop will keep repeating if you enter something larger than $1.00 or if you enter less than the amount needed. It gives the opportunity to correct something that is wrong without going back to the main menu.
At some point you may consider a way to cancel the transaction and return to the main menu.
Like I showed you earlier I wrapped all the if statements inside an if statement. Then the second and third if statements I had to change to "else if" to make it work.
In the two "else if" statement you need to use
machine[choice].price
to refer to the proper element of the array. Also the same concept is used to change information in the struct based on the correct subscript which comes from the value of "choice".
Inside the last "else if" statement is where you work with the array to make changes. If you are not sure how to do this, show me the code of what you think and we can work from there.
The last line of the last "else if" statement is
good = true;
to allow you to exit the do/while loop and return to "main" to make another selection.
Remember in this program for loops start at "1" and not "0" (zero) to work with array properly. This will save you from doing
choice - 1
to access the array.
Hope that helps,
Andy