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.