can anybosy please write me a code that have following specs
*using array
*user registration
*username and password check that is login
*allow the user to vote for a particular person(once)
*publish the results
Printable View
can anybosy please write me a code that have following specs
*using array
*user registration
*username and password check that is login
*allow the user to vote for a particular person(once)
*publish the results
Probably, but you should do your best to write it yourself, and then approach us for help if you run into any problems.
i tried my best......can u pls help me how to make the login only once..that is if he tries to login more than one time he should not be able to login.....
So, what did you try? Post the code.
one have only this much...after this i have to make sure sure that the user should be able to login only once as this is a voting machine.....Code:#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;
void Auth();
void Members();
void Userchange();
void Passchange();
string inuser;
string inpass;
string user;
string pass;
int num = 0;
string com;
main()
{
system("cls");
cout<<"Welcome\n---------------------------\n\n";
while(num==0)
{
system("cls");
cout<<"Command: ";
cin>>com;
if(com=="login")
{
Auth();
}
else if(com==com)
{
cout<<"Unknown command\n";
}
}
}
void Auth()
{
ifstream Passfile("password.txt", ios::in);
Passfile>>inpass;
ifstream Userfile("username.txt", ios::in);
Userfile>>inuser;
system("cls");
cout<<"USERNAME: ";
cin>>user;
cout<<"PASSWORD: ";
cin>>pass;
Userfile.close();
Passfile.close();
if(user==inuser&&pass==inpass)
{
cout<<"\nHit enter to continue to members area";
getch();
Members();
}
else
{
cout<<"nope";
getch();
main();
}
}
Since your code appears to be C++ rather than C, I am moving this thread to the C++ programming forum.
Let's deal with your current code first. You should:
- Change those global variables into local variables.
- Indent your code properly.
- Never call the global main function recursively (use a loop).
- Note that <conio.h> and getch are non-standard.
As to your problem: I would expect that you need to store several username/password combinations in the file. You then read them from the file into parallel std::vector<std::string> containers. With these at hand, you can then search the container with the usernames to see if the username entered by the user matches any of those that were already entered. If not, you then create a new entry, otherwise you update the corresponding vote if the password matches.
actually i am a beginner in c++ and c...so i am in learning stage...so not able to unserstand what you are coveying.....is there any siimple way
Kunku,
I took Laserlight's comments and implemented them. Especially the scope (local/global) of the variables, and the indention. I don't recall if I've ever tried to call main recursively, I think that an early professor did a song an dance about it early in college. He made reference to material we learned in Java, but it had to do with a concept later referred to as a single entry and single exit...it showed up when we started discussing automata theory. Also, take note of the calls to the "system," I am using a Linux emulator so it's "clear" rather than "cls," and I wrote a "pause" program to wait for a user's key press and left it in the working directory. It is included below:
Laserlight, I was rather ignorant of the name space behaviors which are being imposed in the current versions of the C++ standard...(I believe 0x is the next), so I intentionally used the qualified names of library variables/constants/functions.Code:#include <iostream>
#include <fstream>
void Auth();
void Members();
void Userchange();
void Passchange();
void WaitForKeypress();
int main(void)
{
int num = 0;
std::string com;
while(true)
{
system("clear");
std::cout << "Welcome\n---------------------------\n\n" << std::endl;
std::cout << "Command: ";
std::cin >> com;
if( 0 == std::strcmp(com.c_str(),"login") )
{
Auth();
}
else
{
std::cout << "Unknown command\n" << std::endl;
WaitForKeypress();
std::cin.clear( );
}
}
return 0;
}
void Auth()
{
std::string inuser; // read from system file
std::string inpass;
std::string user; // read from user input
std::string pass;
std::ifstream Passfile("password.txt", std::ios::in);
std::ifstream Userfile("username.txt", std::ios::in);
if( !Passfile.good() || !Userfile.good() )
{
// close the offending file...
if( !Passfile.good() )
{
Passfile.close();
}
if( !Userfile.good() )
{
Userfile.close();
}
// discontinue program execution...
std::cerr << "User and/or Password file error...exiting." << std::endl;
std::exit( 1 );
}
Passfile >> inpass;
Userfile >> inuser;
// appearantly there is only one user/password pair in the files, thus they
// may be closed now, rather than after prompting the user for their
// credentials
Userfile.close();
Passfile.close();
system("clear");
std::cout << "USERNAME: ";
std::cin >> user;
std::cout << "PASSWORD: ";
std::cin >> pass;
if( user == inuser && pass == inpass )
{
WaitForKeypress();
Members();
}
else
{
std::cout << "\n Login failed." << std::endl;
std::exit( 1 );
}
return;
}
void Members()
{
std::cout << std::endl << "void Members()" << std::endl;
WaitForKeypress();
return;
}
void Userchange()
{
std::cout << std::endl << "void Userchange()" << std::endl;
WaitForKeypress();
return;
}
void Passchange()
{
std::cout << std::endl << "void Passchange()" << std::endl;
WaitForKeypress();
return;
}
void WaitForKeypress()
{
system("./pause");
return;
}
When I was implementing the "pause" code which follows, I noticed cin's state had been reset upon executing the external program "pause" -- which is clearly because the "pause" program "inherited" the state from the newly invoked shell executing it. The "pause" program would then exit, and the client "vote" would still have an "eof" in the input queue, and loop indefinitely. The test that produced the aforementioned behaviors, corrected with the "cin.clear()" call in the code above, was an end-of-file (Ctrl-D) at a user prompt.
What state is the input stream in initially, do you happen to know? I'll go look for the answer myself, but I figured I would volunteer a question while I was attempting to demonstrate good form for the new learner. I tried several variations on the cin.clear() calls, and found that passing no parameters was the only effective recourse. I'll implement the std::vector suggestion for the next post/reply....Code://
// wait for a keypress from the user, and exit
#include <iostream>
int main( void )
{
char input;
if( std::cin.eof() )
{
return 254;
}
if( ! std::cin.good() )
{
return 253;
}
std::cout << std::endl << "Press Enter to continue...";
do
{
input = std::cin.get();
}
while( std::cin.good() && '\n' != input );
return 0;
}
Best Regards,
New Ink -- Henry