Ok, i found this that works with spaces on the FAQ:
Code:
#include <iostream>
#include <string>
int main()
{
std::string line;
std::cout<<"Enter a string: ";
if ( getline ( std::cin, line ) )
std::cout<<"You entered \""<< line <<"\""<<std::endl;
}
and it works fine, but, i wanted to use it without the std:: for this program
(Simple i know):
Code:
#include <iostream>
#include <string>
using namespace std;
string pass;
string user;
int main()
{
cout<<"What is your username?(12 characters max): ";
cin.getline ( user, 12, '\n' );
cin.ignore();
cout<<"What is your password?(12 characters max): ";
cin.getline ( pass, 12, '\n' );
cin.ignore();
if (user == "The Master" && pass == "I am master")
{
cout<<"Access is granted.\n";
cout<<"Press enter to end user authentication.";
cin.get();
}
else
{
cout<<"Access denied";
cin.get();
}
}
however when i do that, i get the following error from my compiler(Dev-C++):
11 C:\Dev-Cpp\Tutorial\Inproved user authentication.cpp no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::string&, int, char)'
Along with a similar error for line 14, (The 11 is the line number).
Both these errors seem to pertain to cin.getline which of course works fine if i use C strings instead. Ok, please tell me if there is a shorter code for the strings with spaces code, and if so how to use it. Thanks