I just started learning C++ recently, so please excuse me if I have some mistakes in my question.
In most of the tutorials I have watched, I have been taught to use the "using namespace std;" statement. But later I found that it is a bad practice, so now I don't use it. The problem I have is that if I use the getline() function without "using namespace std;", I get an error. However, if i add "using namespace std;", it works. I'll show you some examples.
Without "using namespace std;"
#include <iostream>
int main() {
string my_str;
std::cout << "Enter some text here -> ";
std::getline(cin, my_str);
std::cout << "You entered -> " << my_str;
}
If I run this code, I get this error:
error: 'my_str' was not declared in this scope
Now here is the code with "using namespace std;":
#include <iostream>
using namespace std;
int main() {
string my_str;
cout << "Enter some text here -> ";
getline(cin, my_str);
cout << "You entered -> " << my_str;
}
This code runs without any errors.
Am I supposed to add any syntax? Could anyone please help me?
Thanks.
EDIT: Thanks everyone for your help!