How would I prompt the user to give them the option to rerun program in C++

0 aj burnett · June 19, 2015
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 <<
          
                  


Post a Reply

Replies

Oldest  Newest  Rating
+1 c student · June 19, 2015
Do not use goto.  It is highly unnecessary and is bad programming practice in this situtation.
0 Alper Akgoz · June 23, 2015
Using goto in a program is like using comic sans in a resume.
0 Johnny Appleseed · June 23, 2015
This is how I usually do it. (without needing bool finished)

int main(){

char choice;

do{

//Your code

cout << "Repeat (y/n)?  ";

cin >> repeat;

if(repeat=='n'){ return 0;}

} while(repeat=='');

}
0 c student · June 19, 2015
Loop it with a condition as you've done already.
0 Scott Walker · June 19, 2015
 
#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 <<
"To run the program again enter 'y', to exit enter 'n' 

cin >> choice;

if(choice == 'Y' || choice == 'y')
{
finished = false;
}
else if(choice == 'N' || choice == 'n')                                            //Logic for repeating and breaking loop
{
finished = true;
}

} while(finished == false)
0 James Juan · June 19, 2015
, .use goto statement

void main()
{
char choice;
doagain:
{
cout>>"rerun?";
cin>> choice;
if (choice=='Y')
goto doagain;
}
}



... like that:)
  • 1

C++

107,338 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator