该选项可能如下所示:"要再次运行该程序,请输入',以退出输入' 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 <<
答案 0 :(得分: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;
答案 1 :(得分:0)
试试这个:
#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;
}