簡體   English   中英

如何檢查從鍵盤輸入的特定 integer 值是否存在於 C++ 文件的一行或多行中

[英]How to check if a specific integer value input from keyboard exist in a line or more lines of a file in C++

我有一個 C++ 課程的小項目,我一直在嘗試檢查文件中是否存在學生的 class 的數據成員的值(“ID”)。 我嘗試使用我在互聯網上找到的一些 function 將我正在搜索的 integer 值轉換為字符串,然后在文件的每一行中使用 find ZC1C425268E68385D1AB5074C17A94F1 來搜索它。

它可以工作,但是每當我檢查文件中的一行時,它都會得到錯誤的結果,因為 ID 值(例如“12”)與年齡的值(也是“12”)相同。 這樣做是因為年齡值在我的文件和字符串變量中的 ID 值之前(我無法更改它)。 我不知道只在字符串中搜索 ID 的值。 我使用 function "inputInfo" 從鍵盤輸入 student1 的成員值,並使用 function "checkID" 檢查文件中是否已經存在 "ID" 的值。 此外,對於項目的另一個方面,我正在尋找一種方法來搜索同一文件中出現的 ID 和名稱數據成員值(一旦它們已經被寫入)。 我認為的一個解決方案是在另一個字符出現后以某種方式開始搜索(例如空格字符,因為在文件中,每個字段都用空格與另一個字段分隔),但我不確定找到 function 能夠做到這一點。提前感謝您的幫助。以下是 C++ 中項目代碼的一部分:

#include<iostream>
#include<string>
#include<fstream>
#include <sstream>

using namespace std;

int checkID(int idNumber)
{
    string findID;
    stringstream id_string;
    id_string << idNumber;
    findID = id_string.str();
    int offset;
    ifstream in;
    in.open("Students.txt");
    if(in.is_open())
    {
        string line;
        while(getline(in, line))
        {
            if(offset = line.find(findID, 0)!= string::npos)
            {
                cout<<"The ID already exists. Insert a different ID!"<<endl;
                return 0;
            }
        }

    }
    else
        cout<<"File doesn't exist!"<<endl;
    in.close();
}

class PERSON
{
protected:
    string name;
    string surname;
    unsigned int age;
public:
    void inputinfo()
    {
        cin>>name;
        cin>>surname;
        cin>>age;
    }
    outputinfo()
    {
        cout<<name<<endl;
        cout<<surname<<endl;
        cout<<age<<endl;
    }
};

class STUDENT: public PERSON
{
    int ID;
    float marks_sum;
    string belonging_class;

public:

    inputInfo()
    {
        cout<<"Name:";
        cin>>name;
        cout<<"Surname:";
        cin>>surname;
        cout<<"Age:";
        cin>>age;
        do
        {
            cout<<"ID:";
            cin>>ID;
        }
        while (checkID(ID)==0);

        cout<<"Sum of marks:";
        cin>>marks_sum;
        cout<<"The belonging class:";
        cin>>belonging_class;

    }


    void outputInfo()
    {
        cout<<name<<endl;
        cout<<surname<<endl;
        cout<<age<<endl;
        cout<<ID<<endl;
        cout<<marks_sum<<endl;
        cout<<belonging_class<<endl;
    }

    friend std::ostream& operator << (std::ostream& os, const STUDENT& value )
    {
        os << value.name<<" "<<value.surname<<" "<<value.age<<" "<<value.ID<<" "<<value.marks_sum<<" "<<value.belonging_class<<std::endl;
        return os;
    }
};

STUDENT student1;

int writeInFile(STUDENT studentx)
{
    ofstream os("Students.txt", ofstream::app);
    os << studentx;
    os.close();
}


int main()
{
    int opt1, opt2;
    char option;

    do
    {
        cout<<"1 -  Input data into file"<<endl<<"2 - Close program"<<endl;
        cin>>opt1;
        switch(opt1)
        {
        case 1:
            do
            {
                cout<<endl;
                cout<<"Choose one of variants"<<endl<<"1.Students"<<endl<<"2.Get back to main menu"<<endl;
                cin>>opt2;
                switch(opt2)
                {
                case 1:
                    do
                    {
                        cout<<"Do you wish to introduce a new student(Y/N)?";
                        cin>>option;
                        if(option!='N')
                        {
                            student1.inputInfo();
                            writeInFile(student1);
                        }
                    }
                    while (option!='N');
                    break;
                }

            }
            while(opt2!=2);
            break;
        }
    }
    while(opt1!=2);

}
#include <sstream>

using namespace std;

bool isUniqueID(ifstream& file, int id)
{

    string id_string = to_string(id);
    string currently_read_line;
    // The position of the searched key. So, in this case,
    // only the 3rd value will be tested (starting from 0).
    // John Doe 23 456
    //   |   |   |   |
    //   0   1   2   3 (the id)
    int offset = 3;

    while (getline(file, currently_read_line))
    {
        istringstream ss(currently_read_line);
        string current_entry;
        int counter = 0;

        while (ss >> current_entry) {

            if (current_entry == id_string && counter == offset) {
                cout << "The Id already exists." << endl;
                return false;
            }
            counter++;

        }
    }

    // No match found
    cout << "The ID does not exist yet." << endl;
    return true;

}

請注意:

  • 只需將打開的文件傳遞給 function。 該文件只打開一次,而不是每次您要檢查 ID 時都打開它。
  • 這需要在 -std=c++11 中編譯(用於 to_string 轉換)

[更新]

偏移變量告訴 function 要測試什么值。 一種更一致的方法是將數據格式化為每個學生條目都有一個鍵/值。 它雖然工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM