Why is std::getline() underlined in Visual Studio?

0
1
Asked By MellowCactus47 On

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?

2 Answers

Answered By SunnyOrbit64 On

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.

Answered By QuietMarble29 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.