[英]How would I prompt the user to give them the option to rerun program in C++
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?该选项可能如下所示:“要再次运行程序,请输入 '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;
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;
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.