Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在函数中循环?_C++ - Fatal编程技术网

C++ 如何在函数中循环?

C++ 如何在函数中循环?,c++,C++,我想用函数循环我的代码,但我不知道如何循环。我已经尝试使用WHEVER()循环,将它放在代码> int main(){< /Cord> >中,但导致了一个错误,因为我只知道如何循环但没有函数。我的意思是,在代码之前,在哪里放,for,do循环,是在int()之前还是之后?我在C++中还是新的。 代码: #包括 #包括 使用名称空间std; 字符串getName(); 字符串getproductCategory(); char getproductCode(); int getproductQua

我想用函数循环我的代码,但我不知道如何循环。我已经尝试使用WHEVER()循环,将它放在代码> int main(){< /Cord> >中,但导致了一个错误,因为我只知道如何循环但没有函数。我的意思是,在代码之前,在哪里放,for,do循环,是在int()之前还是之后?我在C++中还是新的。 代码:

#包括
#包括
使用名称空间std;
字符串getName();
字符串getproductCategory();
char getproductCode();
int getproductQuantity();
int客户距离(int,int);
int calculatetotalPrice(字符串、字符、int);
int距离INKM(int);
无效显示全部(整数总价);
int main(){
字符串名称123,类别;
整数数量、价格、总价、公里、价格;
字符编码;
name123=getName();
category=getproductCategory();
productcode=getproductCode();
数量=getproductQuantity();
priceperkm=客户距离(公里,价格);
总价=计算总价(类别、产品代码、数量);
显示全部(总价);
返回0;}
字符串getName(){
字符串名称123;
cout>name123;
返回名称123;
}
字符串getproductCategory(){
字符串类别;
cout类别;
返回类别;}
char getproductCode(){
字符编码;
cout产品代码;
返回产品代码;
}
int getproductQuantity(){
整数;
产量;
退货数量;
}
国际客户距离(国际公里,国际价格){
int priceperkm;
库特公里;
如果(公里>=20)
priceperkm=1.00*km;
否则,如果(公里数>=20和100公里)
priceperkm=2.00*km;
退货价格;
}
整数calculatetotalPrice(字符串类别、字符产品代码、整数数量){
int价格、totalprice、priceperkm;
字符串描述1,描述2;
如果(类别==“NP456”和产品代码==“P”){
价格=158.50*数量;
description1=“承诺的爱”;
description2=“新产品”;}
else if(类别==“NP456”&&productcode==“E”){
价格=198.50*数量;
description1=“无尽的爱”;
description2=“新产品”;}
else if(类别==“BS123”&&productcode==“F”){
价格=58.50*数量;
description 1=“费列罗巧克力箔气球”;
description2=“畅销书”;}
else if(类别==“BS123”&&productcode==“B”){
价格=188.50*数量;
description1=“生日蛋糕狂热”;
description2=“畅销书”;}
totalprice=价格*priceperkm;
返回总价;
}
无效显示全部(整数总价){
字符串名称123,description1;
整数;

cout检查
while
循环。可以在布尔值计算为true时运行它,每次while运行std::cin(读取控制台)检查是否等同于一个短语,如停止,如果它设置了一个格式的布尔停止,停止循环。< /P>你需要阅读更多关于C++的内容。请参阅并阅读一个。从现有的开源C++程序中得到灵感。等等。编译你的C++代码,调用它为<代码> g++Walth-WOTU-G<代码>,然后用于理解B。ehavior of your program除了函数名和局部变量外,所有的
get
函数都是相同的。这真的有必要吗?你不能直接使用
cin
吗?好的,我会试试,谢谢。
 #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;
}