需要c ++注册和登录程序帮助

时间:2016-12-04 19:49:35

标签: c++ logging

好的,所以我得到了两个错误。

  1. 无效指针添加[在(用户名+" .txt"行)]
  2. function getline应该有一个原型。
  3. 这是代码,我正在进行注册/注册/签到表单。稍后将添加更多功能,例如密码的****格式和密码的限制。截至目前,我已经被困在这里并且不知道如何继续。此外,我被迫使用turbo c ++因为学校:)))))。 请为MacOSX建议一些好的编译器。 谢谢!

        #include<iostream.h>
        #include<fstream.h>
        #include<conio.h>
        #include<stdio.h>
        #include<string.h>
    
        void register_user();
        void login_user();
        void main_menu();
    
        int IsLoggedIn()
        {
            char username[20],password[20],un[20],pw[20];
            cout<<"Enter Username: ";gets(username);
            cout<<"Enter Password: ";gets(password);
    
            ifstream read(username + ".txt");
            getline(un,read);
            getline(pw,read);
    
            if(un==username && pw==password)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    
        void main()
        {
            main_menu();
        }
    
        void main_menu()
        {
            int choice;
            cout<<"1. Register\n2. Login\nYour Choice: "; cin>>choice;
            switch(choice)
            {
                case 1: register_user(); break;
                case 2: login_user(); break;
                default: break;
            }
        }
    
        void register_user()
        {
            char username[20], password1[20],password2[20];
            cout<<"Enter Username: ";gets(username);
            rev1:cout<<"Enter Password: ";gets(password1);
            cout<<"Enter Password again: ";gets(password2);
            while (password1!=password2)
            {
                goto rev1;
            }
            ofstream file;
            file.open(username + ".txt");
            file<<username<<endl<<password1;
            file.close();
        }
    
        void login_user()
        {
            int chk=IsLoggedIn();
            if(chk==1)
            {
                cout<<"Log in successfull!\n";
            }
            else
            {
                cout<<"Log in unsucessfull!\n";
            }
        }
    

1 个答案:

答案 0 :(得分:1)

您不能对C-Style字符串使用运算符'+'。

您需要使用strcat()snprintf

此外,使用fscanffreadfgets来读取字符数组。

您不能在C-Style字符串(字符数组)上使用运算符==,请使用strcmpoperator ==将比较指针中的值,而不是指向的文本。

如果您打算使用C-Style字符串,我强烈建议您查看str*()系列函数。