簡體   English   中英

c++編程函數錯誤

[英]c++ programming function error

我的完整代碼是(無法縮小):

/*password is admin*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>

using namespace std;

//-------------------------------
fstream d_base;
char path[] = "library books.txt";

void output(){
//this function for displaying choices only
cout << "***********************************" << endl;
cout << "1. List all books in library" << endl;
cout << "2. List available books to borrow " << endl;
cout << "3. Borrow a Book from library" << endl;
cout << "4. Search For a Book" << endl;
cout << "5. Add New Books"<< endl; 
cout << "6. Delete a Book" << endl;
cout << "7. EXIT The Library"<< endl;
cout << "***********************************" << endl;
 }

 //=====================================================================================================================================================

struct books{
//identfying books with all needed things
int id, status;
string title, p_name, p_address;
string aut_name, aut_nationality;
string date;
};

//=====================================================================================================================================================

//function for choice 1 showing the books (under constructions)

void choice1(){
ifstream show;
char all;
show.open(path, ios::in | ios::app);
while (!show.eof()){
    show >> all;
    if (all == '%'){
        cout << "\n\n";
    }
    else if (all == '.'){
        cout << "\n\n\n";
    }
    else
        cout << all;
}
cout << endl;
show.close();
}

    //=====================================================================================================================================================

void choice2(){

//function for choice 2 (list available books to borrow)
}

//=====================================================================================================================================================

void choice3(){

//function for choice 3( Borrow a Book )
 }

//=====================================================================================================================================================

void choice4(){
char s;
ifstream search;
char idx;
cout << "what book you want to search for : ";
cin >> idx;
search.open(path, ios::in | ios::app);
while (!search.eof()){
    search >> s;
    if (s == idx)
        cout << "book found" << endl;
    break;
    }
search.close();
}

//=====================================================================================================================================================

//for choice 5 to fill books (under constructions)
void choice5(books new_book[],books aut[], int books_number,int aut_number){

//function for adding books to the system 

cout << "how many books you want to add ? ";
cin >> books_number;

    //call the function to record the book

    d_base.open(path, ios::out | ios::app);
    for (int i = 0; i < books_number; i++){
        d_base << "[Book Id]: " << new_book[i].id << "%[title]: " <<     new_book[i].title;
        d_base << "%[Publisher Name]: " << new_book[i].p_name << "%    [puplisher Address]: " << new_book[i].p_address;
        for (int j = 0; j < aut_number; j++){
            d_base << "%[author info]" << "%[Authors Name]: " <<    aut[i].aut_name << "%[Nationality]: " << aut[i].aut_nationality;
        }
        d_base << "%[PublishedAt]: " << new_book[i].date << "%[status]:" << new_book[i].status << "." << endl;
    }
    d_base.close();
 }

 //=====================================================================================================================================================

 void choice6(){

//function for searching for a book

 }

 //=====================================================================================================================================================

int main(){
string choice;
cout << "welcome to FCIS library\n\n";

do{
    output();
    cout << "what do you want to do ? ";
    getline( cin , choice);

    if (choice == "1"){
        choice1();
    }

    //this one for list available books
    else if (choice == "2"){

        choice2();

    }

    //this one for borrow a book
    else if (choice == "3"){

        //not completed yet don't choose 3

    }
    else if (choice == "4"){

        choice4();

    }

    //this one is for adding new books to the list 
    else if (choice == "5"){
        int books_number, aut_number;
        books new_book[10000], aut[10000];
        string password;
        do{

            cout << "you must be an admin to add new books." << endl << "please enter passowrd (use small letters) : ";
            cin >> password;

            if (password == "b")
                break;

            else if (password == "admin"){
                cout << "ACCESS GAINED   WELCOME " << endl;

                cout << "what books you want to add :" << endl;
                for (int i = 0; i < books_number; i++){
                    cout << "id please : "; cin >> new_book[i].id;
                    cout << "title : ";              cin.ignore();  getline(cin, new_book[i].title);
                    cout << "publisher name :";                     getline(cin, new_book[i].p_name);
                    cout << "publisher address : ";                 getline(cin, new_book[i].p_address);
                    cout << "Publish date :";                       getline(cin, new_book[i].date);
                    cout << "How many copies of " << new_book[i].title << " ";      cin >> new_book[i].status;

                    cout << "How Many Authors for the Book ?"; cin >> aut_number;
                    for (int j = 1; j <= aut_number; j++){
                        cout << "author number " << j << " name : "; cin.ignore();   getline(cin, aut[i].aut_name);
                        cout << "Nationality : ";                   getline(cin, aut[i].aut_nationality);

                        choice5(new_book[i], aut[j], books_number, aut_number);
                    }
                }
            }
            else{

                cout << "Wrong password try again or press (b) to try another choice";
                continue;
            }

        } while (password != "admin");
    }

    //this one for deleteing a book
    else if (choice == "6"){

        //not completed yet

    }
    else if (choice == "7"){

        cout << "Thanks for Using FCIS LIBRARY" << endl;
        break;

    }
    else
        cout << "\nwrong choice please choose again\n\n";

} while (true);

}

問題是,當我調用choice5() 函數時,它會出現錯誤:

*-IntelliSense:沒有合適的從“書籍”到“書籍”的轉換功能

*-IntelliSense:沒有合適的從“書籍”到“書籍”的轉換功能

-錯誤 C2664:'void choice5(books [],books [],int,int)': 無法將參數 1 從 'books' 轉換為 'books []

不知道是參數問題還是什么!!

選擇5(); 提交書籍后,函數調用在 if(choice==5) 的 main 中

我就像 C++ 的 1 級,所以我正在盡我所能讓它更小

不知道是參數問題還是什么!!

編譯器會准確地告訴您問題出在哪里:您對choice5的調用。 第一個參數是一個books數組,你傳入的是一個book

choice5(new_book[i], aut[j], books_number, aut_number);

new_book是一個數組, new_book[i]是數組中的特定book aut

函數choice5(books new_book[],books aut[], int books_number,int aut_number) 必須接收一個books 數組或指向結構books 的指針作為第一個參數。 第二個參數“aut”也會有同樣的問題。 為了與函數定義匹配,您的呼叫應具有以下格式:choice5(new_book, aut, books_number, aut_number)

暫無
暫無

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

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