Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
OOP文件读取问题 我在用C++做OOP项目时遇到了问题。我在下面提到了代码。我得到的坏结果是: 如果我列出添加了一些联系人的现有文件,它将正确列出文件的所有内容。但是如果我尝试以管理员身份登录来添加新联系人(pass=12345,如果您只需要),现在,如果我尝试打开添加新联系人的文件。问题就出现在这里 姓名应再次列为姓名:Samir Chapp 地址应该列为地址:尼泊尔 以此类推,但它跳过了这一行,并在地址处显示名称为address:Samir chap_C++ - Fatal编程技术网

OOP文件读取问题 我在用C++做OOP项目时遇到了问题。我在下面提到了代码。我得到的坏结果是: 如果我列出添加了一些联系人的现有文件,它将正确列出文件的所有内容。但是如果我尝试以管理员身份登录来添加新联系人(pass=12345,如果您只需要),现在,如果我尝试打开添加新联系人的文件。问题就出现在这里 姓名应再次列为姓名:Samir Chapp 地址应该列为地址:尼泊尔 以此类推,但它跳过了这一行,并在地址处显示名称为address:Samir chap

OOP文件读取问题 我在用C++做OOP项目时遇到了问题。我在下面提到了代码。我得到的坏结果是: 如果我列出添加了一些联系人的现有文件,它将正确列出文件的所有内容。但是如果我尝试以管理员身份登录来添加新联系人(pass=12345,如果您只需要),现在,如果我尝试打开添加新联系人的文件。问题就出现在这里 姓名应再次列为姓名:Samir Chapp 地址应该列为地址:尼泊尔 以此类推,但它跳过了这一行,并在地址处显示名称为address:Samir chap,c++,C++,如何克服它??带我出去。我是一名编码初学者。 提前谢谢 源代码 #include <cstring> #include <fstream> #include <iostream> using namespace std; class Contact { public: string details[5] = {"Name", "Phone(Mobile)", "Address", "

如何克服它??带我出去。我是一名编码初学者。
提前谢谢

源代码

#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
class Contact
{
public:
    string details[5] = {"Name", "Phone(Mobile)", "Address", "Email", ""};
    void adding()
    {
        ofstream fout;
        fout.open("ContactBook.txt", ios::app);
        char inpt[30];
        fout << "\n";
        for (int i = 0; i < 4; i++)
        {
            cin.getline(inpt, 30);
            cout << details[i] << ": ";
            fout << inpt << "\n";
        }
        cin.getline(inpt, 30);
        fout << inpt;
    }
    void listing()
    {
        ifstream fin;
        fin.open("ContactBook.txt", ios::in);
        char inpt[30];
        int i = 0;
        while (fin)
        {
            fin.getline(inpt, 30);
            if (i != 4)
                cout << details[i] << " : " << inpt << endl;
            else
                cout << details[i] << inpt << endl;
            i++;
            if (i == 5)
                i = 0;
        }
    }
    void searching()
    {
        string name;
        cout << "Enter name whose contact is to be searched: ";
        getline(cin >> ws, name);
        ifstream fin;
        fin.open("ContactBook.txt", ios::in);
        string inpt;
        int c = 0;
        while (fin)
        {
            getline(fin >> ws, inpt);
            if (inpt == name)
            {
                c++;
                for (int i = 0; i < 4; i++)
                {
                    cout << details[i] << ": " << inpt << endl;
                    getline(fin >> ws, inpt);
                }
            }
            if (c > 0)
                break;
        }
    }
    void editing()
    {
        string name;
        cout << "Enter name whose contact is to be Edited: ";
        getline(cin >> ws, name);
        ifstream fin;
        ofstream fout;
        fin.open("ContactBook.txt", ios::in);
        fout.open("ContactBookTemporary.txt", ios::out | ios::trunc);
        string inpt;
        int b = 0;
        int c = 0;
        while (!fin.eof())
        {
            getline(fin >> ws, inpt);
            b++;
            if (inpt == name)
            {
                c++;
                cout << "Enter new details: \n";
                for (int i = 0; i < 4; i++)
                {
                    string newdetail;
                    cout << "New " << details[i] << ": ";
                    getline(cin >> ws, newdetail);
                    b++;
                    fout << newdetail << "\n";
                }
                for (int i = 0; i <= 3; i++, b++)
                    getline(fin >> ws, inpt);
                fout << "\n"
                     << inpt << "\n";
            }
            else
            {
                if (fin.eof() != 1)
                    fout << inpt << "\n";
                else
                    fout << inpt;
                if (b % 4 == 0 && fin.eof() != 1)
                    fout << "\n";
            }
        }
    }
    void deleting()
    {
        string name;
        cout << "Enter name whose contact is to be Deleted: ";
        getline(cin >> ws, name);
        ifstream fin;
        ofstream fout;
        fin.open("ContactBook.txt", ios::in);
        fout.open("ContactBookTemporary.txt", ios::out | ios::trunc);
        string inpt;
        int b = 0;
        int c = 0;
        while (!fin.eof())
        {
            getline(fin >> ws, inpt);
            b++;
            if (inpt == name)
                c++;
            else
            {
                if (c >= 1 && c <= 3)
                    c++;
                else
                {
                    if (fin.eof() != 1)
                        fout << inpt << "\n";
                    else
                        fout << inpt;
                    if (b % 4 == 0 && fin.eof() != 1)
                        fout << "\n";
                }
            }
        }
    }
    void temporary_to_original()
    {
        ofstream fout1;
        ifstream fin1;
        fout1.open("ContactBook.txt", ios::out | ios::trunc);
        fin1.open("ContactBookTemporary.txt", ios::in);
        char reading[30];
        while (!fin1.eof())
        {
            fin1.getline(reading, 30);
            if (fin1.eof() == 0)
                fout1 << reading << "\n";
            else
                fout1 << reading;
        }
    }
    void exiting()
    {
        cout << "\n\nThank You For Using Contact Management System\n\n";
        exit(0);
    }
};

int main()
{
    Contact object;
    cout << "\n\t\t\t\t\t**** Welcome to Contacts Manager Application ****" << endl;
    cout << "INSTRUCTION: YOU CAN ONLY VIEW/SEARCH FOR A CONTACT IF YOU ARE A USER. TO EDIT, DELETE AN EXISTING CONTACT OR ADD ANY NEW CONTACT, SIGN IN AS ADMINISTRATOR." << endl;
block:
    cout << "\nSelect your identity(1/2)\n[1]USER\n[2]ADMIN\nEnter the choice:";
    int identity;
    int flag = 0;
    int flag1 = 0;
    cin >> identity;
    if (identity == 2)
    {
        char pass[5];
        cout << "\n\nEnter Admin 5 digits Passcode: ";
        for (int i = 0; i < 5; i++)
        {
            cin >> pass[i];
        }
        ifstream fin_pass("password.txt");
        char corr_pass;
        for (int i = 0; i < 5; i++)
        {
            fin_pass.get(corr_pass);
            if (pass[i] != corr_pass)
                flag1++;
        }

        if (flag1 == 0)
            cout << "\n\nWELCOME ADMIN\n\n";
        else
        {
            identity = 1;
            cout << "WRONG PASSCODE, YOU ARE SIGNED IN AS USER ONLY\n\n\nWELCOME USER\n\n";
        }
    }
    else
        cout << "\n\nWELCOME USER\n\n";
    while (1)
    {
        cout << "\n\n\t\t\t\t\t\tMAIN MENU\n\t\t\t\t\t=====================\n\t\t\t\t\t[1] Add a new Contact\n\t\t\t\t\t[2] List all Contacts\n\t\t\t\t\t[3] Search for contact\n\t\t\t\t\t[4] Edit a Contact\n\t\t\t\t\t[5] Delete a Contact\n\t\t\t\t\t[0] Exit\n\t\t\t\t\t=================\n\t\t";
        cout << "\t\t\tEnter the choice:";
        int op;
        cin >> op;
        switch (op)
        {
        case 1:
        {
            if (identity == 1)
            {
                cout << "\n\nSign in as Admin to add a new contact.\n\n";
                flag++;
            }
            else
            {
                object.adding();
                cout << "\nSuccessfully Added.\n";
            }
            break;
        }
        case 2:
        {
            object.listing();
            break;
        }
        case 3:
        {
            object.searching();
            break;
        }
        case 4:
        {
            if (identity == 1)
            {
                cout << "\n\nSign in as Admin to add a new contact.\n\n";
                flag++;
            }
            else
            {
                object.editing();
                object.temporary_to_original();
                cout << "\nSuccessfully Edited.\n";
            }
            break;
        }
        case 5:
        {
            if (identity == 1)
            {
                cout << "\n\nSign in as Admin to add a new contact.\n\n";
                flag++;
            }
            else
            {
                object.deleting();
                object.temporary_to_original();
                cout << "\nSuccessfully Deleted.\n";
            }
            break;
        }
        case 0:
        {
            object.exiting();
        }
        default:
            cout << "\n\nInvalid choice. Try again\n\n";
        }
        if (flag == 1)
        {
            flag = 0;
            goto block;
        }
    }
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
班级联系
{
公众:
字符串详细信息[5]={“姓名”、“电话(手机)”、“地址”、“电子邮件”、“};
无效添加()
{
流式流量计;
打开(“ContactBook.txt”,ios::app);
字符输入[30];

fout问题来自您输入密码的方式

您可以使用以下代码:

        for (int i = 0; i < 5; i++)
        {
            cin >> pass[i];
        }
发生的第一件事是第一个
cin。getline
将返回空输入,因为输入流中剩下的下一件事是读取密码时留下的换行符

为了解决这个问题,我建议您像在其他地方一样,使用getline命令读取密码

编辑:进一步澄清 这是一个可怕的模式:

getline(cin >> ws, name);
当您将字符串读入变量时,控制台将等待您按enter键,然后开始处理流。不过,换行符保留在流中

修理 我强烈建议您编写用户输入函数,并在任何地方使用它们

string getUserEnteredString();
int getUserEnteredInt();

这里是实现。(两者都一样。稍后,您将了解C++模板只需要写一次,但对您来说,既然您是初学者,我将复制它。) 然后,无论你在哪里都有这样的模式:

string name;
cout << "Prompt: ";
getline(cin >> ws, name);

它不是完美的,但更好。

必读:;我有一个密码文件。这不是问题。我进入管理并添加联系人。问题是当我在添加后在文件中列出联系人时。它在保存文件后生成一个空行,但在编码运行时,它不会生成空行。这是主要的p我面临的问题。如果您不相信我,请在添加后在文本编辑器中检查该文件。此外,您可以在文本编辑器中手动编辑该文件,然后再列出。您是否仍能满足我的要求?请尝试使用getline输入密码,看看问题是否消失。您还可以使用调试器观察该文件是否返回空字符串首先
cin.getline
添加
功能中为我提供要更改的代码。我一次又一次地在代码中漫游,但找不到如何调试。您使用的用户流输入,无论是从控制台还是从文件中输入,在您应用它的大多数地方都是错误的或糟糕的。我修改了我的答案以提供更多指导。简短回答:编写简单的用户输入函数,然后正确地实现它们。如果您不喜欢神奇的
1000
,您可以编写:
cin.ignore(numeric_limits::max(),'\n')
,但我正试图让答案适合您的技能水平。
string getUserEnteredString() {
    string result;
    cin >> result;
    cin.ignore(1000, '\n');
    return result;
}

int getUserEnteredInt() {
    int result;
    cin >> result;
    cin.ignore(1000, '\n');
    return result;
}
string name;
cout << "Prompt: ";
getline(cin >> ws, name);
cout << "Prompt: ";
string name = getUserEnteredName();
cout << "\t\t\tEnter the choice:";
int op;
cin >> op;
cout << "\t\t\tEnter the choice:";
int op = getUserEnteredInt();
cout << "\n\nEnter Admin 5 digits Passcode: ";
string pass = getUserEnteredString();
ifstream fin_pass("password.txt");
string corr_pass;
getline(fin_pass, corr_pass);
if (pass != corr_pass) flag1 = 1;