-
Word variables
I am brand new to C++ coding and i have written a short program.
Code:
#include <iostream>
int main()
{
int pd;
int id;
std::cout<<"Loading security profile. Please enter user ID #: \n";
std::cin>> id;
std::cout<<"ID # acceped. Loading password protocols...\n";
std::cout<<"Protocols loaded. Initializing...\n";
std::cout<<"Protocols initialized. Please enter password: \n";
std::cin>> pd;
if (pd == 255 ) {
std::cout<<"Your password is correct. Please continue.\n";
std::cin.get();
}
else {
std::cout<<"Login failure. Security lockout online. Terminal shutdown.\n";
std::cin.get();
}
std::cin.get();
}
Now, what I want to do is use a word password instead of a number. And I have no idea how to do that. Any suggestions?
Thanks in advance.
-
Look into using the string library:
Code:
#include <iostream>
#include<string>
int main()
{
std::string pd;
int id;
std::cout<<"Loading security profile. Please enter user ID #: \n";
std::cin>> id;
std::cout<<"ID # acceped. Loading password protocols...\n";
std::cout<<"Protocols loaded. Initializing...\n";
std::cout<<"Protocols initialized. Please enter password: \n";
std::cin>> pd;
if (pd == "foobar" ) {
std::cout<<"Your password is correct. Please continue.\n";
std::cin.get();
}
else {
std::cout<<"Login failure. Security lockout online. Terminal shutdown.\n";
std::cin.get();
}
std::cin.get();
}
-
amazing...thank you.
EDIT: one thing. i forgot to add the #include<string> but the program still ran with no errors...why did that happen? is the library not necessary?
-
> but the program still ran with no errors...why did that happen?
Luck basically.
A lot of simple programs seem to work despite many things being wrong with them. But sooner or later, such mistakes will have a negative impact on your program.
-
Yeah, definately include the string library. Even though your compiler did not complain here, it might later on when you try to do more complicated things with strings. Also, if you ever plan on switching compilers you'll find that you'll suddenly get errors, for example the code I posted above would not work in Microsoft Visual Studio (it would complain about the the << and the ==) if you didn't include the string library.
-
If I were you, I'd keep this program around, because this would be good to use when you get into file I/O, where you can have ID's and their corresponding passwords in files...
-
The most likely reason why your app worked is that on your compiler (and others might behave differently), <iostream> internally includes <string>, perhaps indirectly (that is, it includes headers which include headers which include <string>). The important thing is that such behaviour is specific to your compiler, and more, to the specific version of compiler you're using and the specific vendor and version of standard library you're using (you can replace it independently from your compiler). So you must not rely on it.
Just as an example, the standard library that comes by default with Visual Studio.Net 2003 (Dinkumware 3.13, I think), includes <istream> from <iostream>. <istream> includes <ostream>, which includes <ios>, which includes <xlocnum>, which includes <streambuf>, which includes <xiosbase>, which includes <xlocale>, which includes <stdexcept>, which includes <xstring>, which is where this standard library defines the std::string class.
The mere length of this include chain and the fact that it includes internal secret headers of the standard library impementation (those starting with x) should be sign enough that you can't rely on any of it.