c++ - 我怎样才能在 C++ 中打印一个 char 数组

标签 c++ arrays printing char

我正在做一个简单的酒店预订项目,但在输入他们的姓名后我无法打印客户姓名和付款方式。这是我输入的一段代码。

 cout << "Please enter the customer name: " << endl;
 cin >> customername1;
 cin.getline(customername1,100,'\n');
 fflush(stdin);
 cout << "Please enter the payment type: " << endl;
 cin >> paymenttype;
 cin.getline(paymenttype,100,'\n');
 fflush(stdin);
 cout << "How many days would you like to stay: " << endl;
 cin >> days;
 room_income = days * 30;
 count_room++ ;
 count_customer1++ ;
 customer_name1[single]= new char [strlen(customername1)+1]; 
 strcpy(customer_name1[single],customername1); 
 payment_type[single]= new char [strlen(paymenttype)+1]; 
 strcpy(payment_type[single],paymenttype);
 cout << "Room number : " << single << endl<< endl;
 cout << "Customer name : "<< customer_name1 << endl << endl;
 cout << "Payment type : "<< payment_type << endl<< endl;
 cout << "Number of day for accomodation :"<< days << endl<< endl;
 cout << "Income for this room : "<< room_income<< endl<< endl;

客户名称和付款类型会显示一些随机数字和字母。我怎样才能正确地编写它们?

最佳答案

对于客户名称和付款类型,您尝试打印数组,而不是一个元素。由于数组基本上只是指向第一个元素的指针,因此您将获得一个“随机数”,即内存地址。

尝试:

cout << "Customer name : "<< customername1 << endl << endl;
cout << "Payment type : "<< paymenttype << endl<< endl;

当你开始工作时,按照评论查看 std::string 和 vectors...

关于c++ - 我怎样才能在 C++ 中打印一个 char 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38305817/

相关文章:

c++ - 不允许在类定义之外覆盖

c++ - 将无符号转换为有符号并返回

java - android phonegap 自定义插件需要的建议

vba - 如何在 VBA 中更改打印机对话框的默认名称? (使用打印机对话框的句柄时)

c++ - 通过引用返回对象

javascript - 搜索数组中的值

c# - 通过 LINQ 查找目标数字是否是数组中两个数字的总和

arrays - Swift:如果数组的自定义对象元素具有相同的键,则合并它们

java - 从作为 Windows 服务运行的 Java 应用程序打印 Word 文档 (.docx)

c++ - 从 trait 获取 const 或非常量引用类型