首页 > 解决方案 > 将结构数组流式传输到排序系统

问题描述

该程序的总体目的是为餐厅建立食品订购服务。该程序旨在从 .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;


}

标签: c++

解决方案


#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;


}

推荐阅读