C ++循环定价声明

时间:2019-07-17 15:47:21

标签: c++

我正在尝试制作一个循环语句,以根据各自的包装确定价格,但似乎无法掌握dowhile语句。

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    const double W_adult=47.90, W_child=41.50, W_senior=35.40, A_adult=25.90, A_child=20.50, A_senior=15.40, P_adult=15.90, P_child=12.50, P_senior=10.40;
    double totaladult,totalchild,totalsenior, total;
    int noadult,nochild,nosenior;
    char option;
    //char choice = 'y'; looping choice

    cout<<"WELCOME TO WAS LOST WORLD THEME PARK\n\n"<<endl;
    cout<<setw(80)<<setfill('*')<<"*";
    cout<<"\nPackage name \t | ADULT\t | CHILD\t | SENIOR CITIZEN"<<endl;
    cout<<"|A| Water Park\t | 47.90\t | 41.50\t | 35.40"<<endl;
    cout<<"|B| Am. Park\t | 25.90 \t | 20.50 \t | 15.40"<<endl;
    cout<<"|C| Pet. Zoo\t | 15.90\t | 12.50\t | 10.40"<<endl<<endl;
    cout<<setw(80)<<setfill('*')<<"*";

    cout<<"\nChoose your package A/B/C: "<<endl;
    cin>>option;

    cout<<"Enter the no of adult: "<<endl;
    cin>>noadult;
    cout<<"Enter the no of child: "<<endl;
    cin>>nochild;
    cout<<"Enter the no of senior citizen: "<<endl;
    cin>>nosenior;

    switch(option)
    {
        case 'A':
        case 'a':
            totaladult=W_adult*noadult;
            totalchild=W_child*nochild;
            totalsenior=W_senior*nosenior;

        break;

        case 'B':
        case 'b':
            totaladult=A_adult*noadult;
            totalchild=A_child*nochild;
            totalsenior=A_senior*nosenior;

        break;

        case 'C':
        case 'c':
            totaladult=P_adult*noadult;
            totalchild=P_child*nochild;
            totalsenior=P_senior*nosenior;

        break;
    }

    cout<<"Total no of adult is: "<<noadult<<endl;
    cout<<"Total no of child is: "<<nochild<<endl;
    cout<<"Total no of senior citizen is: "<<nosenior<<endl;
    cout<<"Total price of adult is: "<<totaladult<<endl;
    cout<<"Total price of child is: "<<totalchild<<endl;
    cout<<"Total price of senior citizen is: "<<totalsenior<<endl;
    cout<<"The total price is: "<<totaladult+totalchild+totalsenior<<endl;

    return 0;       
}

这里还有其他我想念的东西吗?目前,它仅计算一种类别的价格。我想知道应该在哪里包含循环语句。

1 个答案:

答案 0 :(得分:0)

您应该添加一个退出选项,并在用户不断选择软件包时进行while循环:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
const double W_adult=47.90, W_child=41.50, W_senior=35.40, A_adult=25.90, A_child=20.50, A_senior=15.40, P_adult=15.90, P_child=12.50, P_senior=10.40;
double totaladult = 0,totalchild = 0,totalsenior = 0, total = 0;
int noadult = 0,nochild = 0,nosenior = 0;
char option;
bool run = true;
//char choice = 'y'; looping choice

cout<<"WELCOME TO WAS LOST WORLD THEME PARK\n\n"<<endl;
cout<<setw(80)<<setfill('*')<<"*";
cout<<"\nPackage name \t | ADULT\t | CHILD\t | SENIOR CITIZEN"<<endl;
cout<<"|A| Water Park\t | 47.90\t | 41.50\t | 35.40"<<endl;
cout<<"|B| Am. Park\t | 25.90 \t | 20.50 \t | 15.40"<<endl;
cout<<"|C| Pet. Zoo\t | 15.90\t | 12.50\t | 10.40"<<endl<<endl;
cout<<setw(80)<<setfill('*')<<"*";

while(run)
{
cout<<"\nChoose your package A/B/C --- or Q to quit: "<<endl;
cin>>option;


cout<<"Enter the no of adult: "<<endl;
cin>>noadult;
cout<<"Enter the no of child: "<<endl;
cin>>nochild;
cout<<"Enter the no of senior citizen: "<<endl;
cin>>nosenior;

switch(option)
{
    case 'A':
    case 'a':
        totaladult+=W_adult*noadult;
        totalchild+=W_child*nochild;
        totalsenior+=W_senior*nosenior;

    break;

    case 'B':
    case 'b':
        totaladult+=A_adult*noadult;
        totalchild+=A_child*nochild;
        totalsenior+=A_senior*nosenior;

    break;

    case 'C':
    case 'c':
        totaladult+=P_adult*noadult;
        totalchild+=P_child*nochild;
        totalsenior+=P_senior*nosenior;
        break;

        case 'q':
        case 'Q':
            run = false;
            break;

        default:
            // do nothing
            break;

    break;
}

cout<<"Total no of adult is: "<<noadult<<endl;
cout<<"Total no of child is: "<<nochild<<endl;
cout<<"Total no of senior citizen is: "<<nosenior<<endl;
cout<<"Total price of adult is: "<<totaladult<<endl;
cout<<"Total price of child is: "<<totalchild<<endl;
cout<<"Total price of senior citizen is: "<<totalsenior<<endl;
cout<<"The total price is: "<<totaladult+totalchild+totalsenior<<endl;
}
return 0;   

}