C++ Error Message I am getting the following error message when I try to run the
ID: 3700141 • Letter: C
Question
C++ Error Message I am getting the following error message when I try to run the debugger.
'LibraryMain.exe' (Win32): Loaded 'C:Program Files (x86)AVGAntivirusswhookx.dll'. Cannot find or open the PDB file.
'LibraryMain.exe' (Win32): Loaded 'C:WindowsSysWOW64msvcp140d.dll'. Symbols loaded.
'LibraryMain.exe' (Win32): Loaded 'C:WindowsSysWOW64cruntime140d.dll'. Symbols loaded.
'LibraryMain.exe' (Win32): Loaded 'C:WindowsSysWOW64ucrtbased.dll'. Symbols loaded.
Exception thrown at 0x0F7C50D9 (vcruntime140d.dll) in LibraryMain.exe: 0xC0000005: Access violation writing location 0x151590E7.
Here is my books.txt file.
Starting Out with C++
Gaddis, Tony
Pearson
978-0-13-257625-3
129.98
2014
25
The World is Flat
Friedman, Thomas
Farrar, Straus and Giroux
0-374-29279-5
30.00
2006
12
Good to Great
Collins, Jim
Collins Business
978-0-06-662099-2
29.99
2001
10
You Cant Make this Stuff Up
Caputo, Theresa
Atrai Books
978-1-4767-6443-6
25.00
2014
8
Winners
Steel, Danielle
Dell
978-0-440-24525-4
7.99
2013
6
The Success Principles
Canfield, Jack
Harper Collins
0-06-059488-8
24.95
2005
4
Here is the BookClass.h code.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class BookClass
{
string title;
string author;
string publisher;
string isbn;
double price;
int year;
int numInStock;
public:
void IncreaseDecreaseCopies(int flag);
int getCopiesLeft();
void displayBookInfo();
string getTitle();
void storeBook(string info[]);
void checkOutBook()
{
if (numInStock > 0)
{
cout << "Checking out a book." << endl;
numInStock--;
}
else
cout << "No books in stock." << endl;
}
void returnBook()
{
cout << "Book returned successfully." << endl;
numInStock++;
}
int getNumInStock()
{
return numInStock;
}
};
Here is the LibraryMain.cpp code.
#include "BookClass.h"
#include<string>
using namespace std;
void BookClass::IncreaseDecreaseCopies(int flag)
{ // 0 for increment and 1 for decrement
if (flag == 0)
{
numInStock++;
}
if (flag == 1)
{
numInStock--;
}
}
int BookClass::getCopiesLeft()
{
return numInStock;
}
string BookClass::getTitle()
{
return title;
}
void BookClass::displayBookInfo()
{
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Publisher: " << publisher << endl;
cout << "ISBN: " << isbn << endl;
cout << "Price: " << fixed << setprecision(2) << price << endl;
cout << "Year: " << year << endl;
cout << "Books in Stock: " << numInStock << endl;
}
void BookClass::storeBook(string info[])
{
string::size_type sz;
title = info[0];
author = info[1];
publisher = info[2];
isbn = info[3];
price = stof(info[4], &sz);
year = stoi(info[5], &sz);
numInStock = stoi(info[6], &sz);
}
Here is the BookMain.cpp code.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include<cstring>
using namespace std;
#include "BookClass.h"
#define MAX_BOOKS 3
void getBooks(BookClass *books, int &totalBooks)
{
string fileName, line, tempInfo[7];
int info;
totalBooks = 0;
ifstream file("books.txt");
if (file.is_open())
{
info = 0;
while (getline(file, line))
{
if (line != "")
{
tempInfo[info++] = line;
if (info == 7)
{
books[totalBooks++].storeBook(tempInfo);
info = 0;
tempInfo->clear();
}
}
}
}
}
void showBooks(BookClass *books, int totalBooks)
{
for (int i = 0; i < totalBooks; i++)
{
books[i].displayBookInfo();
}
}
void showTitles(BookClass *books, int totalBooks)
{
for (int i = 0; i < totalBooks; i++)
{
cout << books[i].getTitle() << endl;
}
}
int findBook(string book, BookClass *books, int totalBooks)
{
for (int i = 0; i < totalBooks; i++)
{
if (strcmp(books[i].getTitle().c_str(), book.c_str()) == 0)
{
return i;
}
}
return -1;
}
int getMenuChoice()
{
cout << "Michael's Library " << endl;
cout << "Please select from the menu below. " << endl;
int choice;
cout << "1: Display all books 2: Display book titles 3: Find book 4: Check out 5: Check in 6: Exit program" << endl;
cin >> choice;
return choice;
cout << " " << endl;
}
int main()
{
BookClass books[MAX_BOOKS];
string book;
bool exit = false;
int totalBooks, idx;
getBooks(books, totalBooks);
while (!exit)
{
switch (getMenuChoice())
{
case 1:
showBooks(books, totalBooks);
break;
case 2:
showTitles(books, totalBooks);
break;
case 3:
cout << "enter the name of the book to find : ";
getline(cin, book);
getline(cin, book);
idx = findBook(book, books, totalBooks);
if (idx == -1)
{
cout << "This book is not available ";
}
else
{
cout << books[idx].getCopiesLeft() << " copies of " << books[idx].getTitle() << " are in stock " << endl;
}
break;
case 4:
cout << "Enter title of book to check out ";
getline(cin, book);
getline(cin, book);
idx = findBook(book, books, totalBooks);
if (idx == -1)
{
cout << "Sorry we do not stock that book ";
}
else
{
cout << "Book is available to check out ";
books[idx].IncreaseDecreaseCopies(1);
cout << books[idx].getCopiesLeft() << " copies of " << books[idx].getTitle() << " are in stock after checkout" << endl;
}
break;
case 5:
cout << "Enter title of book to check in ";
getline(cin, book);
getline(cin, book);
idx = findBook(book, books, totalBooks);
if (idx == -1)
{
cout << "Sorry we do not stock that book ";
}
else
{
cout << books[idx].getTitle() << " returned ";
books[idx].IncreaseDecreaseCopies(0);
}
break;
case 6:
return 0;
default:
cout << "Enter correct choice : ";
}
}
return 0;
}
Explanation / Answer
Please follow the below description:
This is not actually the error related to the program or the code.
I suppose that there are mainly one of the two reasons as stated below.
a) The program might ran correctly with the outputs genereated but that the console or the output window that has been created for the program displayed the output and that has been closed immediately.
One way to keep it from closing immediately is to add an extra line: cin.get();
right before the closing of the main method in the program. This should resolve the issue.
If that above solution has not resolved then it might be the one as defined below,
b) This isn't really a programming error, so is off-topic Anti-malware injects itself into every program. Just like a virus. The only way to prevent this is by uninstalling the virus or disable the AVG. If not try by adding an exception in AVG for the program which launches the eventual error, you can also avoid the error.
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.