2017-08-14 124 views
0

我写一个图书馆系统的一个小项目,我有这样的代码至今: 我试图表现出的第一件事是显示菜单,以便用户可以选择,一旦用户的动产登记一本新书,registerBook(bookInfo)应该打印所有的问题,数据将存储在我创建的文件中。但是,代码没有这样做,并且在处理registerBook时放入的数据不存储在文本文件中。你能告诉我我缺少什么或为什么不存储数据吗?创建文件并添加数据? C++

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

}

+0

没有'这里cout''<< bookInfo.copiesNumber <<”“<<的BookInfo。价格<< endl;' –

+2

您需要使用'cin.ignore()'来消耗和放弃换行符,'cin.clear()'来重置错误位。希望有人可以找到一个很好的重复问题 - 有负载,但没有很好。 – Useless

+0

'std :: istream :: get()'不像'std :: istream :: getline()'不提取分隔符(默认情况下是换行符(也是**)**总是**验证输入是否成功使用该结果,例如,使用'如果(标准:: cin.get(姓名,50)){/ *使用结果* /}'的 –

回答

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

+0

谢谢!为什么不能使用char?只是为了未来的项目。我设置的大小数组是50,对于说“杀死MockingBird”太短了吗? – Emily

+0

您正在使用的“char array的大小”是固定的。如果您的姓名超过指定的大小,这会造成问题。所以它总是倾向于使用'string' –

+0

如果你仍然想使用'char array',那么在你的代码中每调用一次'cin.get()'函数后,只需添加'cin.ignore();'就可以工作为你而出。但是,如果输入超过50个'char',那么它将覆盖下一个参数的值。所以,要么限制了任何用户可以输入的字符数'string'。 –

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; 
} 
相关问题