Hello there, I started learning c++ a couple of months ago not everyday and everyhour of my day but I do my best. Anyways uhm in an old language I used to program with using a program called PAWNO, there was a diff way of dealing with strings that I would like to know its equal in c++, here is it:
1 2
new str[128];
format(str, sizeof(str),"%s has set you to level %d. ",Name,level);
I tried something like
1 2
char x[50];
x = "HI";
I know it's a wrong format and I cannot assign an array like that but I was just trying out my luck but I am desperate to know if there is an equal to what I asked for.
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
int main()
{
/* C language version */
{
constchar * Name = "Fred";
int level = 7;
char str[128];
snprintf(str, sizeof(str), "%s has set you to level %d. ", Name, level);
puts(str);
}
// C++ language version
{
std::string Name = "Fred";
int level = 7;
std::ostringstream oss;
oss << Name << " has set you to level " << level << ". ";
std::string str = oss.str(); // temporary string, not really needed.
std::cout << str << '\n';
}
}