C++问题,编译无错,运行老是停止工作,求解答
源码:#include<iostream>#include<fstream>#include<string>usingnamespacestd;boolLogin(cha...
源码:
#include<iostream>#include<fstream>#include<string>using namespace std;bool Login(char str1[20], char str2[20]) { char* my_username[20]; char* my_password[20]; int j=1; int i; ifstream input; input.open("username.txt"); for(i=0;i<4;i++) { input>>my_username[i]>>my_password[i]; } input.close(); for(i=0;i<4;i++) { if(strcmp(str1,my_username[i])==0 && strcmp(str2,my_password[i])==0 ) { return true; } } if(j==3) { cout<<"你已经试了3次,程序自动退出."<<endl; exit(0); } j++; return false; }int main(){ char username[20],password[20]; begin: cout<<"请输入你的帐号:"<<endl; cin>>username; cout<<"请输入你的密码:"<<endl; cin>>password; if(Login(username,password)) { cout<<"欢迎使用职工工资管理系统"<<endl; } else { cout<<"用户名和密码输入错误,请重新输入."<<endl; goto begin; } return 0;}
执行使:
源码中的文件内容: 展开
#include<iostream>#include<fstream>#include<string>using namespace std;bool Login(char str1[20], char str2[20]) { char* my_username[20]; char* my_password[20]; int j=1; int i; ifstream input; input.open("username.txt"); for(i=0;i<4;i++) { input>>my_username[i]>>my_password[i]; } input.close(); for(i=0;i<4;i++) { if(strcmp(str1,my_username[i])==0 && strcmp(str2,my_password[i])==0 ) { return true; } } if(j==3) { cout<<"你已经试了3次,程序自动退出."<<endl; exit(0); } j++; return false; }int main(){ char username[20],password[20]; begin: cout<<"请输入你的帐号:"<<endl; cin>>username; cout<<"请输入你的密码:"<<endl; cin>>password; if(Login(username,password)) { cout<<"欢迎使用职工工资管理系统"<<endl; } else { cout<<"用户名和密码输入错误,请重新输入."<<endl; goto begin; } return 0;}
执行使:
源码中的文件内容: 展开
1个回答
展开全部
原因是你的Login函数中的数组是指针数组,修改一下即可,修改后代码如下,修改位置注释给出:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>////////////strcmp()
#include <cstdlib>/////////////exit(0)
using namespace std;
bool Login(char str1[20], char str2[20])
{
char my_username[4][20];///////////////
char my_password[4][20];
static int j=1;//////////////////静态变量
int i;
ifstream input;
input.open("a.txt");
for(i=0;i<4;i++)
{
input>>my_username[i]>>my_password[i];
}
input.close();
for(i=0;i<4;i++)
{
if(strcmp(str1,my_username[i])==0 && strcmp(str2,my_password[i])==0 )
{
j = 1;///////////////重新初始化次数
return true;
}
}
if(j==3)
{
cout<<"你已经试了3次,程序自动退出."<<endl;
exit(0);
}
j++;
return false;
}
int main()
{
char username[20],password[20];
begin: cout<<"请输入你的帐号:"<<endl;
cin>>username;
cout<<"请输入你的密码:"<<endl;
cin>>password;
if(Login(username,password))
{
cout<<"欢迎使用职工工资管理系统"<<endl;
}
else
{
cout<<"用户名和密码输入错误,请重新输入."<<endl;
goto begin;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询