如何在c ++中打印char数组

时间:2016-07-11 11:29:50

标签: 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;

为客户名称和付款类型显示一些随机数字和字母。我该怎么写得好呢?

1 个答案:

答案 0 :(得分:1)

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

尝试:

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

当你完成这项工作后,请按照评论查看std :: string和vector ...

相关问题