C++ File I am unable to open the books.txt file I need to run the program. The f
ID: 3741483 • Letter: C
Question
C++ File I am unable to open the books.txt file I need to run the program. The file is created and in the Debug folder. Here is the code that I have as well as the contents of the text file.
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 tittle 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;
}
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
Explanation / Answer
You are not able to access your file because its in debug folder and when the program executes it default folder is the release folder. Try to change the working folder at run in context menu of properties and then run the program.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.