How to concatenate to char* in C++? i tried using strcat but that only works for char* and a string. strcpy doesn't give the required output as i believe it overwrites the value in first array from the second. i need to do something like this in C++ (this what i do in java)
seat=seat+tempGuest.toString()+". " //Java
strcat_s(p, 150,seating[i][j].toString())
;
this gives an error as seating[i][j].tostring()
returns a char* type.
this is my function code
char* Auditorium::toString(){
char* p = new char[150];
cout << "The current seating is\n";
cout << "-------------------------\n";
for (int i = 0; i < rowNum1; i++)
{
for (int j = 0; j < columnNum1; j++)
{
strcat_s(p, 150,seating[i][j].toString());
strcat_s(p, 150," ");
}
strcat_s(p, 150,"\n");
}
return p;
}
seating[i][j].toString()
is getting value from the following function
char* Guest::toString()
{
char * p = new char[30];
p[0] = firstName[0];
p[1] = '.';
p[2]=lastName[0];
p[3] = '.';
p[4] = '\0';
return p;
}
In C++, you use std::string
. In this case you could even use a string stream:
#include <sstream>
std::string Auditorium::toString() {
std::ostringstream oss;
oss << "The current seating is\n";
oss << "-------------------------\n";
for (int i = 0; i < rowNum1; i++) {
for (int j = 0; j < columnNum1; j++) {
oss << seating[i][j].toString();
}
oss << "\n";
}
return oss.str();
}
Without the stream, consider:
std::string Auditorium::toString() {
std::string result;
for (int i = 0; i < rowNum1; i++) {
for (int j = 0; j < columnNum1; j++) {
result += seating[i][j].toString();
result += ' ';
}
result += '\n';
}
return result;
}
Even more in C++ style, don't provide a toString
, but provide operator<<
:
#include <ostream>
#include <string>
#include <array>
struct Guest {
std::string firstName, lastName;
friend std::ostream& operator<<(std::ostream& os, Guest const& g) {
return os << g.firstName[0] << '.' << g.lastName[0] << '.';
}
};
namespace {
using Row = std::array<Guest, 12>;
inline std::ostream& operator<<(std::ostream& os, Row const& row) {
for (Guest const& guest : row)
os << guest << " ";
return os;
}
}
struct Auditorium {
std::array<Row, 10> seating;
friend std::ostream& operator<<(std::ostream& os, Auditorium const& auditorium) {
for (Row const& row : auditorium.seating)
os << row << "\n";
return os;
}
};
#include <iostream>
int main() {
Auditorium auditorium {
Row {Guest {"Jacob","Lettsom"}, {"Brion","Peasee"}, {"Kelvin","Tomasoni"}, {"Marabel","Fere"}, {"Hertha","Bartlam"}, {"Devondra","Grahl"}, {"Ike","Annott"}, {"Patrick","Cheyenne"}, {"Eada","Dawe"}, {"Marget","Glashby"}, {"Henderson","Glaum"}, {"Cassie","Winsom"}},
Row {Guest {"Pierson","Kitchen"}, {"Elisha","Pass"}, {"Shirline","Wigin"}, {"Lucas","Stanmore"}, {"Norri","Hitzschke"}, {"Mercedes","Blackboro"}, {"Nappie","Breche"}, {"Freda","Mitton"}, {"Neils","Adamou"}, {"Mack","Rannells"}, {"Gaspard","Christoffersen"}, {"Sherwin","Kenwell"}},
Row {Guest {"Wallas","Chellam"}, {"Doralin","Corthes"}, {"Sebastien","Scoble"}, {"Domini","Sprott"}, {"Lazaro","Bunton"}, {"Hinda","Korn"}, {"Renata","Vogt"}, {"Delmar","MacDwyer"}, {"Violette","Villiers"}, {"Nicko","McDirmid"}, {"Susanetta","MacCaughey"}, {"Hillary","McNuff"}},
Row {Guest {"Alex","Downie"}, {"Caro","Westrey"}, {"Burr","Kalkofer"}, {"Ruy","Shelmerdine"}, {"Winfield","Beri"}, {"Isacco","Ellwell"}, {"Gideon","Beaford"}, {"Simon","Blaylock"}, {"Willy","Kloser"}, {"Guillemette","Boult"}, {"Mariya","Oehme"}, {"Emory","Angless"}},
Row {Guest {"Jammie","Klimek"}, {"Monro","Passman"}, {"Page","Kornes"}, {"Giorgio","Couttes"}, {"Alexine","Glayzer"}, {"Jeni","Ferschke"}, {"Rock","Farrants"}, {"Katrinka","Schnieder"}, {"Irina","Ault"}, {"Antonetta","Griss"}, {"Pammy","Bertenshaw"}, {"Erinna","Terbeck"}},
Row {Guest {"Evania","Sympson"}, {"Randy","Colvin"}, {"Legra","Osinin"}, {"Michaeline","Moroney"}, {"Adiana","Westbrook"}, {"Kellsie","Smeeton"}, {"Rodrick","Russ"}, {"Winny","Eggleston"}, {"Odille","Jerosch"}, {"Stacee","Liepina"}, {"Lucila","Jedras"}, {"Selma","Perch"}},
Row {Guest {"Lynda","Palmar"}, {"Gracie","Jennick"}, {"Cleveland","Mordue"}, {"Elias","Manssuer"}, {"Kimbra","Cicchitello"}, {"Genni","Woliter"}, {"Gale","Baudassi"}, {"Johann","Swindlehurst"}, {"Andrei","De" "Laspee"}, {"Edythe","Dorr"}, {"Janela","Fydo"}, {"Silvia","Suerz"}},
Row {Guest {"Rachel","Shoutt"}, {"Germain","Strangward"}, {"Margot","Son"}, {"Electra","Wookey"}, {"Samuel","Tight"}, {"Brande","Cable"}, {"Ford","Aitken"}, {"Noll","Woolf"}, {"Jourdan","Morewood"}, {"Milo","Trimming"}, {"Adair","Peck"}, {"Darnall","Loftus"}},
Row {Guest {"Dene","Creer"}, {"Leena","Autry"}, {"Dickie","Wiggington"}, {"Josephine","Eagan"}, {"Hetty","Yoodall"}, {"Gayla","Dibson"}, {"Elwood","Torri"}, {"Eunice","Scapelhorn"}, {"Everett","Bedingfield"}, {"Ralf","Lodeke"}, {"Steve","Dockreay"}, {"Rod","Bruford"}},
Row {Guest {"Siouxie","Rayment"}, {"Sheila-kathryn","Neal"}, {"Land","Beggi"}, {"Carson","Ferriman"}, {"Aile","Nias"}, {"Paige","Juckes"}, {"Cazzie","Leadstone"}, {"Selinda","Parlet"}, {"Rayner","Kesby"}, {"Murvyn","Laming"}, {"Lorianna","Pardey"}, {"Joane","Sneesby"}},
};
std::cout << auditorium << "\n";
}
Prints
J.L. B.P. K.T. M.F. H.B. D.G. I.A. P.C. E.D. M.G. H.G. C.W.
P.K. E.P. S.W. L.S. N.H. M.B. N.B. F.M. N.A. M.R. G.C. S.K.
W.C. D.C. S.S. D.S. L.B. H.K. R.V. D.M. V.V. N.M. S.M. H.M.
A.D. C.W. B.K. R.S. W.B. I.E. G.B. S.B. W.K. G.B. M.O. E.A.
J.K. M.P. P.K. G.C. A.G. J.F. R.F. K.S. I.A. A.G. P.B. E.T.
E.S. R.C. L.O. M.M. A.W. K.S. R.R. W.E. O.J. S.L. L.J. S.P.
L.P. G.J. C.M. E.M. K.C. G.W. G.B. J.S. A.D. E.D. J.F. S.S.
R.S. G.S. M.S. E.W. S.T. B.C. F.A. N.W. J.M. M.T. A.P. D.L.
D.C. L.A. D.W. J.E. H.Y. G.D. E.T. E.S. E.B. R.L. S.D. R.B.
S.R. S.N. L.B. C.F. A.N. P.J. C.L. S.P. R.K. M.L. L.P. J.S.
Using std::string
is a better option, as per sehe's answer.
However, I would like to add, if you ever feel tempted to type new
in C++, you are doing it wrong. Wrong as in gluing bird-feathers on a rocket, or putting horseshoes on a car. C++ is built around the idea of deterministic destruction, so if you want to 'grok' C++, first order of business is switching over to value-semantics and RAII
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.