创建文件并添加数据C ++ [英] Creating a file and adding data C++

查看:83
本文介绍了创建文件并添加数据C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个小项目编写一个库系统,到目前为止我已经有了这个代码:我要展示的第一件事是show菜单,这样用户可以选择,一旦用户选择注册一本新书, registerBook(bookInfo)应该打印所有问题,数据将存储在我创建的文件中。但是,代码没有这样做,并且我在处理registerBook时输入的数据不存储在文本文件中。你能告诉我我错过了什么,或者为什么没有存储数据?



我尝试了什么:



I am writing a library system for a small project and I have this code so far: The first thing I'm trying to show is the show menu so the user can choose and once the user choses to register a new book, the registerBook(bookInfo) should print all the questions and the data would be stored in the file that I created. However, the code is not doing this and the data that I put in when registerBook is processed is not stored in the text file. Could you someone tell me what I'm missing or why it's not storing the data?

What I have tried:

<pre lang="c++"><pre>#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;

struct BookShelf{
    string name;
    string author;
    int ID;
    int copiesNumber;
    float price;
};

void registerBook(BookShelf *bookInfo) {
    
    cout<<"Enter the book's name: "<<endl;
    std::getline(cin, bookInfo->name) ;
    cout<<"Enter the author of the book: "<<endl;
    std::getline(cin, bookInfo->author);
    cout<<"Enter the books ID: "<<endl;
    cin>>bookInfo->ID;
    cout<<"Enter number of the book's copies available: "<<endl;
    cin>>bookInfo->copiesNumber;
    cout<<"Enter the book's price: "<<endl;
    cin>>bookInfo->price;
    
    
}

void searchBook(BookShelf bookInfo) {
    int choice;
    cout<<"Would you like to search for the book by"<<endl;
    cout<<"1)Book Name"<<endl;
    cout<<"or"<<endl;
    cout<<"2)Book ID"<<endl;
    cin>>choice;
    if(choice==1) {
        string searchName;
        cout<<"Enter book's name:"<<endl;
        std::getline(cin, searchName); //why does the program end here? it outputs the quetsion and the programe ends.
        
    }
    else if (choice==2) {
        int bookID;
        cout<<"To update the book enter book's ID:"<<endl;
        cin>>bookID;
        //create for loop to find a match for the entered book's ID, if found
    }
    
}

void updateBook(BookShelf bookInfo) {
    int bookID;
    cout<<"Enter book's ID to update: "<<endl;
    cin>>bookID;
    
    
}

void deleteBook(BookShelf bookInfo) {
    int deleteID;
    cout<<"Enter book's ID:"<<endl;
    cin>>deleteID;
    
    
}

void borrowBook(BookShelf bookInfo) {
    int borrowName;
    cout<<"Enter name of the book you want to borrow:"<<endl;
    cin>>borrowName;
    //use for loop to search if the entered name matches any of the available book names in the file, if it is available output "book is available to borrow", if no matches were found then output"no matches were found"
    
}

void exitPrograme(BookShelf bookInfo) {
    cout<<"You exited the programe. See you next time!"<<endl;
    
}



int main() {
    
    ofstream trylibrary("fakelibrary.txt", ios::in);
    
    BookShelf bookInfo;
    int option;
    do {
        
        cout<<"What would you like to do?"<<endl;
        cout<<"1)Register new Book"<<endl;
        cout<<"2)Search for book"<<endl;
        cout<<"3)Update a book"<<endl;
        cout<<"4) Delete a book"<<endl;
        cout<<"5)Borrow a book"<<endl;
        cout<<"6)Exit the programe"<<endl;
        cin>>option;
        if(option==1) {
            registerBook(&bookInfo);
        }
        else if(option==2) {
            searchBook(bookInfo);
        }
        else if(option==3) {
            updateBook(bookInfo);
            
        }
        else if(option==4) {
            deleteBook(bookInfo);
        }
        else if(option==5) {
            borrowBook(bookInfo);
        }
        else if(option==6) {
            exitPrograme(bookInfo);
        }
        else {
            cout<<"Please Choose an option."<<endl;
        }
    }
    while(option!=6);
    return 0;

    //trylibrary<<bookInfo.name<<" "<<bookInfo.author<<" "<<bookInfo.ID<<" "<<bookInfo.copiesNumber<<" "<<bookInfo.price<<endl;
}

推荐答案

while(library>>bookInfo.name>>bookInfo.author>>bookInfo.ID<<bookInfo.copiesNumber<<bookInfo.price) {
    cout<<left<<setw(50)<<bookInfo.name<<"   "<<left<<setw(20)<<bookInfo.author<<"   "<<left<<setw(7)<<bookInfo.ID<<"   "<<endl;
}



上述声明必然会引发问题。每行代码读取和写入一个项目,然后您就可以看到它出错的地方。而不是试图将整个程序一次性编写,而是逐步完成。首先编写读取和写入文件的部分,然后使用一些示例数据对其进行测试。然后一次添加一个新方法,逐个测试每个方法。


Statements like the above are bound to cause problems. Read and write your items one per line of code, and you will then be able to see where it is going wrong. And rather than trying to get the entire program written in one go, do it in steps. Start by coding the part that reads and writes the files, and use some sample data to test it. Then add each new method one at a time, testing each as you go along.


在C ++中,struct数据类型是复制参数,因此在函数中调用一个新实例(带有复制的内容) )已创建。



您需要使用引用类型。所以你应该使用指向结构的指针,如:





In C++ struct data type are copy parameters, so in a function call a new instance (with the copied content) is created.

You need to work with reference types. So you should use pointers to the structs like:


// implementation
void registerBook(BookShelf *pbookInfo) {
 
    cout<<"Enter the book's name: "<<endl;
    std::getline(cin, pbookInfo->name) ;
    // ...
}
// call
registerBook( &bookInfo ); // & operator gives the pointer to the object

使用调试器来更好地理解它。

Use the debugger to better understand it.


为了将对象保存(加载)到(来自)文件你需要序列化它们。例如,请参阅:序列化和反序列化 - 标准C ++ [ ^ ]。
In order to save (load) objects to (from) file you need to serialize them. See, for instance: Serialization and Unserialization - Standard C++[^].


这篇关于创建文件并添加数据C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆