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;}
执行使:

源码中的文件内容:
展开
 我来答
wchyumo2011
推荐于2016-05-31 · TA获得超过2万个赞
知道大有可为答主
回答量:5810
采纳率:79%
帮助的人:2711万
展开全部

原因是你的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;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式