提问者:小点点

OOP文件读取问题


我在用C++做oop项目时遇到了一个问题。我在下面提到了代码。我得到的坏结果是:

  • 如果我列出了我添加了一些联系人的现有文件,它将正确地列出文件的所有内容。但是如果我试图通过以管理员身份登录来添加一个新联系人(密码=12345,如果您需要的话),并且现在,如果我试图打开我已经添加了新联系人的文件。问题就出现在这里。
  • 名字应该列为:Samir Chapagain
  • 地址应该列为Adress:Nepal
    等等,但它跳过了这一行,并将地址中的姓名显示为Adress:Samir Chapagain

如何克服它??带我出去。我是编码的初学者。
谢谢!!。

源代码

#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;
}

共2个答案

匿名用户

问题来自于你输入密码的方式。

您使用以下代码:

        for (int i = 0; i < 5; i++)
        {
            cin >> pass[i];
        }

这将读取12345,但不会吸收按enter键提交的换行。它仍然留在输入流中。

因此,如果您下次在添加功能中添加联系人,您会遇到此代码。。。

        for (int i = 0; i < 4; i++)
        {
            cin.getline(inpt, 30);
            cout << details[i] << ": ";
            fout << inpt << "\n";
        }

发生的第一件事是第一个cin.getLine将返回空输入,因为输入流中剩下的下一件事是读取passcode时留下的换行符。

为了修复,我建议您使用getline命令读取passcode,就像您在其他地方所做的那样。

匿名用户

这个程序中的空格有什么用