C++ Fundamental types

Sorry about bad categorization, but there was no C++ category.
Im following C++ course and it is great.
However, in Basic course (1st one), under “Fundamental Data Types”, lesson #10 - Working with characters, I keep getting error.
getline() keeps getting error, regardless if I supply it with 2 parameters or not. Error I keep getting is:
" Identifier “getline” is undefined".
I tried searching online for solution, someone mentioned to point VS to where it is - but I have just scratched C++ and I have no idea how to solve it. Copied Mosh`s code to the letter - same issue.
Help?
Code in my VS:

#include <iostream>
using namespace std;


int main()
{
	cout << " What is your street? ";
	string street;
	getline(cin, street);

}

Nothing wrong with your code. I ran it with CLion, and everything works. For CLion, everything is there, you just press play to run your code, and you don’t need to set your own C++ compiler. I would check your system configuration since it didn’t recognize the getline function.

Are you using VSCode? If so, perhaps try following this canonical guide:

No mate. I use VS Code for Python and others, for C++ I am using Microsoft Visual Studio Community Edition x64 ( as per suggestion from Mosh`s video), v17.3.0 from 2022.
Complete data:

Microsoft Visual Studio Community 2022
Version 17.3.0
VisualStudio.17.Release/17.3.0+32804.467
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Community

Visual C++ 2022   00482-90000-00000-AA154
Microsoft Visual C++ 2022

ASP.NET and Web Tools   17.3.375.53775
ASP.NET and Web Tools

Azure App Service Tools v3.0.0   17.3.375.53775
Azure App Service Tools v3.0.0

C# Tools   4.3.0-3.22401.3+41ae77386c335929113f61d6f51f2663d2780443
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

NuGet Package Manager   6.3.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

TypeScript Tools   17.0.10701.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   4.3.0-3.22401.3+41ae77386c335929113f61d6f51f2663d2780443
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual Studio IntelliCode   2.2
AI-assisted development for Visual Studio.

Per documentation for std::getline, it looks like it may be defined in the string package, so try including that in your imports:

#include <string>
#include <iostream>
2 Likes

This solved it. Thank you very much!

Me again.
Again in first C== course, section “For loops” - lesson “Range based for loops” been following Mosh`s instructions and typed same as he did. However my code runs 12 times, instead of 3, like in CLion does. Could it be due to MS VS IDE?
Code:

#include <iostream>
#include <string>
using namespace std;


int main()
{
	int numbers[] = { 1, 2, 3 };
	for (int i = 0; i < sizeof(numbers); i++)
		cout << numbers[i] << endl;
	cout << sizeof(numbers);

	return 0;
	

}

Output I get:

1
2
3
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
-858993460
11
12

I know addresses from index 3 to 10 contain garbage. But what is value 11?!
For output 12 I know its cout call of sizeof() function, but why am I getting this strange data?
And does in fact in my IDE C++ iterates over every single byte, rather than int value (3x4 bytes = 12).
Very confusing for a newbie like myself… :frowning:

Small update: my VS iterates over every byte, producing wrong output. :smiley:
Tried adding 4th number to array - iterated 16 times.
Changed data type to short - iterated 8 times…
How to solve this?

Disregard all, Mosh explained, I was too quick to type. Sorry.

1 Like