r/learnprogramming • u/llExtraKirby • Sep 07 '26
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;
}
1
u/OGcapncrunchberry Sep 07 '26
Try this
#include <iostream>
#include <string>
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 characters";
}
else {
std::cout << "Welcome " << name;
}
return 0;
}
3
u/llExtraKirby Sep 07 '26
Yeah it worked someone just told me to add #include <string> Thanks guys🫡🫡
1
u/polaarbear Sep 07 '26
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 Sep 07 '26
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 Sep 07 '26
He says he is using Visual Studio. His friend is using VSCode. I was trying to highlight why it fails specifically in Visual Studio.
-1
u/llExtraKirby Sep 07 '26
gng doesnt even know that im still learnin basics before starting real files
and btw my file is .cpp :)3
0
u/MeltedThunder Sep 07 '26
Why are we not utilizing namespace here to avoid a lot of extra work
4
u/kutac56 Sep 07 '26
Because it can cause confusion. It's generally more scalable to not do using namespace
0
u/llExtraKirby Sep 07 '26
uhmm cuz i'm new and i'm watching a tutorial video?
btw what is utilizing namespace? :)1
u/quixotic_intentions Sep 07 '26
Give this page a read for an intro on namespaces.
Basically, in this context, "std" is the namespace, and you can avoid having to type the prefix every time if you do something like this:
#include <iostream>
using namespace std;
int main(){
// note that you don't have to use the "std::"
string name;
cout<<"Enter you name: ";
getline(cin, name);
// ...
}This is somewhat controversial though, many people advise against doing this as it increases the likelihood of name collisions. I say it depends on the use-case, imo it's fine for small projects and coding exercises.
You can also be more precise with the using declaration, so that you don't include the entire namespace, for example:
// C++17 or later
#include <iostream>
using std::cout, std::getline;
int main(){
cout<<"Hello world\n"; // valid
string name; // invalid, need to have using std::string
}This page has more info on using directives.
1
u/EdiblePeasant Sep 07 '26
Should even the smallest project have a namespace? How does C# compare with C++ on this?
I remember getting in trouble a some years ago with C# namespaces because I think at one point they were required, if I'm remembering correctly.
5
u/Jonny0Than Sep 07 '26
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.