Here's the whole thing. There are some parts of it that I find kind of odd, such as asking the user if they want to order at the beginning and therefore manipulating the boolean variable, but I have to do that because I have to make my output look identical to a screen shot my teacher posted, which I have done. I've already turned it in so I'm mainly posting the whole thing in case anyone's curious and to get any input about things I could learn on my own to do things better as I'm learning. If posting whole programs like this is taboo let me know and I won't do so in the future.
Code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
// Size of any arrays
const int MENU_SIZE = 10;
const double TAX_RATE = .05;
// Structure for each menu item.
struct menuItem
{
string name;
double price;
};
// Function prototypes
void getData(ifstream& dataFile, menuItem menuList[]);
void showMenu(menuItem menuList[]);
void printCheck(menuItem menuList[], int orderList[]);
int main()
{
// Declares two arrays for computations and to store file data.
menuItem menuList[MENU_SIZE];
int orderList[MENU_SIZE]={0};
ifstream dataFile;
int menuChoice;
char yesNo;
bool decision;
// Opens necessary CSV file
dataFile.open("breakfast.csv");
// Checks to make sure file opened, displays message and ends program
// if not.
if (!dataFile)
{
cout << "File failed to open. Ending program." << endl;
return 1;
}
// Calls functions to get data from csv file and display menu, respectively.
getData(dataFile, menuList);
showMenu(menuList);
cout << endl << "You can make up to 10 single order selections." << endl;
cout << "Do you want to make a selection? Y/y (yes), N/n (no): ";
cin >> yesNo;
cout << endl;
if (yesNo == 'y' || yesNo == 'Y')
decision = true;
// Prompts user to order and keeps track of items ordered.
while (decision)
{
cout << "Enter item number: ";
cin >> menuChoice;
orderList[menuChoice - 1] += 1;
// Allows user an exit condition.
cout << endl << "Select another item? Y/y (yes), N/n (no): ";
cin >> yesNo;
cout << endl;
if (yesNo == 'n' || yesNo == 'N')
decision = false;
}
// Function call to print final output
printCheck(menuList, orderList);
// closes file
dataFile.close();
return 0;
}
// Function reads data from breakfast.csv into menuItem variables,
// then stores the menuItem variables in a list.
void getData(ifstream& dataFile, menuItem menuList[])
{
for (int z = 0; z < MENU_SIZE; z++)
{
getline(dataFile, menuList[z].name, '%');
dataFile >> menuList[z].price;
dataFile.ignore(1, '\n');
}
}
// Function to display the menu
void showMenu(menuItem list[])
{
cout << fixed << showpoint << setprecision(2);
cout << "Welcome to the Viking's Cafe!" << endl;
cout << "----Today's Menu----" << endl;
for (int z = 0; z < MENU_SIZE; z++)
{
cout << right << setw(2) << z + 1 << ": " << left << setw(50)
<< setfill('.') << list[z].name << " $" << list[z].price << endl;
}
}
// Function that computes total costs and displays final order
void printCheck(menuItem menuList[], int orderList[])
{
double subTotal = 0.0;
double taxTotal = 0.0;
double grandTotal = 0.0;
cout << "Welcome to the Viking's Cafe!" << endl;
for (int z = 0; z < MENU_SIZE; z++)
{
cout << fixed << showpoint << setprecision(2);
if (orderList[z] > 0)
{
cout << setw(50) << setfill('.') << menuList[z].name << " $"
<< menuList[z].price * orderList[z] << endl;
// Keeps track of subtotal
subTotal += (orderList[z] * menuList[z].price);
}
}
// Computes other necessary totals
taxTotal = subTotal * TAX_RATE;
grandTotal = subTotal + taxTotal;
// Displays user's final order total
cout << setw(50) << "Tax " << setfill('.') << " $" << taxTotal << endl;
cout << setw(50) << "Amount Due " << setfill('.') << " $" << grandTotal << endl;
}