Reputation: 1968
Initializing a string in C#
is as easy as this:
string str = Console.Read();
with this method, I don't need to know the size of the string which the user enters. But I cannot find a way like this in C++. I want my string to be defined as char *input
, and I don't want to know the size of the string.
How can I achieve what I want?
Upvotes: 0
Views: 396
Reputation: 21975
Why not use C++'s string type?
#include <iostream>
#include <string>
int main() {
std::string foo;
std::cin >> foo;
std::cout << foo << "\n";
}
Upvotes: 5
Reputation: 247979
C++ has a string class which works much like C#'s string. So use it. :)
char*
is not a string. It's just the closest you get if you're working in C.
So, #include <string>
, and then use std::string
instead of char*
.
Upvotes: 1