Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在我的应用程序中注册多个用户时出现问题 我创建了这个程序,它是一个简单的C++登录和注册程序。它工作正常,但有一个小问题,它只适用于1个用户。当我注册一个帐户并尝试登录时,它工作正常,但当我创建第二个帐户并尝试登录时,第二个帐户不登录。它将只登录已存储的第一个帐户。在数据文件中,它存储第二个帐户的所有数据,如密码和用户名,但不登录。有人能帮我解决这个问题吗_C++ - Fatal编程技术网

在我的应用程序中注册多个用户时出现问题 我创建了这个程序,它是一个简单的C++登录和注册程序。它工作正常,但有一个小问题,它只适用于1个用户。当我注册一个帐户并尝试登录时,它工作正常,但当我创建第二个帐户并尝试登录时,第二个帐户不登录。它将只登录已存储的第一个帐户。在数据文件中,它存储第二个帐户的所有数据,如密码和用户名,但不登录。有人能帮我解决这个问题吗

在我的应用程序中注册多个用户时出现问题 我创建了这个程序,它是一个简单的C++登录和注册程序。它工作正常,但有一个小问题,它只适用于1个用户。当我注册一个帐户并尝试登录时,它工作正常,但当我创建第二个帐户并尝试登录时,第二个帐户不登录。它将只登录已存储的第一个帐户。在数据文件中,它存储第二个帐户的所有数据,如密码和用户名,但不登录。有人能帮我解决这个问题吗,c++,C++,这是我的密码: #include<iostream> #include<fstream> #include<windows.h> #include<stdlib.h> using namespace std; int main() { string Register_Name, Register_Password,Name,Password,Check_Name, Check_Password; Menue: system("co

这是我的密码:

#include<iostream>
#include<fstream>
#include<windows.h>
#include<stdlib.h>
using namespace std;
int main()
{
string Register_Name, Register_Password,Name,Password,Check_Name, Check_Password;
Menue:
    system("color 1f");
    system("cls");
    string select;
    cout<<"Enter 1 for Create account\n";
    cout<<"\nEnter 2  for Login account\n";
    cout<<"\nEnter 3  for Exite\n\n";
    cin>>select;
    if(select=="1")
    {
        system("cls");
        system("color 2f");
        cout<<"\t\t\t\t\t\tCreate Account";
        std::ofstream create_account;
        create_account.open("data.txt",ios::out|std::ios::app);
        cout<<"\n\n\n"<<"New Username: "; 
        cin>>Register_Name;
        cout<<"\nNew Password: ";
        cin>>Register_Password;
        create_account<<"Name is         "<<Register_Name<<'\n';
        create_account<<"password is\t"<<Register_Password<<'\n'<<'\n';
        create_account.close();
        system("cls");
        system("color 3f");
        cout<<"\t\t\t\t\t\tAccount Created";
        Sleep(5*400);
        goto Menue;
    }
    else
    {
        if(select=="2")
        {
            Login:
                system("cls");
                system("color 4f");
                cout<<"\t\t\t\t\t\tLogin";
                std::ifstream Login;
                Login.open("data.txt",ios::out|std::ios::app);
                Login.ignore(16,'\n');
                getline(Login,Check_Name, '\n');
                Login.ignore(11,'\t');
                getline(Login,Check_Password, '\n');
                Login.close();
                cout<<"\n\n\n"<<"Enter Username: ";
                cin>> Name;
                cout<<"Enter Password: ";
                cin>> Password;
                if (Name==Check_Name && Password==Check_Password)
                {
                    system("cls");
                    system("color 5f");
                    cout<<"\t\t\t\t\t\tLogin Successful\n"<<"\n\n"<<"Welcome, "<<Name<<"\n";
                    system("pause");
                    goto Menue;
                }
                else
                {
                    system("color 6f");
                    system("cls");
                    cout<<"\t\t\t\t\t\tincorrect name or password\n";
                    Sleep(5*400);
                    goto Login;
                }
        }
        else
        {
            if(select=="3")
            {
                system("color 8f");
                system("cls");
                cout<<"\n\n\n\n\n\n\n\t\t\t\t\t\tGood Bye";
                Sleep(5*400);
                return 0;
            }
            else
            {
                system("color 9f");
                system("cls");
                cout<<"\t\t\t\t\t\tInvalid Selection";
                Sleep(5*400);
                goto Menue;
            }
        }
    }
}

您的登录仅适用于第一个帐户,因为您仅从输入文件中读取第一个帐户。你根本没有阅读后续账户

从文件中读取第一个帐户,然后关闭文件,然后将用户输入与读取值进行比较。如果用户没有输入匹配的凭据,您将返回以重新打开输入文件,仅读取第一个帐户,关闭文件,并反复比较用户的输入,直到用户最终在文件中输入与第一个帐户匹配的凭据

相反,您需要提示用户输入他们的凭据,然后在整个文件中循环,比较文件中的每个帐户,直到找到匹配项或达到EOF。然后,您可以告诉用户结果并相应地采取行动

另外,另一方面,当您打开输入文件时,您指定的是不属于那里的ios::out和std::ios::app标志。您应该仅将这些标志用于输入,而不用于输入

话虽如此,将单独的操作分解为单独的函数将非常有帮助。这将真正清理代码,使其更具可读性和可管理性

尝试类似以下内容:

包括 包括 包括 包括 包括 包括 真空透明屏 { std::systemcls; } void SetScreenColorconst标准::字符串和值 { std::string cmd=颜色+值; std::systemcmd.c_str; } void SkipLinestd::istream和in { in.ignorested::numeric_limits::max,“\n”; } 无效WaitSecondsWord秒 { 睡眠秒*1000; } void DisplayMsgconst std::string和color,const std::string和message { 透明屏幕; 设置屏幕颜色;
std::cout@Adeelzaman786您已经知道如何运行循环,尽管您真的不应该使用goto!并且您已经知道如何读取文件的前两行。那么,是什么阻止您只需添加另一个循环来读取前两行,然后再读取下两行,依此类推?请至少自己尝试解决这个问题我要求有人为你写代码