C++问题,编译无错,运行老是停止工作,求解答

2025-04-25 06:19:31
推荐回答(1个)
回答1:

原因是你的Login函数中的数组是指针数组,修改一下即可,修改后代码如下,修改位置注释给出:

#include 
#include 
#include 
#include ////////////strcmp()
#include /////////////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次,程序自动退出."<        exit(0);
    }
    j++;
    return false;
}
int main()
{
    char username[20],password[20];
    begin:  cout<<"请输入你的帐号:"<    cin>>username;
    cout<<"请输入你的密码:"<    cin>>password;
    if(Login(username,password))
    {
        cout<<"欢迎使用职工工资管理系统"<    }
    else
    {
        cout<<"用户名和密码输入错误,请重新输入."<        goto begin;
    }
    return 0;
}