0

Storing Text to Variables

I'm trying to make text adventure game. I tried to make program ask your name and store it in variable "name" and then it will: cout << "Your name is << name; . But output is: Your name is 0 ... Why is text replaced with 0 ? Help please.

28th Apr 2017, 9:31 PM
Mikuláš Suchanovský
Mikuláš Suchanovský - avatar
11 odpowiedzi
+ 6
#include <iostream> #include <string> using namespace std; int main() { //code here }
6th May 2017, 6:30 AM
jay
jay - avatar
+ 6
yes indeed you can. just on a new line for each. just to make it more readable
6th May 2017, 6:29 AM
jay
jay - avatar
+ 5
string is an object in c++. http://www.cplusplus.com/reference/string/string/ In james example he used a string to store the data for the variable [name] it will enable [name] to hold more characters than using a char array (char[20])
6th May 2017, 6:15 AM
jay
jay - avatar
+ 5
no easier in my books anyway. If you see the link I provided the string object has many helper methods and functions to make your life much easier
6th May 2017, 6:25 AM
jay
jay - avatar
+ 4
this works also. to use James example you would need to use #include <string>
28th Apr 2017, 10:26 PM
jay
jay - avatar
+ 3
using namespace std; int main(int argc, char* argv[]) { string name; cout << "Type your name: "; cin >> name; cout << "Your name is " << name << endl; system("pause"); return 0;
28th Apr 2017, 9:36 PM
James
James - avatar
+ 1
Sorry, I'm new to C++ , I don't know your code, but I repaired it by myself. I changed: int name; to char name[20]; Now it's working properly. Sorry for my bad English and wasting your time.
28th Apr 2017, 9:41 PM
Mikuláš Suchanovský
Mikuláš Suchanovský - avatar
+ 1
And what the #include <string> means? It will change something? But thanks.
6th May 2017, 6:05 AM
Mikuláš Suchanovský
Mikuláš Suchanovský - avatar
+ 1
OK thanks... but is using strings harder than char? I'm still new here.
6th May 2017, 6:22 AM
Mikuláš Suchanovský
Mikuláš Suchanovský - avatar
+ 1
OK i checked the link... i don't know anything... but im gonna learn...I just wanna give last question... Can be #include <iostream> and #include <string> together?
6th May 2017, 6:28 AM
Mikuláš Suchanovský
Mikuláš Suchanovský - avatar
+ 1
OK Thanks !
6th May 2017, 6:30 AM
Mikuláš Suchanovský
Mikuláš Suchanovský - avatar