Why is std::getline() underlined in Visual Studio?
Asked by
MellowCactus47
Whenever I type std::getline in my C++ file, Visual Studio shows a red underline and I can't run the program. The same code works for a friend using a different editor. I'm trying to read a name and check whether it is longer than 15 characters:
#include
int main() {
std::string name;
std::cout < 15) {
std::cout << "Your name can't be over 15 characters";
} else {
std::cout << "Welcome " << name;
}
return 0;
}
What do I need to change, and is there an alternative to getline?
Answers
Your code should be inside a proper C++ project in Visual Studio, with the .cpp file added to that project. Visual Studio is organized around solutions and projects, so opening a standalone source file may leave IntelliSense or the compiler configuration incomplete. Creating a C++ console project and adding the file to it should resolve project-related errors.
Reply to this answer
You don't need an alternative to getline. It is the right tool for reading an entire line, including spaces. Also, writing std:: explicitly is normal C++ style; using namespace std; can shorten the code, but avoiding it prevents name conflicts and is a better habit as programs grow.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions