如何在 C++ 中的同一函数中使用字符串和双精度数
Posted
技术标签:
【中文标题】如何在 C++ 中的同一函数中使用字符串和双精度数【英文标题】:How to use strings and doubles in the same function in C++ 【发布时间】:2018-11-02 01:18:10 【问题描述】:对于一个班级项目,我需要在一个函数中获取客户的所有信息(包括他们的姓名、房间号等),因此我需要在同一个函数中使用双精度数和字符串。如果没有,是否有这样做或替代方法?
项目希望我们通过引用传递值。
//Prints a statement for each overnight customer.
#include <iostream>
#include <string>
#include <math.h>
#include <iomanip>
using namespace std;
const double sales_tax_rate = 0.055;
//Function prototypes
string customerInformation(string &customer_name, string &date_of_bill,
string &hotelormotel_name, double &room_rate, double &number_of_nights,
double &phone_charges, double &room_number);
int main()
string customer_name, dummy, date_of_bill, hotelormotel_name;
double room_rate = 0;
double number_of_nights = 0;
double phone_charges = 0;
double room_number = 0;
double room_cost;
double subtotal;
double total;
double taxes;
int counter = 1;
char repeat;
do
counter += 1;
taxes = 0;
total = 0;
subtotal = 0;
room_cost = 0;
customerInformation(customer_name, room_rate, number_of_nights,
phone_charges, room_number, date_of_bill, hotelormotel_name);
room_cost = room_rate * number_of_nights;
taxes = sales_tax_rate * room_cost;
subtotal = taxes + room_cost;
total = subtotal + phone_charges;
cout << hotelormotel_name << endl << endl;
cout << "Date: " << date_of_bill << endl;
cout << "Customer's Name: " << customer_name << endl;
cout << "Room Number: " << room_number << endl;
cout << "Number of Nights: " << number_of_nights << endl;
cout << setprecision(2) << fixed << endl;
cout << "Room Rate: $" << room_rate << endl;
cout << "Room Cost: $" << room_cost << endl;
cout << "Taxes: $" << taxes << endl;
cout << "Subtotal: $" << subtotal << endl << endl;
cout << "Phone Charges: $" << phone_charges << endl << endl;
cout << "TOTAL DUE: $" << total << endl << endl;
cout << "Thank you for staying at " << hotelormotel_name << "!" <<
endl;
cout << "Drive safely and please come again!" << endl << endl;
cout << "Would you like to run this program again? (Y or N): ";
cin >> repeat;
getline(cin, dummy);
cout << endl;
while (repeat == 'Y' || repeat == 'y');
string customerInformation(string &customer_name, string &date_of_bill,
string &hotelormotel_name, double &room_rate, double &number_of_nights,
double &phone_charges, double &room_number)
cout << "Please enter the following information: " << endl;
cout << "Hotel/Motel Name: ";
getline(cin, hotelormotel_name);
cout << "Customer Name: ";
getline(cin, customer_name);
cout << "Date: ";
getline(cin, date_of_bill);
cout << "Room Number: ";
cin >> room_number;
do
cout << "Number of Nights: ";
cin >> number_of_nights;
if (number_of_nights <= 0)
cout << "Error: Invalid data entered, please try again.\n";
while (number_of_nights <= 0);
cout << endl;
do
cout << "Room Rate: $";
cin >> room_rate;
if (room_rate <= 0)
cout << "Error: Invalid data entered, please try again.\n";
while (room_rate <= 0);
do
cout << "Phone Charges: $";
cin >> phone_charges;
if (phone_charges < 0)
cout << "Error: Invalid data entered, please try again.\n";
while (phone_charges < 0);
cout << endl;
return date_of_bill, customer_name, room_number, room_rate,
number_of_nights, phone_charges, hotelormotel_name;
【问题讨论】:
你的程序怎么没有做你想做的事?您的函数是否已经同时接受字符串和双精度数? 每次我运行它我都会得到这个错误:错误 C2664 'std::string customerInformation(std::string &,std::string &,std::string &,double &,double & ,double &,double &)': 无法将参数 2 从 'double' 转换为 'std::string &' Project1 c:\users\worth\downloads\lab8-1-wortham.cpp 39 等你想返回所有这些东西吗?你不需要。您通过 通过引用 传递它们,这意味着如果您在函数中更改它们的值,这些更改将传回给调用者。不要将它们全部添加到 return 语句中。 请阅读the help pages,尤其是"What topics can I ask about here?"和"What types of questions should I avoid asking?"。还有take the tour 和read about how to ask good questions 和this question checklist。并了解如何创建Minimal, Complete, and Verifiable Example。 我也收到很多错误,例如:错误(活动)E0434 类型“double &”(非 const 限定)的引用无法使用“std::string”类型的值初始化Project1 C:\Users\worth\Downloads\Lab8-1-Wortham.cpp 39 【参考方案1】:为什么不创建一个包含所有这些信息的结构?
struct customerinfo
string customer_name;
string date_of_bill;
string hotelormotel_name;
double room_rate;
double number_of_nights;
double phone_charges;
double room_number;
;
然后你从类型结构返回一个变量。
【讨论】:
【参考方案2】:是的,还有一种使用结构体的替代方法,您可以按照以下方式创建自己的结构体
struct customerInfo
string customer_name, dummy, date_of_bill, hotelormotel_name;
double room_rate;
double number_of_nights;
double phone_charges;
double room_number;
double room_cost;
double subtotal;
double total;
double taxes;
;
然后您可以将结构类型的对象传递给函数,如下所示
//Function prototypes
string customerInformation(customerInfo tempCustomer);
你可以使用这个 tempCustomer 如下
string customerInformation(customerInfo tempCustomer)
cin>>tempCustomer.customer_name;
//you can call all attributes and assign them values accordingly
//to return a particular value you can do
return tempCustomer.customer_name;
【讨论】:
从这个方法你可以返回一个完整的对象(通过将函数的返回类型从字符串更改为customerInfo)。以上是关于如何在 C++ 中的同一函数中使用字符串和双精度数的主要内容,如果未能解决你的问题,请参考以下文章
单类型和双类型变量如何在 Matlab 中的同一代码副本中工作,就像 C++ 中的模板一样
在 X87 和 SSE FPU 中用户定义的点之后截断浮点数和双精度数