|
查看: 958|回复: 6
|
C++ 问题 !!! 紧急 !!!
[复制链接]
|
|
|
#include <iostream>
#include <cmath>
#include <iomanip>
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <string>
#include <cstdlib>
#include <fstream>
using std::ifstream;
using namespace std;
char staffname[50],staffpwd[50],blank[50],username[50],userpwd[50],userid[10],staffid[10];
char astaffname[50],astaffpwd[50],ausername[50],auserpwd[50],auserid[10],astaffid[10];
using namespace std;
void loginmenu1();
int lic,day,B,E;
double pay1,peak,dr,mc;
char buffer [80],cname[30],season1,code1,code2,car_type[20],task,task1,task2;
void menu1()
{
cout << "\aWelcome, " << staffname << " [" << staffid << "] :\n\n"<<endl;
a: cout<<" lease select a task .\n"<<endl;
cout<<"[N]ew Rental ."<<endl;
cout<<"[E]xit ."<<endl;
cin>>task;
switch(task)
{
case 'N':
break ;
case 'E':
exit(1) ;
break ;
default :
cout<<"Invalid selection please select again ."<<endl;
goto a;
break ;
}
}
void adminlogin()
{
bool login;
system("color bc" ;
//open file
ifstream fsdbfile( "admin.dat", ios::in || ios: ut);
if ( !fsdbfile )
{
cerr << "File could not be opened" << endl;
exit (1);
}
//login part
do
{
cout << " Please Insert your staff ID : ";
cin >> astaffid;
cout << " Please Insert your password : ";
cin >> astaffpwd;
fsdbfile >> auserid >> auserpwd;
while ( !fsdbfile.eof() )
{
if ( auserid == astaffid )
{
break;
}
fsdbfile >> auserid >> auserpwd;
}
if (strcmp(auserpwd,astaffpwd) != 0)
{
system ("cls" ;
cout << "Invalid Login!!\n";
fsdbfile.clear();
fsdbfile.seekg(0);
}
else
{
login = true;
loginmenu1();
}
}while(login != true);
}
void admin1()
{
FILE * pFile;
pFile = fopen ("user.dat","a+" ;
cout<<" lease enter user name:"<<endl;
gets(username);
cout<<"\nPlease enter user ID:"<<endl;
gets(userpwd);
fprintf (pFile, "\n%s %s",username,userpwd);
rewind (pFile);
fclose (pFile);
cout<<"\nNew user account has been created .\n"<<endl;
cout<<"\nYou may now use your new account to login ."<<endl;
}
void admin()
{
FILE * pFile;
pFile = fopen ("admin.dat","a+" ;
cout<<" lease enter user name:"<<endl;
gets(ausername);
gets(ausername);
cout<<"\nPlease enter user ID:"<<endl;
gets(auserpwd);
fprintf (pFile, "\n%s %s",ausername,auserpwd);
rewind (pFile);
fclose (pFile);
cout<<"\nNew user account has been created .\n"<<endl;
cout<<"\nYou may now use your new account to login ."<<endl;
}
void loginmenu1()
{
a: cout<<" lease select a task .\n"<<endl;
cout<<"1.create admin ."<<endl;
cout<<"2.create stuff"<<endl;
cout<<"3.exit"<<endl;
cin>>task2;
switch(task2)
{
case '1':
admin();
break ;
case '2':
admin1();
break ;
case '3':
exit(1);
break ;
default :
cout<<"Invalid selection please select again ."<<endl;
goto a;
break ;
}
}
void login()
{
bool login;
system("color bc" ;
//open file
ifstream fsdbfile( "user.dat", ios::in || ios: ut);
if ( !fsdbfile )
{
cerr << "File could not be opened" << endl;
exit (1);
}
//login part
do
{
cout << " Please Insert your staff ID : ";
cin >> staffid;
cout << " Please Insert your password : ";
cin >> staffpwd;
fsdbfile >> userid >> userpwd;
while ( !fsdbfile.eof() )
{
if ( userid == staffid )
{
break;
}
fsdbfile >> userid >> userpwd;
}
if (strcmp(userpwd,staffpwd) != 0)
{
system ("cls" ;
cout << "Invalid Login!!\n";
fsdbfile.clear();
fsdbfile.seekg(0);
}
else
{
menu1();
}
}while(login != true);
}
void loginmenu()
{
a: cout<<" lease select a task .\n"<<endl;
cout<<"1.login as admin ."<<endl;
cout<<"2.login as staff"<<endl;
cout<<"3.exit"<<endl;
cin>>task1;
switch(task1)
{
case '1':
adminlogin();
break ;
case '2':
login();
case 'E':
;
break ;
default :
cout<<"Invalid selection please select again ."<<endl;
goto a;
break ;
}
}
main()
{
system("color F0" ;
loginmenu();
return 0;
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-8-2008 10:23 PM
|
显示全部楼层
在我 create 一个 user 之后 ...
能用那个 user login ...
but 如果 create second user 之后 ...
first user 就不能用了 ...
有什么办法可以让他从头 compare 到尾 ??? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 6-8-2008 10:25 PM
|
显示全部楼层
请用这个 code 来 create admin user ...
之后再用第一个 code 来 login ...
#include <iostream>
#include <cmath>
#include <iomanip>
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <string>
#include <cstdlib>
#include <fstream>
using std::ifstream;
using namespace std;
char staffname[50],staffpwd[50],blank[50],username[50],userpwd[50],userid[10],staffid[10];
char astaffname[50],astaffpwd[50],ausername[50],auserpwd[50],auserid[10],astaffid[10];
using namespace std;
void loginmenu1();
int lic,day,B,E;
double pay1,peak,dr,mc;
char buffer [80],cname[30],season1,code1,code2,car_type[20],task,task1,task2;
void menu1()
{
cout << "\aWelcome, " << staffname << " [" << staffid << "] :\n\n"<<endl;
a: cout<<" lease select a task .\n"<<endl;
cout<<"[N]ew Rental ."<<endl;
cout<<"[E]xit ."<<endl;
cin>>task;
switch(task)
{
case 'N':
break ;
case 'E':
exit(1) ;
break ;
default :
cout<<"Invalid selection please select again ."<<endl;
goto a;
break ;
}
}
void adminlogin()
{
bool login;
system("color bc" ;
//open file
ifstream fsdbfile( "admin.dat", ios::in || ios: ut);
if ( !fsdbfile )
{
cerr << "File could not be opened" << endl;
exit (1);
}
//login part
do
{
cout << " Please Insert your staff ID : ";
cin >> astaffid;
cout << " Please Insert your password : ";
cin >> astaffpwd;
fsdbfile >> auserid >> auserpwd;
while ( !fsdbfile.eof() )
{
if ( auserid == astaffid )
{
break;
}
fsdbfile >> auserid >> auserpwd;
}
if (strcmp(auserpwd,astaffpwd) != 0)
{
system ("cls" ;
cout << "Invalid Login!!\n";
fsdbfile.clear();
fsdbfile.seekg(0);
}
else
{
login = true;
loginmenu1();
}
}while(login != true);
}
void admin1()
{
FILE * pFile;
pFile = fopen ("user.dat","a+" ;
cout<<" lease enter user name:"<<endl;
gets(username);
cout<<"\nPlease enter user ID:"<<endl;
gets(userpwd);
fprintf (pFile, "\n%s %s",username,userpwd);
rewind (pFile);
fclose (pFile);
cout<<"\nNew user account has been created .\n"<<endl;
cout<<"\nYou may now use your new account to login ."<<endl;
}
void admin()
{
FILE * pFile;
pFile = fopen ("admin.dat","a+" ;
cout<<" lease enter user name:"<<endl;
gets(ausername);
gets(ausername);
cout<<"\nPlease enter user ID:"<<endl;
gets(auserpwd);
fprintf (pFile, "\n%s %s",ausername,auserpwd);
rewind (pFile);
fclose (pFile);
cout<<"\nNew user account has been created .\n"<<endl;
cout<<"\nYou may now use your new account to login ."<<endl;
}
void loginmenu1()
{
a: cout<<" lease select a task .\n"<<endl;
cout<<"1.create admin ."<<endl;
cout<<"2.create stuff"<<endl;
cout<<"3.exit"<<endl;
cin>>task2;
switch(task2)
{
case '1':
admin();
break ;
case '2':
admin1();
break ;
case '3':
exit(1);
break ;
default :
cout<<"Invalid selection please select again ."<<endl;
goto a;
break ;
}
}
void login()
{
bool login;
system("color bc" ;
//open file
ifstream fsdbfile( "user.dat", ios::in || ios: ut);
if ( !fsdbfile )
{
cerr << "File could not be opened" << endl;
exit (1);
}
//login part
do
{
cout << " Please Insert your staff ID : ";
cin >> staffid;
cout << " Please Insert your password : ";
cin >> staffpwd;
fsdbfile >> userid >> userpwd;
while ( !fsdbfile.eof() )
{
if ( userid == staffid )
{
break;
}
fsdbfile >> userid >> userpwd;
}
if (strcmp(userpwd,staffpwd) != 0)
{
system ("cls" ;
cout << "Invalid Login!!\n";
fsdbfile.clear();
fsdbfile.seekg(0);
}
else
{
menu1();
}
}while(login != true);
}
void loginmenu()
{
a: cout<<" lease select a task .\n"<<endl;
cout<<"1.login as admin ."<<endl;
cout<<"2.login as staff"<<endl;
cout<<"3.exit"<<endl;
cin>>task1;
switch(task1)
{
case '1':
admin();
break ;
case '2':
admin1();
case 'E':
;
break ;
default :
cout<<"Invalid selection please select again ."<<endl;
goto a;
break ;
}
}
main()
{
system("color F0" ;
loginmenu();
return 0;
} |
|
|
|
|
|
|
|
|
|
|
发表于 7-8-2008 01:29 PM
|
显示全部楼层
使用 struct.
struct user_struct
{
char userID[max_id_length]; //max_id_length 是你所允许的用户名字最大值
char password[max_pwd_length];
};
当你写入用户资料时,将整个 struct 写进档案的末端(也就是说你的用户资料库是以这个struct为资料单位的)。当你要检查用户资料时,则是从头开始逐个的读取用户资料,一次读取一个用户的struct,再比较用户名字和密码。就这样。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 7-8-2008 08:41 PM
|
显示全部楼层
原帖由 geekman 于 7-8-2008 01:29 PM 发表 
使用 struct.
struct user_struct
{
char userID[max_id_length]; //max_id_length 是你所允许的用户名字最大值
char password[max_pwd_length];
};
当你写入用户资料时,将整个 struct 写进档案的末 ...
谢谢 ...
可惜偶不怎么会用 struct ...
偶已经 solve 掉偶的问题了 ...
谢谢 ... |
|
|
|
|
|
|
|
|
|
|
发表于 8-8-2008 04:28 PM
|
显示全部楼层
虽然你已经解决了问题,但是学习以下struct也不错,将来用得着。
structure:
declardation: (keyword)struct [structure tag] { [structure members...]};
struct 可以理解为一个可以包含各种不同data type,甚至 function (for C++) 的 array。 你可以在struct里面放入任何数量的任何一种数据:
struct my_struct
{
int myInteger;
string mystring;
void mystructfunction(void)
{
...//function code
}
};
使用struct的好处是你能够把各种不同的数据group在一起,然后把这些数据当作一个独立的数据来处理。
要使用struct 里面的数据,你可以使用 dot operator (。)
例如当你要是用上面的那个例子里面的myinteger 你可以:
my_struct exampleStruct;
exampleStruct.myinteger = 100;
如果你设计了一个User_Struct:
struct user_struct
{
char user_name[50];
char user_pwd[20];
int user_age;
char user_sex;
}
user_struc user_account;
这样,当你写入资料时就可以:
filestream.write(user_account, sizeof(user_struct));
而不必逐个项目去写入。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 9-8-2008 03:36 PM
|
显示全部楼层
原帖由 geekman 于 8-8-2008 04:28 PM 发表 
虽然你已经解决了问题,但是学习以下struct也不错,将来用得着。
structure:
declardation: (keyword)struct [structure tag] { [structure members...]};
struct 可以理解为一个可以包含各种不同data type ...
直接 use sturct 很方便下 ...
请问 struct 需要 include s0mok /??? |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|