I am starting learning C++ a little while ago.
Came to know about header files and preprocessor statements.
I know that std::cin and std::cout are the objects/Function is declared in standard library IOSTREAM.
But when taking input as a string and to read the whole line. We have to getline() function or at least the instructor is using it in the video.
Now I have checked on the internet and most of the sites are showing that getline() is defined under STRING file/Library. But the thing is my program is working perfectly fine even without including the string file. So what I am missing.? or doing something wrong. and if you can please also explain how getline function is working. and also please what's the actual difference between using namespace std, and using std::
Thank you
#include <iostream>
int main()
{
std::string str;
std::cout << "Please enter your name: \n";
getline(std::cin, str);
std::cout << "Hello, " << str
<< " welcome to GfG !\n";
return 0;
}
You have to include the header <string> to use the function std::getline. It is implementation defined whether the header <iostream> includes the header <string>.
In this call using unqualified name getline
getline(std::cin, str);
the compiler applies the argument dependent lookup ADL (the first argument std::cin is defined in the namespace std::) and finds the corresponding name std::getline in the namespace std::.
Related
This question already has answers here:
String Undeclared In C++
(5 answers)
Closed 1 year ago.
I am new to C++ and am currently working on a project that wants me to turn a Roman Numeral into a Hindu-Arabic number (our normal number system). I'm currently writing a class RomanNumeralType which needs to store a roman numeral type string. When I try to run the code below I get error code C3646: unknown override specifier and also error codes C4430 and C2061 which both address string (in bold) not being a valid identifier. It was my impression that #include fixed this however I am unsure why it is not working. I currently do not have a function that actually converts the numeral to a number but I want to fix this problem first. How do I get the string data type to work in my class?
Here is a minimally reproduced example of my code
//main
#include <iostream>
#include <iomanip>
#include "Header2.h"
#include <string>
using namespace std;
int main() {
string ans;
cout << "Please enter a string of Roman Numerals (M,D,C,L,X,V,I): " << endl;
cin >> ans;
}
RomanNumeralType r1(ans);
cout << "Your original Roman Numeral was: " << r1.getNumeral() << endl;
cout << "Your Roman Numeral as a integer is: " << r1.getNumber(r1.getNumeral()) << endl;
return 0;
}
//Header2.h
#ifndef HEADER2
#define HEADER2
#include <string>
class RomanNumeralType {
public:
**string** romanNumeral;
RomanNumeralType(**string** x)
{romanNumeral = x;}
**string** getNumeral()
{return romanNumeral;}
}
When you do #include <string> think of it as though you are pasting in the contents of a file shared across all developers that has all the definitions for string. That class lives within the std namespace, so when you want to use a string, you have to tell the compiler that string is in std. The preferred method is to use the fully qualified name, so every time you use string, you would replace it with std::string.
The other option is to do what you did in main.cpp, using namespace std;This tells the compiler that the namespace to look in is in std. This is generally considered bad practice for actual programs, but it is OK if it is a small, one off program like you seem to be writing.
If you do the first option, you will have to change everything in the std namespace such as cout, but it is clearer that you didn't write your own.
#include <istream> //Includes the input/output library
using namespace std; // Makes std features available
// The main function of the program
// It outputs the greeting to the screen
int main() {
count <<"Hello World! I am C++ Program." <<endl;
return 0;
}
IntelliSense: no operator message Line 7, Column 8
error C2563:mismatch in formal parameter list Line 7, Column 1
Replace #include <istream> with the correct header, #include <iostream>.
Helpful mnemonic:
io = input/output
stream = "stream of data"
Additionally, the name of the standard output stream is std::cout or cout with the std:: namespace scope removed.
Helpful mnemonic:
std:: = Standard library's
cout = console output
The problems you are having with your simple block of code is simply: spelling errors.
Firstly, you have misspelled the input/output stream file in your include statement, so you need to rename the header file to:
#include <iostream>
There is no heade file named istream.
Secondly, you also misspelled the cout function to count. Change that line to:
cout << "Hello World! I am C++ Program." << endl;
Those lines should work now.
Also a recommendation for your future programs; avoid using the line
using namespace std;
Why? Because as you move on to more complex programming, you will undoubtedly learn and begin to define a data type or variable, and sometimes, that name may also be used by the standard library. As a result, you will have a hard time trying to differentiate the variables or data types you defined and the ones defined in the std library.
Therefore, try and attach std:: before every function that is a part of the standard library.
EDIT:
The code you posted in the comments box is pretty unreadable, so I just fixed it and have posted it below:
#include <iostream> //Includes the input/output library
using namespace std; // Makes std features available
// The main function of the program
// It outputs the greeting to the screen
int main()
{
cout <<"Hello World! I am C++ Program." <<endl;
return 0;
}
I've tried this in my IDE and fixed with the same and only recommendations from above. It works for me.
I am following the C++ Primer book and trying out all the code examples.
I am intrigued by this one:
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
string line;
while (getline(cin,line))
cout << line << endl;
return 0;
}
Before compiling this code I was guessing that the compilation would fail, since I am not using
while (std::getline(cin,line))
Why is getline in the global namespace?
As I understand, this should only happen if I used
namespace std;
or
using std::getline;
I am using g++ version 4.8.2 on Linux Mint Debian Edition.
This is argument dependent lookup.
Unqualified lookup (what you are doing when you just call getline() instead of std::getline()) will start by trying to do normal name lookup for getline. It will find nothing - you have no variables, functions, classes, etc. in scope with that name.
We will then look in the "associated namespaces" of each of the arguments. In this case, the arguments are cin and line, which have types std::istream and std::string respectively, so their associated namespaces are both std. We then redo lookup within namespace std for getline and find std::getline.
There are many more details, I encourage you to read the reference I cited. This process is additionally known as Koenig lookup.
Since std::getline() for std::string is defined in the header I would have to say that Argument-dependent lookup is coming into play.
When you use getline(cin, line), it is equivalent to using getline(std::cin, line) since you have the line:
using std::cin;
Using Argument Dependent Lookup (ADL), the compiler is able to resolve that function call to std::getline(std::cin, line). You can read more about ADL at http://en.cppreference.com/w/cpp/language/adl.
Why is getline() from header string in local scope and can be used:
#include <iostream>
#include <string>
int main() {
std::string str;
getline(std::cin, str);
std::cout << str << "\n";
return 0;
}
That works with gcc. But why? It is defined in header string, which should required me to use std::getline() instead of getline().
You're experiencing Argument Dependent Lookup (ADL, and also referred to as Koenig Lookup). Since one or more of the arguments is a type defined in the std namespace, it searches for the function in the std namespace in addition to wherever else it would search. I point you to Stephan T. Lavavej's video to learn more about it and name lookup in general.
In the C++ program below, I include the string.h file and I successfully instantiate the C++ string class in it and call one of its member functions: size().
#include <iostream>
#include <string.h>
using namespace std;
int main( )
{
string s = "Hello";
cout << "String: " << s << endl;
cout << "Size of string: " << s.size() << endl;
cin.get();
return 0;
}
The output is:
String: Hello
Size of string: 5
I am using Dev-C++ 4.9.9.2
My question: doesn't the string.h file just provide the functions for manipulating C strings? It doesn't include the definition of the C++ string class right? So, how is it that I am able to access the C++ string class without using #include <string>? My understanding is that the string.h file is the C strings library file and <string> includes the C++ string library file. Is this not right?
Thanks!
This is because std::string is defined though one of the files included in the <iostream> header. The streams provide support for input and output of strings, so they need to include a string header in order to define the corresponding >> and << operations.