Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何提示用户给他们在C++中重选程序的选项_C++ - Fatal编程技术网

我如何提示用户给他们在C++中重选程序的选项

我如何提示用户给他们在C++中重选程序的选项,c++,C++,该选项可能如下所示:要再次运行程序,请输入“y”,要退出,请输入“n”。在我的程序中,我要求用户输入包a、B或C。然后我根据不同的因素计算价格。但是我必须给用户选择另一个包的选项,然后重新运行整个程序 #include <iostream> using namespace std; int main() { bool finished = false; char choice; int choice_a = 995; int choice_b = 1995; int choice

该选项可能如下所示:要再次运行程序,请输入“y”,要退出,请输入“n”。在我的程序中,我要求用户输入包a、B或C。然后我根据不同的因素计算价格。但是我必须给用户选择另一个包的选项,然后重新运行整个程序

#include <iostream>

using namespace std;

int main() {
bool finished = false;
char choice;
int choice_a = 995;
int choice_b = 1995;
int choice_c = 3995;
int message_units;
int price;
bool selected = false;

do {

    do {          //Prompt user to enter package
        cout << "Which package do you choose (enter A, B or C)" << endl;

        cin >> choice;

        if (choice == 'A') { price = choice_a; selected = true; }
        else if (choice == 'B') { price = choice_b; selected = true; }
        else if (choice == 'C') { price = choice_c; selected = true; }
        cout << endl;
    }

    while (selected == false);
            //Prompt user to enter message units
    cout << "How many message units (enter 1 - 672)" << endl;

    cin >> message_units;

           //calculate message units
    if((message_units > 5) && (choice == 'A')){
        price += 100 * (message_units - 5);
    }
      if((message_units > 15) && (choice == 'B')){
        price += 50 * (message_units - 15);
    }



                    //Total Cost
    cout << "Your total cost is " << price/100 << "." << price%100 << 
试试这个:

#include <iostream>

using namespace std;

static const int choice_a = 995;
static const int choice_b = 1995;
static const int choice_c = 3995;

int main()
{
    char choice;
    int message_units;
    int price;

    do
    {
        // Prompt user to enter package
        do
        {
            cout << "Which package do you choose - A, B or C? Or X to exit" << endl;
            cin >> choice;
            cout << endl;

            switch (choice)
            {
                case 'a': 
                    choice = 'A'; 
                case 'A': 
                    price = choice_a;
                    break;

                case 'b': 
                    choice = 'B';
                case 'B': 
                    price = choice_b;
                    break;

                case 'c': 
                    choice = 'C';
                case 'C': 
                    price = choice_c;
                    break;

                case 'x':
                case 'X':
                    return 0;

                default:
                    continue;
            }

            break;
        }
        while (true);

        // Prompt user to enter message units
        cout << "How many message units (enter 1 - 672)" << endl;
        cin >> message_units;

        // calculate message units
        if ((message_units > 5) && (choice == 'A')) {
            price += (100 * (message_units - 5));
        }
        if ((message_units > 15) && (choice == 'B')) {
            price += (50 * (message_units - 15));
        }

        //Total Cost
        cout << "Your total cost is " << price/100 << "." << price%100 << endl;

        // Prompt user to enter another package
        cout << "Do you want to enter another package? (Y/N)" << endl;
        cin >> choice;
    }
    while ((choice == 'y') || (choice == 'Y'));

    return 0;
}

在main的内容周围放置一个无限循环,从cin读取一个字符串或bool,并添加一个中断条件。你能举个例子吗?“我从看中学到的更多。@SU3:declator在半小时前发布了基本相同的问题,然后以全名发布,但代码中的注释表明这是其他人对他的家庭作业问题的解决方案,您需要对此进行检查。”。他删除了那个问题。所以,这看起来像是试图让其他人做他的家庭作业。如果你不是unix这样的系统程序员,请避免使用goto。资料来源:
#include <iostream>

using namespace std;

static const int choice_a = 995;
static const int choice_b = 1995;
static const int choice_c = 3995;

int main()
{
    char choice;
    int message_units;
    int price;

    do
    {
        // Prompt user to enter package
        do
        {
            cout << "Which package do you choose - A, B or C? Or X to exit" << endl;
            cin >> choice;
            cout << endl;

            switch (choice)
            {
                case 'a': 
                    choice = 'A'; 
                case 'A': 
                    price = choice_a;
                    break;

                case 'b': 
                    choice = 'B';
                case 'B': 
                    price = choice_b;
                    break;

                case 'c': 
                    choice = 'C';
                case 'C': 
                    price = choice_c;
                    break;

                case 'x':
                case 'X':
                    return 0;

                default:
                    continue;
            }

            break;
        }
        while (true);

        // Prompt user to enter message units
        cout << "How many message units (enter 1 - 672)" << endl;
        cin >> message_units;

        // calculate message units
        if ((message_units > 5) && (choice == 'A')) {
            price += (100 * (message_units - 5));
        }
        if ((message_units > 15) && (choice == 'B')) {
            price += (50 * (message_units - 15));
        }

        //Total Cost
        cout << "Your total cost is " << price/100 << "." << price%100 << endl;

        // Prompt user to enter another package
        cout << "Do you want to enter another package? (Y/N)" << endl;
        cin >> choice;
    }
    while ((choice == 'y') || (choice == 'Y'));

    return 0;
}