c++ - 将结构数组流式传输到排序系统
问题描述
该程序的总体目的是为餐厅建立食品订购服务。该程序旨在从 .txt 文件中提取食品和价格,显示它们并让客户挑选商品,然后在屏幕上打印账单。除了 printCheck 函数之外,我已经让所有东西都可以工作了。
似乎程序在该函数运行之前就结束了。我的代码如下。
//模块 9 项目
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct menuItemType
{
string menuItem;
double menuPrice;
};
menuItemType menuList[3];
void getData(menuItemType menuList[3]);
void showMenu(menuItemType menuList[3]);
void printCheck(double);
int main(){
getData(menuList);
showMenu(menuList);
printCheck;
return 0;
}
void getData(menuItemType menuList[3]) //This pulls the items and prices from the text file into the array
{
//declare variables
ifstream in_stream;
int i;
//open txt file
in_stream.open("menuitems.txt");
// loop to pull items from txt file into struct array
for (i = 0; i < 3 ; i++){
in_stream >> menuList[i].menuItem >> menuList[i].menuPrice;
}
}
void showMenu(menuItemType menuList[3])//shows the user the menu items and tells them how to select them
{
int selection;
double total;
int sentinel = 9;
//Welcome message and instructions for how to order
cout << "Welcome To The Restaurant" << endl;
cout << "Please select the items you would like: " << endl;
cout << "Press 9 to complete your order." << endl;
//displaying items from array onto screen
cout << menuList[0].menuItem << " $" << menuList[0].menuPrice << endl;
cout << menuList[1].menuItem << " $" << menuList[1].menuPrice << endl;
cout << menuList[2].menuItem << " $" << menuList[2].menuPrice << endl;
//while loop to allow user to select as many items as needed, and calculate total
while (selection != sentinel){
cin >> selection;
if (selection == 1){
total += 2.45;
cout << "You've selected Bacon" << endl;
}
else if (selection == 2){
total += .99;
cout << "You've selected a Muffin" << endl;
}
else if (selection == 3) {
total += .50;
cout << "You've selected coffee" << endl;
}
}
}
void printCheck(double total)
{
double after_tax;
const double tax = .05;
cout << "Total Bill: $" << total << endl;
after_tax = total + (total * tax);
cout << setprecision(2) << "Total Check After Tax: $" << after_tax << endl;
}
解决方案
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
struct menuItemType
{
string menuItem;
double menuPrice;
};
menuItemType menuList[3];
void getData(menuItemType menuList[3]);
void showMenu(menuItemType menuList[3]);
void printCheck(double);
int main(){
getData(menuList);
double total = showMenu(menuList);
printCheck(total);
return 0;
}
void getData(menuItemType menuList[3]) //This pulls the items and prices from the text file into the array
{
//declare variables
ifstream in_stream;
int i;
//open txt file
in_stream.open("menuitems.txt");
// loop to pull items from txt file into struct array
for (i = 0; i < 3 ; i++){
in_stream >> menuList[i].menuItem >> menuList[i].menuPrice;
}
}
double showMenu(menuItemType menuList[3])//shows the user the menu items and tells them how to select them
{
int selection;
double total = 0.0;
int sentinel = 9;
//Welcome message and instructions for how to order
cout << "Welcome To The Restaurant" << endl;
cout << "Please select the items you would like: " << endl;
cout << "Press 9 to complete your order." << endl;
//displaying items from array onto screen
cout << menuList[0].menuItem << " $" << menuList[0].menuPrice << endl;
cout << menuList[1].menuItem << " $" << menuList[1].menuPrice << endl;
cout << menuList[2].menuItem << " $" << menuList[2].menuPrice << endl;
//while loop to allow user to select as many items as needed, and calculate total
do {
cin >> selection;
if (selection == 1){
total += 2.45;
cout << "You've selected Bacon" << endl;
}
else if (selection == 2){
total += .99;
cout << "You've selected a Muffin" << endl;
}
else if (selection == 3) {
total += .50;
cout << "You've selected coffee" << endl;
}
} while (selection != sentinel)
return total;
}
void printCheck(double total)
{
double after_tax;
const double tax = .05;
cout << "Total Bill: $" << total << endl;
after_tax = total + (total * tax);
cout << setprecision(2) << "Total Check After Tax: $" << after_tax << endl;
}
推荐阅读
- javascript - 如何获取现有的输入值,随机它们并将其放回
- powershell - Powershell 脚本 | 查找前两个八位字节的匹配项
- php - Pagination Prepared Statement 查询 2 题
- java - 如何使用 JUnit 测试 ArrayList 的 getter 和 setter 方法?
- c# - 在使用流式阅读器的使用语句中检查 null 的最佳方法是什么?
- r - 具有 (i,j) 值的矩阵中的操作,没有 for 或 while 循环
- r - R中的二维列表
- javascript - 专门匹配 html5 中运行可汗学院 CS 项目的对象
- c++ - 无限循环与无限递归。两者都未定义吗?
- sql - Sybase ASE - UPDATE 语句影响的记录是否比结果窗口显示的多?