How can you use strings in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can you use strings in C++?

No matter what I #include at the beginning of the code my strings aren’t considered declared within the rest of my code. Any ideas on how to get them to work would be appreciated.

8th Dec 2017, 11:35 PM
Bailey
Bailey  - avatar
10 Answers
+ 2
@Keerthi It tells the compiler to use the namespace we want, so that we do not have to scope all our keywords like this: std::cout, std::cin, etc. After C++98, all headers were standardised, and were merged into a standard namespace. Thats why you no longer see the .h with their names. Basically, the namespace was created to help avoid confusion. Now you may declare multiple variables with the same name inside different namespaces and you may call any one of them. It is just a way to limit a scope of a variable. Eg - namespace alpha { int var; }; namespace beta { int var; }; int main() { using namespace std; alpha::var = 2; beta::var = 4; cout<<beta::var<<endl; // Prints 4. }
9th Dec 2017, 4:30 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
Yes, as Forest suggests, first check if you are including the <string> library.
9th Dec 2017, 12:46 AM
Eric Blinkidu
Eric Blinkidu - avatar
+ 3
#include <string>
9th Dec 2017, 12:14 AM
Forest137 _
Forest137 _ - avatar
+ 2
And do check if you have this: using namespace std; or this: using std::string; in your code, or your strings look like this: std::string str;
9th Dec 2017, 4:20 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
@Keerthi This is C++, nothing else. And yes, I'm just a school student, so, I don't think I should be addressed as sir... // If that comment was meant for me, that is...
9th Dec 2017, 4:24 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
@kinshuk vasisht what is the use of "using namespace ?"
9th Dec 2017, 4:26 AM
Keerthi
Keerthi - avatar
+ 1
#include <string.h>
9th Dec 2017, 3:00 AM
Keerthi
Keerthi - avatar
+ 1
which language is this Sir?
9th Dec 2017, 4:21 AM
Keerthi
Keerthi - avatar
+ 1
thank you @kinshuk
9th Dec 2017, 4:31 AM
Keerthi
Keerthi - avatar
0
Namespacesare used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
9th Dec 2017, 4:30 AM
Sandeep - SJ
Sandeep - SJ - avatar