-1

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!

su2187
  • 173
  • 1
  • 1
  • 9
  • 7
    I think you meant `std::string my_str;`. Did you get other error messages? Also, you need to `#include `. Oh, and `std::getline(std::cin, my_str);`. – Fred Larson Mar 11 '21 at 23:42
  • 2
    In fact, you could omit the `std::` on the `getline()` call, due to [Argument Dependent Lookup](https://stackoverflow.com/q/8111677/10077). But you need it on the `cin`. So, `getline(std::cin, my_str);` would also work. – Fred Larson Mar 11 '21 at 23:49
  • 1
    Instead of "bad" `using namespace std;` you can use "acceptable" `using std::getline;`. – Eljay Mar 11 '21 at 23:50
  • 1
    @Eljay: That isn't the problem. Look more carefully at the first code example. It has `std::getline`, but it's missing `std::` in a couple of other places. – Fred Larson Mar 11 '21 at 23:52
  • 5
    Based on the code and the stated error message, I suspect you are not looking at the **first** error reported. Always start with the first error, as later error might be artifacts of the first. For example, an error on the line defining a local variable could cause that definition to be ignored, resulting in an error whenever that variable is used. Those later errors are not real errors, just echoes of the first error. What is the *first* error reported by your compiler? – JaMiT Mar 12 '21 at 00:00
  • 2
    Also if your IDE has a dedicated "errors" view, ignore it and look for "compiler output" view instead. The latter should provide more details information about the errors. – rustyx Mar 12 '21 at 00:24
  • 1
    [Take a look at all your errors](https://godbolt.org/z/fW5567) and start by understanding and addressing the **first** error. – Drew Dormann Mar 12 '21 at 00:28
  • 1
    As a rule of thumb, if you remove `using namespace std` from a source file, then EVERY name you use that comes from namespace `std` needs to be prefixed with `std::`. In this case, that includes `string`, `cout`, `cin`, and `getline()`. There are a few exceptions to this rule of thumb - cases where you don't need to prefix with `std::` due to name lookup rules. The other thing to check is that all headers needed by your code are `#include`d. In this case, `std::string` is in a header `` which is not `#include`d. It is not guaranteed to be useable only with `#include `. – Peter Mar 12 '21 at 01:11
  • @FredLarson Yeah, I meant `std::string my_str`. And yes, after adding `std::getline(std::cin, my_str)` , my code works perfectly fine now. I didn't get any other errors after fixing these two lines. – su2187 Mar 12 '21 at 19:05
  • @Eljay After @MartinYork 's answer, I learnt that `std::getline` was a thing. Now my code works fine. – su2187 Mar 12 '21 at 19:08
  • @JaMiT , sorry I never used to see the first error, and always assumed the last error to be the actual error. Well at least I learned a thing today. Thanks! – su2187 Mar 12 '21 at 19:14
  • @DrewDormann , sorry I never used to see the first error, and always assumed the last error to be the actual error. Well at least I learned a thing today. Thanks! – su2187 Mar 12 '21 at 19:14
  • @Peter Yeah I learned this particular syntax only after @ MartinYork 's answer. Thanks for your help! – su2187 Mar 12 '21 at 19:17

1 Answers1

1

The issue is that a ot of things are in the std namespace.

Because you are not using using namespace std; everything that is in this namespace must be explicitly prefixed with std::.

In this situation my_str has a type string. But there is no type string there is a type std::string. You need to be explicit.

#include <iostream>

int main() {
    std::string my_str;
 // ^^^^^   Add this.

    std::cout << "Enter some text here -> ";
    std::getline(std::cin, my_str);
              // ^^^^^ Add this.

    std::cout << "You entered -> " << my_str << "\n";
                                       //    ^^^^^^^   probably add this
     
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Thanks a lot, it works now. Just a doubt, can I use "std:::endl;" instead of "\n"? – su2187 Mar 12 '21 at 04:08
  • 2
    You could use `std::endl` instead of `"\n"`. But that is bad practice. You should not force the flush (which endl does). Flushing is very inefficient when done incorrectly (and the system will do it correctly). – Martin York Mar 12 '21 at 05:13
  • 1
    @helloWorld • The number of times in the last 10 years where `std::endl` was the right thing to do rather than `"\n"` has been exactly: 0. (And on my platforms `"\n"` is better than `'\n'` because outputting a single char gets put into a 2-char array where the second char is `'\0'` null-terminator, and then calls the `char const*` output-er.) – Eljay Mar 12 '21 at 12:21