创建文件并添加数据? C ++

时间:2017-08-14 15:00:42

标签: c++

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

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

}

void showMenu(BookShelf bookInfo) {
    int option;
    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);
    }

}

int main() {

    ofstream library("mylibrary.txt", ios::app);


    BookShelf bookInfo;


    showMenu(bookInfo);

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

    ifstream bank("mylibrary.txt", ios::in);//to read the file that I previously created
    BookShelf bookInfo;
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;

}

2 个答案:

答案 0 :(得分:0)

试试这段代码:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

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

int main() {
    ofstream books("My Library Books.txt", ios::app); //creating a file
    myBook 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;

    return 0;
}

我已将nameauthor更改为字符串,并使用std::getline代替cin.get

参考here

答案 1 :(得分:0)

你需要在每次要求更多输入之前插入cin.ignore():

int main() {
   ofstream books("My Library Books.txt", ios::app); //creating a file  
   myBook bookInfo;
   cout << "Enter the book's name: " << endl;
   cin.get(bookInfo.name, 50);
   cout << "Enter the author of the book: " << endl;
   cin.ignore(); // <--
   cin.get(bookInfo.author, 50);
   cout << "Enter the books ID: " << endl;
   cin.ignore(); // <--
   cin >> bookInfo.ID;
   cout << "Enter number of the book's copies available: " << endl;
   cin.ignore(); // <--
   cin >> bookInfo.copiesNumber;
   cout << "Enter the book's price: " << endl;
   cin.ignore(); // <--
   cin >> bookInfo.price;

   return 0;
}