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.
11 Antworten
+ 6
#include <iostream>
#include <string>
using namespace std;
int main()
{
//code here
}
+ 6
yes indeed you can. just on a new line for each. just to make it more readable
+ 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])
+ 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
+ 4
this works also. to use James example you would need to use #include <string>
+ 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;
+ 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.
+ 1
And what the #include <string> means? It will change something? But thanks.
+ 1
OK thanks... but is using strings harder than char? I'm still new here.
+ 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?
+ 1
OK Thanks !