Why does this C++ code result in a segmentation fault?
- by user69514
I keep getting a segmentation fault when the readAuthor() method is called. Does anybody know why this happens? I am supposed to use dynamic arrays, I know this would be so easy if I was using static array.
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
/** declare arrays **/
int* isbnArr = new int[25];
char* authorArr = new char[25];
char* publisherArr = new char[25];
char* titleArr = new char[25];
int* editionArr = new int[25];
int* yearArr = new int[25];
int* pagesArr = new int[25];
float* retailPriceArr = new float[25];
float* discountedPriceArr = new float[25];
int* stockArr = new int[25];
/** function prototypes **/
int readIsbn();
char* readAuthor();
char* readPublisher();
char* readTitle();
int readEdition();
int readYear();
int readPages();
float readMsrp();
float readDiscountedPrice();
int readStockAmount();
void readonebook(int* isbn, char* author, char* title, char* publisher, int* edition,
                int* year, int* pages, float* msrp, float* discounted, int* inventory);
int main()
{
    bool stop = false;  //flag when to stop loop
    int ind = 0;        //index for current book
    while( !stop ){
        cout << "Add book: press A: ";
        cout << "another thing here ";
        char choice;
        cin >> choice;
        if( choice == 'a' || choice == 'A' ){
            readonebook(&isbnArr[ind], &authorArr[ind], &titleArr[ind], &publisherArr[ind], &editionArr[ind],
            &yearArr[ind], &pagesArr[ind], &retailPriceArr[ind], &discountedPriceArr[ind], &stockArr[ind]);
            test(&authorArr[ind]);
            ind++;
        }
    }
    return 0;
}
/** define functions **/
int readIsbn(){
    int isbn;
    cout << "ISBN: ";
    cin >> isbn;
    return isbn;
}
char* readAuthor(){
    char* author;
    cout << "Author: ";
    cin >> author;
    return author;
}
char* readPublisher(){
    char* publisher = NULL;
    cout << "Publisher: ";
    cin >> publisher;
    return publisher;
}
char* readTitle(){
    char* title = NULL;
    cout << "Title: ";
    cin >> title;
    return title;
}
int readEdition(){
    int edition;
    cout << "Edition: ";
    cin >> edition;
    return edition;
}
int readYear(){
    int year;
    cout << "Year: ";
    cin >> year;
    return year;
}
int readPages(){
    int pages;
    cout << "Pages: ";
    cin >> pages;
    return pages;
}
float readMsrp(){
    float price;
    cout << "Retail Price: ";
    cin >> price;
    return price;
}
float readDiscountedPrice(){
    float price;
    cout << "Discounted Price: ";
    cin >> price;
    return price;
}
int readStockAmount(){
    int amount;
    cout << "Stock Amount: ";
    cin >> amount;
    return amount;
}
void readonebook(int* isbn, char* author, char* title, char* publisher, int* edition,
                int* year, int* pages, float* msrp, float* discounted, int* inventory){
    *isbn = readIsbn();
    author = readAuthor();
    title = readTitle();
    publisher = readPublisher();
    *edition = readEdition();
    *year = readYear();
    *pages = readPages();
    *msrp = readMsrp();
    *discounted = readDiscountedPrice();
    *inventory = readStockAmount();
}