i want the equivalent of this C# piece of code in C++
String name;
name=Console.ReadLine();
i tried the following code, but its not working!
struct node{string player_name};
p=new struct node;
getline(cin,p->player_name);
i want the equivalent of this C# piece of code in C++
String name;
name=Console.ReadLine();
i tried the following code, but its not working!
struct node{string player_name};
p=new struct node;
getline(cin,p->player_name);
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
getline(cin,s);
cout << s;
}
Try it here at http://ideone.com/AgLUGv
The code you posted doesn't compile. It is missing a ;
after player_name
, for example. Here is a version that does compile:
#include <iostream>
#include <string>
struct node{
std::string player_name;
};
int main()
{
node * p= new node();
std::getline(std::cin, p->player_name);
delete p;
return 0;
}
Of course there is a simpler way of doing this, you do not need to use new/delete
you can create the object on the stack. The contents of player_name
are created in the heap:
#include <iostream>
#include <string>
struct node {
std::string player_name;
};
int main()
{
node p;
std::getline( std::cin, p.player_name);
return 0;
}
If you want the equivalent of your C#
code, then we can remove the node struct
:
#include <iostream>
#include <string>
int main()
{
std::string name;
std::getline( std::cin, name);
return 0;
}