如何在C++中的同一函数中使用字符串和双精度
How to use strings and doubles in the same function in C++
对于一个类项目,我需要在一个函数中获取客户的全部信息(包括他们的姓名、房间号等(,所以我需要在同一函数中使用doubles和string。有什么办法可以这样做吗?如果没有,还有其他办法吗?
该项目希望我们通过引用传递值。
//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;
}
为什么不创建一个包含所有信息的结构?
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;
};
然后从类型struct返回一个变量。
是的。有一种使用structs的替代方法,您可以按照创建自己的structs
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;
}
相关文章:
- 如何在C++中的同一函数中使用字符串和双精度
- C 字符串返回字符串的整数/双精度/长整型值
- 如何从字符串转换为双精度*
- C++如何将字符串逐行转换为双精度
- 如何在 C++ 中将双精度数组中的值"force convert"到字符串输出中?
- 在C++中将字符串转换为双精度,而无需科学记数法
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 如何在C++中将字符串转换为双精度?
- 将双精度值转换为 char 变量时字符串流如何工作
- 在 c++ 中从双精度转换为字符串
- 从字符串转换为双精度和整数 - C++
- 如何从char16_t字符串文字中读取双精度?
- 分析字符串中的双精度和单词
- 使用 sprintf 和 %g 将双精度转换为字符串的意外结果
- C++将包含非人类可读数据的字符串转换为 200 双精度
- 将大双精度转换为科学记数法以用作字符串?
- 字符串和双精度的麻烦,等值后再得到一个数字
- 如何从字符串流中提取双精度
- 如何调用返回双精度或字符串并将其保存在地图上但作为对象的类的方法
- C# 是否有办法将双精度数组强制转换为类似于C++转换为 char* 的字符串?