c++ - 我的决赛遇到了麻烦。代码不断循环回到我的订购菜单,不会显示我的总价

标签 c++

<分区>

我是编程新手。在我的期末考试中,我被告知要编写一个包含 if else while do 和数组的程序。我真的很困惑,为什么在我的程序中,我必须计算价格,它没有显示,它只是循环回到我的订购代码。有什么想法吗?

                cout <<"COKE \t\t\t-Php 30 \t" ;
                cout << setw (50) <<" Carbonated softdrink by Coca-Cola Company in 2.5L."<<endl;

                cout <<"SPRITE  \t\t-Php 30 \t";
                cout<< setw(50) <<" A colorless, lemon and lime-flavored soft drink in 2.5L."<<endl;

                cout <<"ICED TEA\t\t-Php 30 \t ";
                cout <<setw (20) <<"Houseblend iced tea in 1L pitcher."<<endl;

                cout <<"COFFEE \t\t\t-Php 25 \t";
                cout << setw (55) << " A brewed drink prepared from roasted coffee beans in a cup."<<endl;

                cout <<"BOTTLED WATER \t\t-Php 20 \t";
                cout <<setw (25) <<" A purified drinking water in 500mL." <<endl;
                cout <<"-----------------------------------------------------------------------------------------------------------------------" <<endl <<endl;

                cout <<"1 \t Order Now" <<endl;
                cout <<"0 \t Back" <<endl <<endl;

                cout <<"Enter the number of your choice: ";
                cin >> ow;

                    if (ow==1){

                        history[length] = "Order";
                        length += 1;
                        while(ow!=0){
                        system ("cls");

                        cout <<"\nRICE MEAL " <<setw (30) << "DRINKS\n" <<endl;
                        cout <<"Hotsilog \t-Php 45 \t" <<setw(15) <<"Coke \t\t\t-Php 30" <<endl;
                        cout <<"Tocilog \t-Php 45 \t" << setw(15) <<"Sprite  \t\t-Php 30" <<endl;
                        cout <<"Tapsilog \t-Php 45 \t" << setw(15) <<"Iced Tea \t\t-Php 30" << endl;
                        cout <<"Porksilog \t-Php 50 \t" << setw(15) <<"Coffee \t\t\t-Php 25" <<endl;
                        cout <<"Chickensilog \t-Php 55 \t" <<setw (15) <<"Bottled Water \t\t-Php 20" << endl;

                        string product [50];
                        string note [50];
                        int qty [10];
                        int price [10];
                        int e;
                        int totalprice = 0;

                        cout <<"\nNumber of Product Name you will need: ";
                        cin>> e;

                        cin.ignore();

                        cout <<"\nFill in the order form based on above.\n";
                        cout <<"Orders not mention above will be disregarded.\n\n";

                            for (int i=0; i<e; i++){
                                cout <<"Enter the 'Name' of the product you will buy: ";
                                getline (cin, product[i]);

                                cout <<"Additional Note (Press Enter if no additional note): ";
                                getline (cin, note [i]);

                                cout <<"Quantity: ";
                                cin >> qty[i] ;

                                cout <<"Price: ";
                                cin >> price[i];
                                cin.ignore();
                            system ("cls");
                             }
                            // system ("cls");

                            cout <<"Your Product's List " <<endl << endl;
                            for (int j=0; j<e; j++){
                                cout <<"Product Name: " << product [j] <<endl;
                                cout <<"Quantity:" << qty[j]<< endl;
                                cout <<"Price: "<< price [j] << endl <<endl;

                                    totalprice = totalprice + (qty [j]*price[j]);  

                                cout << "Total Price: " <<totalprice;
                            } \\This is the line that I am having trouble with, it won't show up when I enter the number of food I input.


                        }   
                    }

                }
        }
    }
    return 0;
}

如有任何帮助,我们将不胜感激!

最佳答案

要使文本更具可读性,函数是您最好的 friend ,变量名也非常重要。我对您的代码进行了一些修改,以便您可以观察到它产生的巨大差异:

#include <iostream>
using namespace std;

void display_main_menu();
void display_header();

int main() {
    int selected_option;

    display_main_menu();
    cin >> selected_option;

    if (selected_option == 1) {
        while(selected_option != 0) {
            string product [50];
            string note [50];
            int qty [10];
            int price [10];
            int e;
            int totalprice = 0;

            cout <<"\nNumber of Product Name you will need: ";
            cin >> e;

            cout <<"\nFill in the order form based on above.\n";
            cout <<"Orders not mention above will be disregarded.\n\n";

            for (int counter = 0; counter < e; counter++){
                cout <<"Enter the 'Name' of the product you will buy: ";
                getline (cin, product[counter]);

                cout <<"Additional Note (Press Enter if no additional note): ";
                getline (cin, note[counter]);

                cout <<"Quantity: ";
                cin >> qty[counter] ;

                cout <<"Price: ";
                cin >> price[counter];
                system ("cls");
            }

            cout <<"Your Product's List " << endl << endl;
            for (int counter = 0; counter < e; counter++){
                cout <<"Product Name: " << product[counter] <<endl;
                cout <<"Quantity:" << qty[counter]<< endl;
                cout <<"Price: "<< price[counter] << endl <<endl;

                totalprice += (qty[counter] * price[counter]);  
                cout << "Total Price: " << totalprice;
            }

            cin >> selected_option;
            system ("cls");
        }   
    }
    return 0;
}

void display_header() {
    system ("cls");
    cout <<"\nRICE MEAL " << "DRINKS\n" <<endl;
    cout <<"Hotsilog \t-Php 45 \t" <<"Coke \t\t\t-Php 30" <<endl;
    cout <<"Tocilog \t-Php 45 \t" <<"Sprite  \t\t-Php 30" <<endl;
    cout <<"Tapsilog \t-Php 45 \t" <<"Iced Tea \t\t-Php 30" << endl;
    cout <<"Porksilog \t-Php 50 \t" <<"Coffee \t\t\t-Php 25" <<endl;
    cout <<"Chickensilog \t-Php 55 \t" <<"Bottled Water \t\t-Php 20" << endl;
}

void display_main_menu() {
    cout <<"COKE \t\t\t-Php 30 \t" ;
    cout <<" Carbonated softdrink by Coca-Cola Company in 2.5L."<<endl;

    cout <<"SPRITE  \t\t-Php 30 \t";
    cout <<" A colorless, lemon and lime-flavored soft drink in 2.5L."<<endl;

    cout <<"ICED TEA\t\t-Php 30 \t ";
    cout <<"Houseblend iced tea in 1L pitcher."<<endl;

    cout <<"COFFEE \t\t\t-Php 25 \t";
    cout << " A brewed drink prepared from roasted coffee beans in a cup."<<endl;

    cout <<"BOTTLED WATER \t\t-Php 20 \t";
    cout <<" A purified drinking water in 500mL." <<endl;
    cout <<"-----------------------------------------------------------------------------------------------------------------------" <<endl <<endl;

    cout <<"1 \t Order Now" <<endl;
    cout <<"0 \t Back" <<endl <<endl;

    cout <<"Enter the number of your choice: ";
}

你可以进一步划分它来分离功能,我没有这样做是为了不让你用指针吓到你。

关于c++ - 我的决赛遇到了麻烦。代码不断循环回到我的订购菜单,不会显示我的总价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58797010/

相关文章:

c++ - 遍历 cv::Mat 中包含的 cv::Points

c++ - 获取缺陷对象的坐标值

c++ - 为什么我不必在 C++ 中重载赋值运算符?

c++ - 我可以对我的 conan 包的用户隐藏我的链接标志吗?

c++ - 将Qt qml文件转换为位图图像

c++ - 如何从参数化基函数生成多个函数指针?

C++:条件表达式中的 Always-Throw 函数

c++ - remove_if 删除 vector 中的所有内容

c++ - 初始化 std::unique_ptr 作为原始数组指针初始化

c++ - 我在这一行代码中的语法有什么问题(指针和引用以及取消引用哦,我的)?