Hi,
I need a program to search all lines of a file (a whole file) for a certain string. In my concrete case, I need it for login. Let me sum all of this up, so you get the whole overall point of what I have so far:
- a user enters a username (inputUsername) and a password (inputPassword)and these 2 strings are connected into a new string: userAndPass
- the file which contains multiple lines of [username] [password] patterns is opened and searched
- when the line with the entered [username] [password] is found, which is practically equal to userAndPass string, the user is forwarded to the main menu.
Here's the code of the Login.h header file.
`Inline Code Example Here
#pragma once
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <iosfwd>
#include <iterator>
#include <algorithm>
#include "Lek.h"
using namespace std;
class Login
{
public:
string username, password;
string inuser, inpass;
char opcija;
ofstream usersFile;
string inputUsername;
string inputPassword;
Lek lek1;
// User registration
void userRegisterDo()
{
if ( !usersFile.is_open())
{
usersFile.open("userData.txt");
}
usersFile << username << " " << password << endl;
usersFile.close();
}
// Initialize user registration
void userRegister()
{
cout << "Dobrodosli!\n-------------------------\n\nMolimo registrujte se.\nUnesite novi username:\n";
cin >> username;
cout << "\nUnesite novi password:\n";
cin >> password;
userRegisterDo();
}
// User login function
void login()
{
cout << "Username:\n";
cin >> inputUsername;
cout << "\nPassword:\n";
cin >> inputPassword;
string userAndPass = inputUsername + " " + inputPassword; // Ono sto se trazi
int offset;
string line;
ifstream usersFile;
usersFile.open ("userData.txt");
if(usersFile.is_open())
{
while(!usersFile.eof())
{
istream_iterator<char> eof;
getline(usersFile,line);
if (eof == search(istreambuf_iterator<char>(usersFile), eof, userAndPass.begin(), userAndPass.end())) { // ako je pronadjen login
// Autentikacija je uspela
system("cls");
// Meni aplikacije
do
{
cout << endl;
cout << "Dobrodosli. Odaberite opciju:\n"
<< "1. Provera starosti leka\n"
<< "2. Unos novog leka\n"
<< "3. Brisanje leka\n"
<< "4. Pregled lekova\n"
<< "5. Izlaz"
<< endl << endl;
cin >> opcija;
cout << endl;
switch (opcija)
{
case '1':
system("cls");
lek1.unosDatumaProizvodnje();
lek1.starostLeka();
lek1.prikazStarostiLeka();
break;
case '2':
system("cls");
lek1.unosLeka();
system("cls");
break;
case '3':
system("cls");
lek1.brisanjeLeka();
getch();
system("cls");
break;
case '4':
lek1.pregledLekova();
cout << endl << "Kraj fajla.\n";
getch();
system("cls");
break;
case '5':
exit(1);
break;
default:
cout << endl;
system("cls");
cout << "Uneli ste pogresnu opciju. Pokusajte ponovo!\n";
cout << endl;
break;
}
} while (opcija != '5');
}
else // ako login nije pronadjen
{
cout << "\nUsername i/ili sifra nisu dobri!\n\n\n\n";
login();
}
}
usersFile.close();
}
else
cout << "Fajl userData.txt se ne moze otvoriti." << endl;
long begin, end;
if (usersFile.good())
{
cout << "userData.txt pronadjen!\n\n";
}
else {
userRegister();
}
if (usersFile.is_open())
{
begin = usersFile.tellg();
usersFile.seekg (0, ios::end);
end = usersFile.tellg();
usersFile.close();
if (begin == end)
{
userRegister();
}
else
{
login();
}
}
getch();
}
};
I didn't post any other files here, because they're not directly related to the problem I have.
The error I receive after compiling the code is:
Output from Build
1>------ Build started: Project: IT320 Pharmacy, Configuration: Debug Win32 ------
1>Build started 22-Jun-12 21:50:38.
1>InitializeBuildStatus:
1> Touching "Debug\IT320 Pharmacy.unsuccessfulbuild".
1>ClCompile:
1> Main.cpp
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(63): warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\time.inl(112) : see declaration of 'localtime'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(148): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(155): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(168): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(175): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(188): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(195): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(205): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(214): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(224): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(237): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(263): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(281): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(295): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(308): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\lek.h(313): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\login.h(74): error C2780: '_FwdIt1 std::search(_FwdIt1,_FwdIt1,_FwdIt2,_FwdIt2,_Pr)' : expects 5 arguments - 4 provided
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(621) : see declaration of 'std::search'
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\login.h(74): error C2782: '_FwdIt1 std::search(_FwdIt1,_FwdIt1,_FwdIt2,_FwdIt2)' : template parameter '_FwdIt1' is ambiguous
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(581) : see declaration of 'std::search'
1> could be 'std::istream_iterator<_Ty>'
1> with
1> [
1> _Ty=char
1> ]
1> or 'std::istreambuf_iterator<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\login.h(162): warning C4244: '=' : conversion from 'std::streamoff' to 'long', possible loss of data
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\login.h(165): warning C4244: '=' : conversion from 'std::streamoff' to 'long', possible loss of data
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\program.h(37): warning C4244: '=' : conversion from 'std::streamoff' to 'long', possible loss of data
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\program.h(40): warning C4244: '=' : conversion from 'std::streamoff' to 'long', possible loss of data
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.72
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
These two lines are colored red:
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\login.h(74): error C2780: '_FwdIt1 std::search(_FwdIt1,_FwdIt1,_FwdIt2,_FwdIt2,_Pr)' : expects 5 arguments - 4 provided
1>c:\users\boris\documents\visual studio 2010\projects\it320 pharmacy\it320 pharmacy\login.h(74): error C2782: '_FwdIt1 std::search(_FwdIt1,_FwdIt1,_FwdIt2,_FwdIt2)' : template parameter '_FwdIt1' is ambiguous
Do you have any idea about how to solve these issues? Thanks in advance. If you need any more code or info, tell me.