2016-07-11 14 views
0

を印刷することができます。ここに私がタイプしたコードがあります。は、どのように私は、単純なホテルreservsationプロジェクトに取り組んでいますが、私は自分の名前を入力した後、顧客の名前と支払いの種類を印刷することができませんでしたCの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; 

いくつかのランダムな数字と文字は、顧客名や決済タイプに表示されます。どうすればそれらを正しく書くことができますか?

+2

言う 'customername1'の種類は何です...コメントをたどるとstd :: stringとベクターに見えますか?なぜそれを2回読むのですか? ( '' ''と '' getline') –

+6

'新しいchar [strlen(customername1)+1];' wooooah there!代わりに 'std :: string'のいいゲームをどうですか? –

+1

'cin >> customername1;'はすでに入力を読み込みます、なぜあなたは 'cin.getline(customername1,100、 '\ n');'それ以降ですか? –

答えて

1

顧客名と支払いタイプについては、1つの要素ではなく配列を印刷しようとします。配列は基本的に最初の要素へのポインタなので、メモリアドレスである '乱数'が得られます。

試してみてください。

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

そして、あなたはその作業を持っているとき、

+0

はい、あなたも正しいと思いますが、cin.getline(customername1,100、 '\ n')は不要です – erengsgs

+0

@NathanOliver:いいえ、変数の名前をよく見てください... OP、あなたのコメントは正しいです。 – DrDonut

+0

よろしくお願いします。厄介な小さなことが強調しています。 – NathanOliver

関連する問題