What Variable should I use to store text
Printable View
What Variable should I use to store text
Look at C-style string (char*'s) or, the preferable std::string class in <string> (C++ Standard Library).
Since this is the C++ area, I would suggest a string. You can learn all about it here.
- Stack Overflow
Don't worry, the link Stack Overflow provided is a good reference, but the string class is much easier to use than reading that page would suggest. I don't know of any specific tutorials on string that would be easier to follow, but I'm sure there are some.
thanks everyone
Oh,
I must admit it is a good reference compared to learning about it. Here are some tutorials that you may find helpful:
- C++ String Class Tutorial
- C++ Input and Output
- Manipulating String Variables
- C++ Standard Template Library
- Stack Overflow
Nice, thank you. Upon quick skimming I like the fourth one best.
Yeah but the third one didn't exactly give me the warm fuzzies:
That using namespace std; line doesn't have anything to do with the string.h header does it? Maybe string but not string.h. Also, I wouldn't say that simply including a header lets you "recognize" the string library.Quote:
A string is a sequence of characters. In order to use strings in your programs, you need to first make sure that you are using a recognized string library. This can be achieved by adding the following to the top of your code (where you place your header files):
#include<string.h>
using namespace std;
The second line tells the compiler to use the standard "namespace" for the string library.
Quote:
The rest is fairly simple. Here is how a sample program using strings would look like:
#include <iostream>
#include <string>
using namespace std;
void main() // Yuck
{
string firstname="test";
cout << "Enter first name: ";
cin >> firstname;
cout << firstname << endl;
}
Some tutorials don't give accurate information. It is hard to find tutorials on <string>, so I just posted all the information I had. Sadly, some people still use incorrect syntax and other idio's like void main() when they shouldn't. A post I made a while back shows why it isn't good to use these types of coding practices.
- Stack Overflow