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:
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;
}
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.
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.
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;
}
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....
Best Regards,
New Ink -- Henry