HEIMIIY 2018-06-11 17:07 采纳率: 0%
浏览 1129
已采纳

为什用户名一直显示错误

class Account

{
public:
string name,pw;
int i;
const string map[3][2];

Account()
{
    const string map[3][2] = {
    {"user1", "123455"}, 
    {"user2", "asdfasdf"},
    {"user3", "sb"}
 };
}
void Login()
{
while(cin >> name >> pw)
{
    for(i = 0; i < 3; i ++)
    {
        if(map[i][0] == name)break;
    }
    if(i == 3) cout << "用户名输入错误,请重新输入\n";
    else if(map[i][1] != pw)
        cout << "密码输入错误,请重新输入\n";
    else break;

}
cout << "欢迎进入金拱门管理系统"<<"-----"<<name << endl;

}

void Goout()
{
    while(cin >> name>>pw)

{
for(i = 0; i < 3; i ++)
{
if(map[i][0] == name)break;
}
if(i == 3) cout << "用户名输入错误,不能退出系统\n";
else if(map[i][1] != pw)
cout << "密码输入错误,不能退出系统\n";
else break;

}
cout<<"欢迎下次登陆金拱门管理系统"<<"-----"<<name<< endl;
}
};

  • 写回答

4条回答 默认 最新

  • threenewbee 2018-06-11 17:31
    关注

    你在Account()里面又定义了一个map,此map非彼map,作为成员变量的map还是没有初始化。

    正确的代码如下:

     // Q691800.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <iostream>
    #include <string>
    using namespace std;
    class Account
    {
        public:
        string name,pw;
        int i;
        string map[3][2];
    
    
        Account()
        {
            map[0][0] = "user1";
            map[1][0] = "user2";
            map[2][0] = "user3";
            map[0][1] = "123455";
            map[1][1] = "asdfasdf";
            map[2][1] = "sb";
        }
    
        void Login()
        {
        while(cin >> name >> pw)
        {
            for(i = 0; i < 3; i ++)
            {
                if(map[i][0] == name)break;
            }
            if(i == 3) cout << "用户名输入错误,请重新输入\n";
            else if(map[i][1] != pw)
                cout << "密码输入错误,请重新输入\n";
            else break;
    
        }
        cout << "欢迎进入金拱门管理系统"<<"-----"<<name << endl;
       }
    
        void Goout()
        {
            while(cin >> name>>pw)
       {
        for(i = 0; i < 3; i ++)
            {
                if(map[i][0] == name)break;
            }
            if(i == 3) cout << "用户名输入错误,不能退出系统\n";
            else if(map[i][1] != pw)
                cout << "密码输入错误,不能退出系统\n";
            else break;
    
       }
       cout<<"欢迎下次登陆金拱门管理系统"<<"-----"<<name<< endl;
       }
       };
    
    int main()
    {
        Account acc;
        acc.Login();
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP