roostaamir
roostaamir

Reputation: 1968

How can I initialize a *char using user input?

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

Answers (3)

Paul
Paul

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

masoud
masoud

Reputation: 56479

Use std::string and std::cin:

std::string str;
std::cin >> str;

Upvotes: 1

Stack Overflow is garbage
Stack Overflow is garbage

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

Related Questions