Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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++是比较新的(只是用这个语言编码了大约一个星期),我认为在我的一个空洞函数中,一个变量没有被正确地传递(这就是我认为问题可能是不同的)。 #include <iostream> using namespace std; void mainProcess (string, string, int, double); int main(){ //input string name; string carType; string carModel; int daysOfHire; double totalPrice; cout << "what is the name\n"; cin >> name; cout <<"what is the car type\n"; cin >> carType; cout <<"what is the car model\n"; cin >> carModel; cout <<"how many days of hire\n"; cin >> daysOfHire ; //process mainProcess(carType, carModel, daysOfHire, totalPrice); //output cout <<"name: " <<name<< endl; cout <<"car type: " <<carType<< endl; cout <<"car model: " <<carModel<< endl; cout << "days of hire: " <<daysOfHire<< endl; cout <<"total bill: $" <<totalPrice<< endl; //this line is supposed to print the totalPrice variable which was declared below } void mainProcess(string carType, string carModel, int daysOfHire, double totalPrice){ if (carType == "family" && carModel == "saloon"){ totalPrice = daysOfHire*33.00; } else if (carType == "family" && carModel == "hatchback"){ totalPrice = daysOfHire*33.99; } else if (carType == "sports" && carModel == "reliant"){ totalPrice = daysOfHire*40.00; } else if (carType == "sports" && carModel == "supa fast"){ totalPrice = daysOfHire*80.00; } else if (carType == "city" && carModel == "executive"){ totalPrice = daysOfHire*100.00; } } #包括 使用名称空间std; void mainProcess(string、string、int、double); int main(){ //输入 字符串名; 字符串类型; 弦卡模型; int daysOfHire; 双倍总价; 姓名; cout-carType; cout-carModel; 库特戴索夫; //过程 主流程(carType、carModel、daysOfHire、totalPrice); //输出 cout_C++_Void - Fatal编程技术网

我在c++;没有正确地传递变量 我对C++是比较新的(只是用这个语言编码了大约一个星期),我认为在我的一个空洞函数中,一个变量没有被正确地传递(这就是我认为问题可能是不同的)。 #include <iostream> using namespace std; void mainProcess (string, string, int, double); int main(){ //input string name; string carType; string carModel; int daysOfHire; double totalPrice; cout << "what is the name\n"; cin >> name; cout <<"what is the car type\n"; cin >> carType; cout <<"what is the car model\n"; cin >> carModel; cout <<"how many days of hire\n"; cin >> daysOfHire ; //process mainProcess(carType, carModel, daysOfHire, totalPrice); //output cout <<"name: " <<name<< endl; cout <<"car type: " <<carType<< endl; cout <<"car model: " <<carModel<< endl; cout << "days of hire: " <<daysOfHire<< endl; cout <<"total bill: $" <<totalPrice<< endl; //this line is supposed to print the totalPrice variable which was declared below } void mainProcess(string carType, string carModel, int daysOfHire, double totalPrice){ if (carType == "family" && carModel == "saloon"){ totalPrice = daysOfHire*33.00; } else if (carType == "family" && carModel == "hatchback"){ totalPrice = daysOfHire*33.99; } else if (carType == "sports" && carModel == "reliant"){ totalPrice = daysOfHire*40.00; } else if (carType == "sports" && carModel == "supa fast"){ totalPrice = daysOfHire*80.00; } else if (carType == "city" && carModel == "executive"){ totalPrice = daysOfHire*100.00; } } #包括 使用名称空间std; void mainProcess(string、string、int、double); int main(){ //输入 字符串名; 字符串类型; 弦卡模型; int daysOfHire; 双倍总价; 姓名; cout-carType; cout-carModel; 库特戴索夫; //过程 主流程(carType、carModel、daysOfHire、totalPrice); //输出 cout

我在c++;没有正确地传递变量 我对C++是比较新的(只是用这个语言编码了大约一个星期),我认为在我的一个空洞函数中,一个变量没有被正确地传递(这就是我认为问题可能是不同的)。 #include <iostream> using namespace std; void mainProcess (string, string, int, double); int main(){ //input string name; string carType; string carModel; int daysOfHire; double totalPrice; cout << "what is the name\n"; cin >> name; cout <<"what is the car type\n"; cin >> carType; cout <<"what is the car model\n"; cin >> carModel; cout <<"how many days of hire\n"; cin >> daysOfHire ; //process mainProcess(carType, carModel, daysOfHire, totalPrice); //output cout <<"name: " <<name<< endl; cout <<"car type: " <<carType<< endl; cout <<"car model: " <<carModel<< endl; cout << "days of hire: " <<daysOfHire<< endl; cout <<"total bill: $" <<totalPrice<< endl; //this line is supposed to print the totalPrice variable which was declared below } void mainProcess(string carType, string carModel, int daysOfHire, double totalPrice){ if (carType == "family" && carModel == "saloon"){ totalPrice = daysOfHire*33.00; } else if (carType == "family" && carModel == "hatchback"){ totalPrice = daysOfHire*33.99; } else if (carType == "sports" && carModel == "reliant"){ totalPrice = daysOfHire*40.00; } else if (carType == "sports" && carModel == "supa fast"){ totalPrice = daysOfHire*80.00; } else if (carType == "city" && carModel == "executive"){ totalPrice = daysOfHire*100.00; } } #包括 使用名称空间std; void mainProcess(string、string、int、double); int main(){ //输入 字符串名; 字符串类型; 弦卡模型; int daysOfHire; 双倍总价; 姓名; cout-carType; cout-carModel; 库特戴索夫; //过程 主流程(carType、carModel、daysOfHire、totalPrice); //输出 cout,c++,void,C++,Void,您正在通过值将totalPrice传递到mainProcess,这意味着创建了一个副本,并对其本地副本进行了更改,而不是原始副本。这就是main中的totalPrice保持不变(且未初始化!)的原因 要使totalPrice从内部变得可修改,您需要通过引用传递它,如下所示: void mainProcess(string carType, string carModel, int daysOfHire, double& totalPrice) { // now assignmen

您正在通过值将
totalPrice
传递到
mainProcess
,这意味着创建了一个副本,并对其本地副本进行了更改,而不是原始副本。这就是
main
中的
totalPrice
保持不变(且未初始化!)的原因

要使
totalPrice
从内部变得可修改,您需要通过引用传递它,如下所示:

void mainProcess(string carType, string carModel, int daysOfHire, double& totalPrice) {
    // now assignment to totalPrice will change the original
    ...
}
此类参数称为“输出”或“输入输出”参数。但是,在这种情况下,不需要使用输出参数,因为最好只从函数返回所需的值。此外,为函数命名具有语义意义也很好。看看程序变得多么容易阅读:

double calculateTotalPrice(string carType, string carModel, int daysOfHire){
    if (carType == "family" && carModel == "saloon"){
        return daysOfHire*33.00;
    }    
    else if (carType == "family" && carModel == "hatchback"){
        return daysOfHire*33.99;
    } 
    else if (carType == "sports" && carModel == "reliant"){
        return daysOfHire*40.00;
    }
    // ... etc    
}

...
const auto totalPrice = calculateTotalPrice(carType, carModel, daysOfHire);

mainProcess
中,
totalPrice
是该函数的局部变量。您需要返回结果(或者,您可以通过引用传递
totalPrice