提问者:小点点

如何在函数中循环?[关闭]


我想用函数循环我的代码,但我不知道怎么做。我想循环直到用户说停止。我的输出也有问题,当调用函数。我对C++还是个新手。感谢任何帮助!

我计划我的输出是这样的:欢迎来到和谐礼品店-(用户名)

您已购买:(说明1)类别:(说明2)您应支付:(总价)

代码:

  #include <iostream>
#include <string>

using namespace std;

string getName();

string getproductCategory ();

char getproductCode ();

int getproductQuantity ();

int customerDistance (int,int);

int calculatetotalPrice (string,char,int);

int distanceInKM (int);

void displayALL (int totalprice);

int main () {
    
    string name123,category;
    int quantity,priceperkm,totalprice,km,price;
    char productcode;
    
    name123 = getName();
    
    category = getproductCategory ();
    
    productcode = getproductCode ();
    
    quantity = getproductQuantity();
    
    priceperkm = customerDistance(km,price);
    
    totalprice = calculatetotalPrice(category,productcode,quantity);

    displayALL (totalprice);
    
    return 0;}
    
string getName (){
    string name123;
    cout << "Enter name : ";
    cin >> name123;
    return name123;
}
    
string getproductCategory (){
    string category;
    cout <<"Enter category : ";
    cin >> category;
    return category;}

char getproductCode(){
    char productcode;
    cout <<"Enter product code : ";
    cin >> productcode;
    return productcode;
}

int getproductQuantity(){
    int quantity;
    cout <<"Enter quantity : ";
    cin >> quantity;
    return quantity;
}

int customerDistance(int km, int price){
    int priceperkm;
    cout <<"Enter distance (km) : ";
    cin>>km;
    
    if (km >= 20)
    priceperkm = 1.00*km;
    else if (km >= 20 && km <= 80)
    priceperkm = 1.50*km;
    else if (km > 100)
    priceperkm = 2.00*km;
    
    return priceperkm;
}

int calculatetotalPrice (string category,char productcode,int quantity){
    int price,totalprice,priceperkm;
    string description1,description2;
    
    if (category == "NP456" && productcode == 'P'){
    price = 158.50*quantity;
        description1 = "Promising Love";
            description2 = "New Product";}
    else if (category == "NP456" && productcode == 'E'){
    price = 198.50*quantity;
        description1 = "Endless Love" ;
            description2 = "New Product";}
    else if (category == "BS123" && productcode == 'F'){
    price = 58.50*quantity;
        description1 = "Ferrero Choclate Foil Balloon";
            description2 = "Best Seller";}
    else if (category == "BS123" && productcode == 'B'){
    price = 188.50*quantity;
        description1 = "Birthday Cake Mania";
            description2 = "Best Seller";}
    
totalprice = price * priceperkm;

return totalprice;
}

void displayALL(int totalprice){
    string name123,description1;
    int quantity;
    cout <<"WELCOME TO HARMONY GIFT SHOP - "<<name123<<endl;
    cout <<"You have purchased : "<<description1<<endl;
    cout <<"Quantity :"<<quantity<<endl;
    cout <<"You should pay : RM "<<totalprice<<endl;
}

共1个答案

匿名用户

签出while循环。您可以让它在布尔值为true时运行,并且每次while运行std::cin(读取控制台),检查它是否等同于一个短语,如stop以及它是否将aformentioned布尔值设置为stop(停止),从而停止循环。