【问题标题】:How do I increment an integer by the user's input in C++ within a loop and keep it incremented when the loop starts over?如何在循环中通过用户在 C++ 中的输入来递增整数,并在循环重新开始时保持递增?
【发布时间】:2015-09-26 01:15:50
【问题描述】:

这是四家出租车公司的综合出租车计划代码的一部分。当用户将商品添加到他们的购物车并计算所选公司的成本时,我需要增加每个出租车公司。最后程序需要返回主菜单。谢谢!

#include <iostream>    
using namespace std;    
int main()
{
    cout<<"Welcome to Lawrence Aggregation Service"<< endl;
    //main menu
    int menu_select;
    cout<< "Please select from the following options"<< endl
        << "Purchase mileage vouchers from:"<< endl
        << "1 - Checker Cab"<< endl
        << "2 - GTS Lawrence"<< endl
        << "3 - Jayhawk Taxi"<< endl
        << "4 - RedyCab"<< endl
        << "Or"<< endl
        << "5 - View Cart"<< endl
        << "6 - Exit" << endl;
    cin>> menu_select;

    while ((menu_select) >=1 && (menu_select) <= 6)
    {    
        //user's menu choice
        //cab companies for incrementing miles
        int checker_sum;
        int checker;
        int gts;
        int jhawk;
        int redyc;

        //taxi company selection
        if (menu_select >= 1 && menu_select <= 4)
        {
            int mileage;
            cout<< "How many miles would you like to purchase?"<< endl;
            cin>> mileage;

            //checker cab selection
            if (menu_select == 1)
            {
                int menu_opt;
                double price_flat;
                double price_miles;
                double mileage_cost;
                price_flat = 30.00;
                price_miles = 0.00;
                mileage_cost = mileage* price_miles + price_flat;
                checker_sum = (checker + mileage);
                cout<< "That will be $" <<(mileage_cost)<<
                    " with Checker Cab. Thank you."<< endl<< endl;
                cout<<"You have " <<checker<< " miles with Checker cab."<< endl;    
            }

            //gts lawrence selection
            else if (menu_select == 2)
            {
                int menu_opt;
                double price_flat;
                double price_miles;
                double mileage_cost;
                price_flat = 5.00;
                price_miles = .20;
                mileage_cost = mileage* price_miles + price_flat;
                cout<< "That will be $" << mileage_cost <<
                    " with GTS Lawrence. Thank you."<< endl<< endl;

            }
            else if (menu_select == 3)
            {
                int menu_opt;
                double price_flat;
                double price_miles;
                double mileage_cost;
                price_flat = 10.00;
                price_miles = .05;
                mileage_cost = mileage* price_miles + price_flat;
                cout<< "That will be $" << mileage_cost <<
                    " with Jayhawk Taxi. Thank you."<< endl<< endl;

            }

            else if (menu_select == 4)
            {
                int menu_opt;
                double price_flat;
                double price_miles;
                double mileage_cost;
                price_flat = 0;
                price_miles = 1.10;
                mileage_cost = mileage* price_miles + price_flat;
                cout<< "That will be $" << mileage_cost <<
                    " with RedyCab. Thank you."<< endl<< endl;
            }
        }
    }
    return 0;
}

【问题讨论】:

  • 您能解释一下您在当前代码中“增加出租车公司”的位置吗?
  • 提供一个您想看到的示例流程可能会有所帮助。
  • 你真的应该从设计开始,而不是从代码开始。你的数据结构在哪里?
  • 如果希望变量的值在循环迭代之间保持不变,则需要在循环之前在外部范围内声明它。
  • 你建议如何实现view_cart功能?想想看。然后你会发现你的程序中的缺陷。我希望你熟悉数组和结构的基本概念。您必须单独存储每个订单。请考虑为订单使用struct。该结构将包含里程代金券订单的特定详细信息,以及一个计数变量,该变量将在为该特定里程代金券下订单时增加。

标签: c++ loops increment


【解决方案1】:

对于初学者,您应该声明:

int checker_sum;
    int checker;
    int gts;
    int jhawk;
    int redyc;

在while循环之外,这样变量就不会被多次声明

至于你提到的增量,你可以做的是创建一个变量:

int totalCost;

您必须在任何 while 循环之外的 main 中的某处声明此变量。例如:

int main()
{
     int totalCost = 0;
     cout<<"Welcome to Lawrence Aggregation Service"<< endl;
    //main menu
    int menu_select;
    cout<< "Please select from the following options"<< endl;

您必须这样做才能更改任何循环中总成本的值;

一旦你声明了这一点,你可以做的是:

if (menu_select == 1)
        {
            int menu_opt;
            double price_flat;
            double price_miles;
            double mileage_cost;
            price_flat = 30.00;
            price_miles = 0.00;
            mileage_cost = mileage* price_miles + price_flat;
            checker_sum = (checker + mileage);
            cout<< "That will be $" <<(mileage_cost)<<
                " with Checker Cab. Thank you."<< endl<< endl;
            cout<<"You have " <<checker<< " miles with Checker cab."<< endl;
            totalCost = totalCost + mileage_cost
            // you could also do totalCost += mileage_cost

        }

如果您在每个循环中都执行此操作(因为您在所有循环之外声明了该变量),它会将您支付的所有款项添加到该变量中。然后当用户想要查看他们的购物车时,您可以输入:

else if(menu_select == 5)
{
      //whatever code you want
      cout << "Total: " << totalCost;
      //whatever code you want
}

您想要循环回主菜单的方法是将整个内容放入一个while循环中:

#include<iostream>

 using namespace std;

 int main()
 {
     int totalCost = 0;
     cout<<"Welcome to Lawrence Aggregation Service"<< endl;
     //main menu
     int menu_select;
while(menu_select != 6)
{
cout<< "Please select from the following options"<< endl
    << "Purchase mileage vouchers from:"<< endl
    << "1 - Checker Cab"<< endl
    << "2 - GTS Lawrence"<< endl
    << "3 - Jayhawk Taxi"<< endl
    << "4 - RedyCab"<< endl
    << "Or"<< endl
    << "5 - View Cart"<< endl
    << "6 - Exit" << endl;
cin>> menu_select;
//user's menu choice
    //cab companies for incrementing miles
    int checker_sum;
    int checker;
    int gts;
    int jhawk;
    int redyc;
while ((menu_select) >=1 && (menu_select) <= 6)
{
    //taxi company selection
    if (menu_select >= 1 && menu_select <= 4)
    {
        int mileage;
        cout<< "How many miles would you like to purchase?"<< endl;
        cin>> mileage;

        //checker cab selection
        if (menu_select == 1)
        {
            int menu_opt;
            double price_flat;
            double price_miles;
            double mileage_cost;
            price_flat = 30.00;
            price_miles = 0.00;
            mileage_cost = mileage* price_miles + price_flat;
            checker_sum = (checker + mileage);
            cout<< "That will be $" <<(mileage_cost)<<
                " with Checker Cab. Thank you."<< endl<< endl;
            cout<<"You have " <<checker<< " miles with Checker cab."         << endl;
            totalCost = totalCost + mileage_cost;
            // or totalCost += mileage_cost;

        }

        //gts lawrence selection
        else if (menu_select == 2)
        {
            int menu_opt;
            double price_flat;
            double price_miles;
            double mileage_cost;
            price_flat = 5.00;
            price_miles = .20;
            mileage_cost = mileage* price_miles + price_flat;
            cout<< "That will be $" << mileage_cost <<
                " with GTS Lawrence. Thank you."<< endl<< endl;
           totalCost = totalCost + mileage_cost;
            // or totalCost += mileage_cost;

        }
        else if (menu_select == 3)
        {
            int menu_opt;
            double price_flat;
            double price_miles;
            double mileage_cost;
            price_flat = 10.00;
            price_miles = .05;
            mileage_cost = mileage* price_miles + price_flat;
            cout<< "That will be $" << mileage_cost <<
                " with Jayhawk Taxi. Thank you."<< endl<< endl;
            totalCost = totalCost + mileage_cost;
            // or totalCost += mileage_cost;

        }

        else if (menu_select == 4)
        {
            int menu_opt;
            double price_flat;
            double price_miles;
            double mileage_cost;
            price_flat = 0;
            price_miles = 1.10;
            mileage_cost = mileage* price_miles + price_flat;
            cout<< "That will be $" << mileage_cost <<
                " with RedyCab. Thank you."<< endl<< endl;
            totalCost = totalCost + mileage_cost;
            // or totalCost += mileage_cost;
        }
        else if(menu_select == 5)
        {
            /whatever code you want
           cout << "Total: " << totalCost;
           //whatever code you want
        }
     }
}
}
return 0;

}

请注意我是如何将 int menu_select 移动到 while 循环上方的,因为如果它位于 while 循环下方的任何位置,那么编译器将报告错误,因为您在声明之前使用了 menu_select

我希望这对您有所帮助,并祝您编码愉快:)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    相关资源
    最近更新 更多