Error accured in get_money() function - c++

#include <iostream>
#include <iomanip>
int main ()
{
long double price;
std::cout << "Please, enter the price: ";
std::cin >> std::get_money(price);
if (std::cin.fail()) std::cout << "Error reading price\n";
else std::cout << "The price entered is: " << price << '\n';
return 0;
}
I am getting error forlling error while compiling the above program.
getmoney.cpp: In function int main()':
getmoney.cpp:10: error:get_money' is not a member of `std'.
get_money is the predefined function. I don't know why i am receiving this error.

If you included iomanipbut nevertheless the compiler issues an error it means that your compiler does not support C++ 2011 standard or you did not select such an option of the compiler.

Did you include the header "iomanip" in your code?

Wen you have such problem go to check on a reference website such as http://www.cplusplus.com/reference/iomanip/get_money/
and you will see that
This manipulator is declared in header <iomanip>.
and you even often have an example code which show you how to use it.
So including iomanip may solve your problem
If that is not working, you are probably using an older compiler which does not support C++ 11. And therefore you could simply not use this function without changing your compiler.

Related

Where is getline() function coming from?

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::.

cout not working with ostream header file

I have read that cout is an object of ostream...
But why does this code
#include<ostream>
using namespace std;
int main()
{
cout << "ostream included!" << endl;
return 0;
}
Throwing an error :-
practice1.cpp: In function 'int main()':
practice1.cpp:6:1: error: 'cout' was not declared in this scope
cout << "ostream included!" << endl;
^~~~
Am I going wrong in my understanding or is there some other fault?
(MinGW windows 10)
Thanks in advance!
Description
The reason of why this doesn't work is because cout is of type OStream but is inside the IOStream header. Hence, to get the definition of cout you need to include iostream library but not the ostream class.
Solution
Include iostream instead of ostream, as OriBS mentioned.
References
Object cout found in IOStream objects list
"Including iostream automatically includes also ostream..." see http://www.cplusplus.com/reference/iostream/
"The standard objects cout, cerr and clog are objects of this type." see http://www.cplusplus.com/reference/ostream/ostream/
You should include iostream
#include <iostream>

Is <cmath> or <math.h> really needed? Compiles without it

So, I have the following code, and it builds and runs perfectly, tried various values and all is well. You'll notice I use log10 function and I do not include cmath or math.h. Why does it still build and run fine? Are those libraries really needed? Why/Why not? Does it have anything to do with me using visual studio? Like, would it not compile if say I was using a different IDE or command prompt to compile it?
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << "Classify solutions as acidic or nonacidic" << endl<< endl;
//declaring double molar concentration
double mc = 1;
//using while and if statements to calculate pH in fixed notaion and acidic or nonacidic
while (mc != 0)
{
cout << "Please enter a molar concentration (or enter 0 to exit): ";
cin >> mc;
if (mc != 0)
{
cout << "Molar Conentration = " << scientific << mc << endl; //scientific notation
double pH = -log10(mc);
cout << "pH = " << fixed << setprecision(6) << pH << endl; //6 deciumals
if (pH > 7)
{
cout << "Nonacidic" << endl << endl;
}
else if (pH < 7)
{
cout << "Acidic" << endl << endl;
}
else
cout << "Neutral" << endl << endl;
}
}
//end program when inputing 0
cout << "End of Program" << endl;
return 0;
}
The code:
i = i++ + ++i;
may also compile okay, but that doesn't make it a good idea :-)
It would be wise to include headers for library functions that you use. You don't lose any functionality by doing so but you do guarantee the functionality will work (misuse notwithstanding).
Detailed analysis follows.
Even if an implementation is relaxed about this, the standard mandates it. C++11 17.6.2.2 Headers /3 states:
A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header.
The gcc compiler, for example, will grumble bitterly about your code:
xyzzy.cpp: In function 'int main()':
xyzzy.cpp:22:34: error: 'log10' was not declared in this scope
double pH = -log10(mc);
^
As to why VC++ seemingly violates this rule, it has to do with the fact that header files are allowed to include other header files.
If you compile your code to produce pre-processor output (with /P), you'll find a line buried deep within it thus (at least in VS2013):
#line 1 "c:\\blah\\blah\\vc\\include\\cmath"
And a bit of analysis turns up the following hierarchy of includes:
iostream
istream
ostream
ios
xlocnum
cmath
(<xlocnum>, one of the internal headers used by <locale>, appears to need ldexp() from the <cmath> library, though there may be others as well).
That's further evidenced by the fact that VC++ does complain about the following code:
//#include <iostream>
using namespace std;
int main() {
double oneHundred = 100;
int two = log10 (oneHundred);
return two;
}
with:
error C3861: 'log10': identifier not found
but that error disappears the instant you uncomment the iostream inclusion line.
However, as previously stated, that is not behaviour you should rely on. If you are going to use a library function (or macro/template/whatever), it's up to you to include the correct header.
Otherwise your program compiling correctly is simply an accident.
As you've noticed, the code snippet you provided works in Visual Studio but not with other compilers. This is because of how the standard library is implemented for each compiler.
It turns out that when you include Visual Studio's implementation of <iostream>, you end up including a bunch of other headers indirectly, and one of these headers is <cmath>.
To see the exact chain, navigate to the standard library include directory. For me (I use Visual Studio 2013 Community Edition) this is located at
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
Open iostream. Note the line #include <istream>
Open istream. Note the line #include <ostream>
Open ostream. Note the line #include <ios>
Open ios. Note the line #include <xlocnum>
Open xlocnum. Note the line #include <cmath>
Guess what? You included cmath when you included iostream... so your code is good to go, on Visual Studio at least. But, don't rely on implementation details or your code will break if you try to migrate it to another platform/toolchain.
For example, trying to compile the provided snippet using g++ on Cygwin results in the following error:
temp.cpp: In function ‘int main()’:
temp.cpp:22:34: error: ‘log10’ was not declared in this scope
double pH = -log10(mc);
This must mean g++'s implementation of <iostream> doesn't depend on <cmath>

Error: C++ requires a type specifier for all declarations

I'm new to C++ and I've been reading this book. I read a few chapters and I thought of my own idea. I tried compiling the code below and I got the following error:
||=== Build: Debug in Password (compiler: GNU GCC Compiler) ===|
/Users/Administrator/Desktop/AppCreations/C++/Password/Password/main.cpp|5|error:
C++ requires a type specifier for all declarations| ||=== Build
failed: 1 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|.
I don't understand what is wrong about the code, might anyone explain what's wrong and how to fix it? I read the other posts but I was unable to understand it.
Thanks.
#include <iostream>
using namespace std;
main()
{
string password;
cin >> password;
if (password == "Lieutenant") {
cout << "Correct!" << endl;
} else {
cout << "Wrong!" << endl;
}
}
You need to include the string library, you also need to provide a return type for your main function and your implementation may require you to declare an explicit return statement for main (some implementations add an implicit one if you don't explicitly provide one); like so:
#include <iostream>
#include <string> //this is the line of code you are missing
using namespace std;
int main()//you also need to provide a return type for your main function
{
string password;
cin >> password;
if (password == "Lieutenant") {
cout << "Correct!" << endl;
} else {
cout << "Wrong!" << endl;
}
return 0;//potentially optional return statement
}
You need to declare the return type for main. This should always be int in legal C++. The last line of your main, in many cases, will be return 0; - i.e. exit successfully. Anything other than 0 is used to indicate an error condition.
One Extra Point:
You can also get exact same error if you try to assign variable in class. Because in C++ you can initialize variables in class but can't assign later after variable declaration but if you try to assign in a function which is defined in the class then it would work perfectly fine in C++.

Warning when compiling with clang++

When I compile the code:
#include <iostream>
#include <string.h>
using namespace std;
int main(void) {
string m1;
cout<< "enter your name: "<<endl;
getline(cin,m1);
cout << "Your name is: " << m1 << endl;
return EXIT_SUCCESS;
}
It give the following warning:
type of symbol `_main' changed from 32 to 512 in >C:\Users\KDesktop\AppData\Local\Temp\cc7XPBuL.o
Secondly, the compiler does generate an .exe file, but whenever I run it, the program crashes immediately. Can someone help me with this issue.
Thank you
Your toolchain is outdated. Older versions of LLVM used the wrong value for the function symbol type; the bug was fixed in late 2010. Here's the bug report: http://llvm.org/bugs/show_bug.cgi?id=8320
You should upgrade your LLVM; the problem will go away.