-1

The option could look like: "To run the program again enter 'y', to exit enter 'n'. In my program I ask the user to enter a package A,B, or C. Then I calculate the price based on different factors. But I have to give the user the option to select another package an rerun the entire program?

#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 << 
decleator
  • 1
  • 1
  • 3
    Put an infinite loop around the contents of `main` and read a string (or a `bool`) from `cin` and add a breaking condition. – SU3 Jun 19 '15 at 01:06
  • could you show an example? I learn better by seeing it. – decleator Jun 19 '15 at 01:08
  • @SU3: "declator" posted essentially the same question half an hour ago or so, then under full name, but where comments in the code indicated that it was someone else's solution to his homework problem (like "you need to check this"). he deleted that question. so, it looks like an attempt to get others to do his homework. – Cheers and hth. - Alf Jun 19 '15 at 01:08

2 Answers2

0
#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;
    char rerunp;

rerunprog:
    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);
        }

        cout<<"Do you want to run again?(y/n)";
        cin >> rerunp;
        while(rerunp == 'y')
            goto rerunprog;

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

        cout<<"Do you want to run again?(y/n)";
        cin >> rerunp;
        while(rerunp == 'y')
            goto rerunprog;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sailesh Babu Doppalapudi
  • 1,534
  • 1
  • 10
  • 22
0

Try this:

#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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770