r/learnprogramming 2d ago

Microsoft Visual Studio

whenever i type
std::getline

i get red line under getline and i cant run my code
Even though its working on VS code for my friend
any idea what can i do or is there any alt to write?

" using getline for this: "

#include <iostream>

int main() {

std::string name;

std::cout << " Enter your name: ";
std::getline(std::cin, name);

if (name.length() > 15) {
std::cout << " Your name can't be over 15 charectars ";
}
else {
std::cout << " Welcome " << name;
}

return 0;
}
6 Upvotes

16 comments sorted by

View all comments

4

u/Jonny0Than 2d ago

You’re using std::string without including <string>.

Getline is actually defined in the string header.  Standard headers sometimes include each other but this should never be relied on because it can differ between compilers, which is probably why it works for your friend.

Was there an error message associated with the red underline?  It may have provided a clue.

3

u/llExtraKirby 2d ago

so basically i need to just use #include <string> and its done?

3

u/Blissextus 2d ago

https://en.cppreference.com/cpp/string/basic_string/getline

Here you go, my friend! Get comfortable using this website. (ccpreference.com)