I'm writing a program for my class that is due tomorrow. I thought I was done, but as I starting using it to get an output file, I ran into a problem, and I can't get past it. I have also gone over my code to see if I made a mistake but I can't seem to find one.

This is the code to my program:

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include"time.h"
using namespace std;

int main()
{
    srand(time(NULL));

    //Constants
    const double SALESTAX = 0.08;
    const double DISCOUNTRATE = 0.05;
    const double DISCOUNT = 95;

    //Counts
    int customer, book;

    //Customer arrays
    string customer_Non[5][2]; // 1 = name, 2 = address
    int customer_Num[5][2]; // 1 = age, 2 = receipt#
    int quantity = 3; //3 books per customer

    //Book arrays
    string book_Non[5][3][3]; // 1 = book number, 2 = Title, 3 = Author
    int bookAisle[5][3]; // 1 aisle per book
    double bookPrice[5][3][2]; // 1 = book price, 2 = discount

    //Calculations
    double calculation[5][5];
    /* 1 = subtotal, 2= total discount, 3 = subtotal and discount
     * 4 = sales tax, 5 = Total to be paid */

    //Validations
    int customerValid [5][2]; //1 = name validation, 2 = address validation
    int bookValid[5][3][3]; //1 = booknum validation, 2 = title validation,
                            //3 = author validation
    ofstream fOut;
    fOut.open("Project4_A00635756_Output.txt");

    for(customer = 1; customer <= 5; customer++)
    {
        customer_Num[customer][2] = rand() % 999 + 1; //Receipt number
        cout << "Customer Name:" << endl;
        getline(cin,customer_Non[customer][1]);
        customerValid[customer][1] = customer_Non[customer][1].length();
        while(customerValid[customer][1] > 30)
        {
            cout << "Name is not valid, max allowable characters is 30."
                    << endl;
            cout << "Customer Name:" << endl;
            getline(cin,customer_Non[customer][1]);
            customerValid[customer][1] = customer_Non[customer][1].length();
        }
        cout << "Customer's address:" << endl;
        getline(cin,customer_Non[customer][2]);
        customerValid[customer][2] = customer_Non[customer][2].length();
        while(customerValid[customer][2] > 50)
        {
            cout << "Address is not valid, max allowable characters is 50."
                    << endl;
            cout << "Customer's address:" << endl;
            getline(cin,customer_Non[customer][2]);
            customerValid[customer][2] = customer_Non[customer][2].length();
        }
        cout << "Customer's age:" << endl;
        cin >> customer_Num[customer][1];
        while(customer_Num[customer][1] > 100 || customer_Num[customer][1] < 1)
        {
            cout << "Please enter an age between 1-100" << endl;
            cout << "Customer's age:" << endl;
            cin >> customer_Num[customer][1];
        }
        cin.ignore(2);
        for(book = 1; book <= quantity; book++)
        {
            cout << "Book number: " << endl;
            getline(cin,book_Non[customer][book][1]);
            bookValid[customer][book][1] = book_Non[customer][book][1].length();
            while(bookValid[customer][book][1] != 7)
            {
                cout << "Book number is not valid, must be 6 characters."
                        << endl;
                cout << "Book number: " << endl;
                getline(cin,book_Non[customer][book][1]);
                bookValid[customer][book][1] =
                        book_Non[customer][book][1].length();
            }
            cout << "Book title: " << endl;
            getline(cin,book_Non[customer][book][2]);
            bookValid[customer][book][2] = book_Non[customer][book][2].length();
            while(bookValid[customer][book][2] > 50)
            {
                cout << "Book title is not valid,"
                        << " max allowable characters is 50."
                        << endl;
                cout << "Book title:" << endl;
                getline(cin,book_Non[customer][book][2]);
                bookValid[customer][book][2] =
                        book_Non[customer][book][2].length();
            }
            cout << "Book author: " << endl;
            getline(cin,book_Non[customer][book][3]);
            bookValid[customer][book][3] = book_Non[customer][book][3].length();
            while(bookValid[customer][book][3] > 30)
            {
                cout << "Author name is not valid,"
                        << " max allowable characters is 30."
                        << endl;
                cout << "Book author:" << endl;
                getline(cin,book_Non[customer][book][3]);
                bookValid[customer][book][3] =
                        book_Non[customer][book][3].length();
            }
            cout << "Book aisle: " << endl;
            cin >> bookAisle[customer][book];
            while(bookAisle[customer][book] > 16 || bookAisle[customer][book] < 1)
            {
                cout << "Book aisle is not valid, must be Aisle 1-16" << endl;
                cout << "Book aisle:" << endl;
                cin >> bookAisle[customer][book];
            }
            cout << "Book price: " << endl << "$";
            cin >> bookPrice[customer][book][1];
            while(bookPrice[customer][book][1] < 0)
            {
                cout << "Book price cannot be negative." << endl;
                cout << "Book price:" << endl << "$";
                cin >> bookPrice[customer][book][1];
            }
            if(bookPrice[customer][book][1] >= DISCOUNT)
                bookPrice[customer][book][2] =
                        bookPrice[customer][book][1] * DISCOUNTRATE;
            else
                bookPrice[customer][book][2] = 0.00;
            cin.ignore(2);
        }

        //Calculations
        calculation[customer][1] = bookPrice[customer][1][1] +
                bookPrice[customer][2][1] + bookPrice[customer][3][1];
        calculation[customer][2] = bookPrice[customer][1][2] +
                bookPrice[customer][2][2] + bookPrice[customer][3][2];
        calculation[customer][3] = calculation[customer][1] +
                                   calculation[customer][2];
        calculation[customer][4] = calculation[customer][3] * SALESTAX;
        calculation[customer][5] = calculation[customer][3]+
                                   calculation[customer][4];



        //RECEIPT Header
        cout << "\n\n\n\n";
        cout << "Customer Name: " << customer_Non[customer][1] << endl;
        cout << "Customer Address: " << customer_Non[customer][2] << endl;
        cout << "Customer Age: " << customer_Num[customer][1] << endl;
        cout << "Number of books: " << quantity << "\n\n";
        cout << setprecision(2) << fixed << showpoint;

        fOut << "\n\n\n\n";
        fOut << "The University Bookstore, Austin, Texas 78716\n";
        fOut << "Receipt #" << customer_Num[customer][2]
             << ", November 6, 2012, 5:0"<< (customer - 1) <<"PM\n\n\n";
        fOut << "Customer Name: " << customer_Non[customer][1] << endl;
        fOut << "Customer Address: " << customer_Non[customer][2] << endl;
        fOut << "Customer Age: " << customer_Num[customer][1] << endl;
        fOut << "Number of books: " << quantity << "\n\n";
        fOut << setprecision(2) << fixed << showpoint;

        //RECEIPT Books
        for(book = 1; book <= quantity; book++)
        {
            cout << "Book Number: " << book_Non[customer][book][1] << endl;
            cout << "Book Title: " << book_Non[customer][book][2] << endl;
            cout << "Book Author: " << book_Non[customer][book][3] << endl;
            cout << "Aisle Number: " << bookAisle[customer][book] << endl;
            cout << setw(21) << left << "Book Price:"
                 << setw(20) << right << bookPrice[customer][book][1] << endl;
            cout << setw(21) << left << "Discount:"
                 << setw(20) << right << bookPrice[customer][book][2] << "\n\n";

            fOut << "Book Number: " << book_Non[customer][book][1] << endl;
            fOut << "Book Title: " << book_Non[customer][book][2] << endl;
            fOut << "Book Author: " << book_Non[customer][book][3] << endl;
            fOut << "Aisle Number: " << bookAisle[customer][book] << endl;
            fOut << setw(21) << left << "Book Price:"
                 << setw(20) << right << bookPrice[customer][book][1] << endl;
            fOut << setw(21) << left << "Discount:"
                 << setw(20) << right << bookPrice[customer][book][2] << "\n\n";
        }

        //RECEIPT Footer
        cout << setw(21) << left << "Subtotal:"
             << setw(20) << right << calculation[customer][1] << "\n";
        cout << setw(21) << left << "Total Discount:"
             << setw(20) << right << calculation[customer][2] << "\n";
        cout << setw(21) << left << "Subtotal w/ Discount:"
             << setw(20) << right << calculation[customer][3] << "\n";
        cout << setw(21) << left << "Sales Tax:"
             << setw(20) << right << calculation[customer][4] << "\n";
        cout << "Your Total Today is $" << calculation[customer][5] << "\n\n";

        fOut << setw(21) << left << "Subtotal:"
             << setw(20) << right << calculation[customer][1] << "\n";
        fOut << setw(21) << left << "Total Discount:"
             << setw(20) << right << calculation[customer][2] << "\n";
        fOut << setw(21) << left << "Subtotal w/ Discount:"
             << setw(20) << right << calculation[customer][3] << "\n";
        fOut << setw(21) << left << "Sales Tax:"
             << setw(20) << right << calculation[customer][4] << "\n";
        fOut << "Your Total Today is $" << calculation[customer][5] << "\n\n";
        fOut << "Thank you for shopping at your University Bookstore!"
             << "\n\n\n\n"
             << "====================================================";

    }
    fOut.close();
}

Pretty simple. Makes a "receipt" that is printed out on screen and in an output file based on inputted data. The problem is that when I get to the fourth customer, after entering the customers name and pressing enter, I get this problem from my compiler:

    1 [main] Project4v2 6652 exception::handle: Exception: STATUS_ACCESS_VIOLATION
    557 [main] Project4v2 6652 open_stackdumpfile: Dumping stack trace to Project4v2.exe.stackdump

and this file is generated:

Exception: STATUS_ACCESS_VIOLATION at eip=0043FEDA
eax=00000000 ebx=00000004 ecx=00447448 edx=0028ABD8 esi=0028ABD8 edi=00447440
ebp=0028A518 esp=0028A40C program=C:\Users\Shelby Jr\workspace\Project4v2\Debug\Project4v2.exe, pid 6652, thread main
cs=0023 ds=002B es=002B fs=0053 gs=002B ss=002B
Stack trace:
Frame     Function  Args
0028A518  0043FEDA  (00447440, 0028ABD8, 0000000A, 0028A6C0)
0028A538  0043FD6B  (00447440, 0028ABD8, 40754333, 004011C1)
0028AC18  00401850  (00000001, 0028AC40, 200280E8, 20061E0F)
0028ACF8  61007535  (00000000, 0028CD78, 61006B20, 00000000)
End of stack trace

I would really appreciate some help with this, and if it helps any, I am using the Eclipse Indigo compiler.

I ran an earlier version of this code, that is missing a lot of the output stuff and all the validation, but has the same input, and just outputs the raw data of the arrays. I changed the number of books to 5, along with any 3's in the arrays that reffered to the book# to 5's to see if maybe it was stopping after the third customer because of the 3 in quantity of books, but I still had the problem after customer 3.

Anyone? I really need to turn this in in about 5 hours

The main problem I see is that you are indexing the arrays incorrectly, which is probably the cause of the segfault. In C++, arrays are zero-indexed, that is to say, an array arr[5] would have elements arr[0], arr[1], arr[2], arr[3], and arr[4]. When you try to access arr[5], you overrun the array and get an access violation.

What this means is that you need to fix all of the array indices in the program. Fortunately for you, I've done just that:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand(time(NULL));

    //Constants
    const double SALESTAX = 0.08;
    const double DISCOUNTRATE = 0.05;
    const double DISCOUNT = 95;

    //Counts
    int customer, book;

    //Customer arrays
    string customer_Non[5][2]; // 1 = name, 2 = address
    int customer_Num[5][2]; // 1 = age, 2 = receipt#
    int quantity = 3; //3 books per customer

    //Book arrays
    string book_Non[5][3][3]; // 1 = book number, 2 = Title, 3 = Author
    int bookAisle[5][3]; // 1 aisle per book
    double bookPrice[5][3][2]; // 1 = book price, 2 = discount

    //Calculations
    double calculation[5][5];
    /* 1 = subtotal, 2= total discount, 3 = subtotal and discount
     * 4 = sales tax, 5 = Total to be paid */

    //Validations
    int customerValid [5][2]; //1 = name validation, 2 = address validation
    int bookValid[5][3][3]; //1 = booknum validation, 2 = title validation,
                            //3 = author validation
    ofstream fOut;
    fOut.open("Project4_A00635756_Output.txt");

    for(customer = 0; customer < 5; customer++)
    {
        customer_Num[customer][1] = rand() % 999 + 1; //Receipt number
        cout << "Customer Name:" << endl;
        getline(cin,customer_Non[customer][0]);
        customerValid[customer][0] = customer_Non[customer][0].length();
        while(customerValid[customer][0] > 30)
        {
            cout << "Name is not valid, max allowable characters is 30."
                    << endl;
            cout << "Customer Name:" << endl;
            getline(cin,customer_Non[customer][0]);
            customerValid[customer][0] = customer_Non[customer][0].length();
        }
        cout << "Customer's address:" << endl;
        getline(cin,customer_Non[customer][1]);
        customerValid[customer][1] = customer_Non[customer][1].length();
        while(customerValid[customer][1] > 50)
        {
            cout << "Address is not valid, max allowable characters is 50."
                    << endl;
            cout << "Customer's address:" << endl;
            getline(cin,customer_Non[customer][1]);
            customerValid[customer][1] = customer_Non[customer][1].length();
        }
        cout << "Customer's age:" << endl;
        cin >> customer_Num[customer][0];
        while(customer_Num[customer][0] > 100 || customer_Num[customer][0] < 1)
        {
            cout << "Please enter an age between 1-100" << endl;
            cout << "Customer's age:" << endl;
            cin >> customer_Num[customer][0];
        }
        cin.ignore(2, '\n');
        for(book = 1; book <= quantity; book++)
        {
            cout << "Book number: " << endl;
            getline(cin,book_Non[customer][book][0]);
            bookValid[customer][book][1] = book_Non[customer][book][0].length();
            while(bookValid[customer][book][0] < 7)
            {
                cout << "Book number is not valid, must be 6 characters."
                        << endl;
                cout << "Book number: " << endl;
                getline(cin,book_Non[customer][book][0]);
                bookValid[customer][book][0] =
                        book_Non[customer][book][0].length();
            }
            cout << "Book title: " << endl;
            getline(cin,book_Non[customer][book][1]);
            bookValid[customer][book][1] = book_Non[customer][book][1].length();
            while(bookValid[customer][book][1] > 50)
            {
                cout << "Book title is not valid,"
                        << " max allowable characters is 50."
                        << endl;
                cout << "Book title:" << endl;
                getline(cin,book_Non[customer][book][1]);
                bookValid[customer][book][1] =
                        book_Non[customer][book][1].length();
            }
            cout << "Book author: " << endl;
            getline(cin,book_Non[customer][book][2]);
            bookValid[customer][book][2] = book_Non[customer][book][2].length();
            while(bookValid[customer][book][2] > 30)
            {
                cout << "Author name is not valid,"
                        << " max allowable characters is 30."
                        << endl;
                cout << "Book author:" << endl;
                getline(cin,book_Non[customer][book][2]);
                bookValid[customer][book][2] =
                        book_Non[customer][book][2].length();
            }
            cout << "Book aisle: " << endl;
            cin >> bookAisle[customer][book];
            while(bookAisle[customer][book] > 16 || bookAisle[customer][book] < 1)
            {
                cout << "Book aisle is not valid, must be Aisle 1-16" << endl;
                cout << "Book aisle:" << endl;
                cin >> bookAisle[customer][book];
            }
            cout << "Book price: " << endl << "$";
            cin >> bookPrice[customer][book][0];
            while(bookPrice[customer][book][0] < 0)
            {
                cout << "Book price cannot be negative." << endl;
                cout << "Book price:" << endl << "$";
                cin >> bookPrice[customer][book][0];
            }
            cin.ignore('\n');
            if(bookPrice[customer][book][0] >= DISCOUNT)
                bookPrice[customer][book][1] =
                        bookPrice[customer][book][0] * DISCOUNTRATE;
            else
                bookPrice[customer][book][1] = 0.00;
            cin.ignore('\n');
        }

        //Calculations
        calculation[customer][0] = bookPrice[customer][0][0] +
                bookPrice[customer][1][0] + bookPrice[customer][2][0];
        calculation[customer][1] = bookPrice[customer][0][1] +
                bookPrice[customer][1][1] + bookPrice[customer][2][1];
        calculation[customer][2] = calculation[customer][0] +
                                   calculation[customer][1];
        calculation[customer][3] = calculation[customer][2] * SALESTAX;
        calculation[customer][4] = calculation[customer][2]+
                                   calculation[customer][3];



        //RECEIPT Header
        cout << "\n\n\n\n";
        cout << "Customer Name: " << customer_Non[customer][0] << endl;
        cout << "Customer Address: " << customer_Non[customer][1] << endl;
        cout << "Customer Age: " << customer_Num[customer][0] << endl;
        cout << "Number of books: " << quantity << "\n\n";
        cout << setprecision(2) << fixed << showpoint;

        fOut << "\n\n\n\n";
        fOut << "The University Bookstore, Austin, Texas 78716\n";
        fOut << "Receipt #" << customer_Num[customer][1]
             << ", November 6, 2012, 5:0"<< (customer - 1) <<"PM\n\n\n";
        fOut << "Customer Name: " << customer_Non[customer][0] << endl;
        fOut << "Customer Address: " << customer_Non[customer][1] << endl;
        fOut << "Customer Age: " << customer_Num[customer][0] << endl;
        fOut << "Number of books: " << quantity << "\n\n";
        fOut << setprecision(2) << fixed << showpoint;

        //RECEIPT Books
        for(book = 0; book < quantity; book++)
        {
            cout << "Book Number: " << book_Non[customer][book][0] << endl;
            cout << "Book Title: " << book_Non[customer][book][1] << endl;
            cout << "Book Author: " << book_Non[customer][book][2] << endl;
            cout << "Aisle Number: " << bookAisle[customer][book] << endl;
            cout << setw(21) << left << "Book Price:"
                 << setw(20) << right << bookPrice[customer][book][0] << endl;
            cout << setw(21) << left << "Discount:"
                 << setw(20) << right << bookPrice[customer][book][1] << "\n\n";

            fOut << "Book Number: " << book_Non[customer][book][0] << endl;
            fOut << "Book Title: " << book_Non[customer][book][1] << endl;
            fOut << "Book Author: " << book_Non[customer][book][2] << endl;
            fOut << "Aisle Number: " << bookAisle[customer][book] << endl;
            fOut << setw(21) << left << "Book Price:"
                 << setw(20) << right << bookPrice[customer][book][0] << endl;
            fOut << setw(21) << left << "Discount:"
                 << setw(20) << right << bookPrice[customer][book][1] << "\n\n";
        }

        //RECEIPT Footer
        cout << setw(21) << left << "Subtotal:"
             << setw(20) << right << calculation[customer][0] << "\n";
        cout << setw(21) << left << "Total Discount:"
             << setw(20) << right << calculation[customer][1] << "\n";
        cout << setw(21) << left << "Subtotal w/ Discount:"
             << setw(20) << right << calculation[customer][2] << "\n";
        cout << setw(21) << left << "Sales Tax:"
             << setw(20) << right << calculation[customer][3] << "\n";
        cout << "Your Total Today is $" << calculation[customer][4] << "\n\n";

        fOut << setw(21) << left << "Subtotal:"
             << setw(20) << right << calculation[customer][0] << "\n";
        fOut << setw(21) << left << "Total Discount:"
             << setw(20) << right << calculation[customer][1] << "\n";
        fOut << setw(21) << left << "Subtotal w/ Discount:"
             << setw(20) << right << calculation[customer][2] << "\n";
        fOut << setw(21) << left << "Sales Tax:"
             << setw(20) << right << calculation[customer][3] << "\n";
        fOut << "Your Total Today is $" << calculation[customer][4] << "\n\n";
        fOut << "Thank you for shopping at your University Bookstore!"
             << "\n\n\n\n"
             << "====================================================";

    }
    fOut.close();
}

Mind you, this isn't how I would write this in the first place; I would use structure types or classes to represent the books and customers. However, since I don't know if you've covered those yet, I'll refrain from commenting further on this. I would also break the program up into several functions, but again, I don't know if you've covered those yet.

I don't know how I missed that, but you sir are a lifesaver! Thank you very much. Solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.