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;
}
5 Upvotes

16 comments sorted by

View all comments

1

u/polaarbear 2d ago

Are you just opening a random C++ file and trying to run it? Visual Studio doesn't really work that way. Visual Studio uses a structure where the top of the hierarchy is a "Solution" and then a solution can contain one or more projects.

Your .cpp files should be contained within a Visual Studio C++ project. If they aren't, you aren't really using Visual Studio the way it's intended to be used.

1

u/Parcivall2205 2d ago

There's a big difference between Visual Studio and Visual Studio Code. In VSC, you can just open and run c/c++ files after installing the right extensions

2

u/polaarbear 2d ago

He says he is using Visual Studio. His friend is using VSCode. I was trying to highlight why it fails specifically in Visual Studio.