. Advertisement .
..3..
. Advertisement .
..4..
I don’t know what I’m doing wrong, but I’ve already lost a couple of days struggling with this. Here is my command line:
#include <iostream>
using namespace std;
main()
{
string password;
cin >> password;
if (password == "Lieutenant") {
cout << "Correct!" << endl;
} else {
cout << "Wrong!" << endl;
}
}
This returns:
||=== 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 have any experience with the “c++ requires a type specifier for all declarations.” In this case, how should I change?
The cause:
You’re missing a string library, and the return type for the main function isn’t given. You’ll receive the same problem if you attempt to assign a variable in a class, because you can initialize variables in a class but not assign them later after variable declaration in C++.
Solution: You must include the string library, as well as give a return type for your main function. Depending on your implementation, you may be required to define an explicit return statement for
main
, as follows:It is necessary to declare the return type of main. In legal C++, this should be
int
In many cases, thereturn 0;
line at the end of your main will bereturn 0;
, i.e. You can exit successfully. Any other0
can be used to indicate an error condition.