Here is my .h file
#pragma once
#include <iostream>
#include <string>
using namespace std;
enum PublicationType { BOOK, MAGAZINE, NEWSPAPER, AUDIO, VIDEO };
class Publication
{
private:
string title;
string publisher;
double price;
int year;
PublicationType type;
int stock;
const int size = 40;
public:
void storePublication(string itemTitle, string itemPublisher, double itemPrice, int itemYear, PublicationType itemType, int itemStock);
void displayInfo();
void checkOut();
void checkIn();
string getTitle();
int getStock();
};
Here is my .cpp file
#include "Publication.h"
void Publication :: storePublication(string itemTitle, string itemPublisher, double itemPrice, int itemYear, PublicationType itemType, int itemStock)
{
title = itemTitle;
publisher = itemPublisher;
price = itemPrice;
year = itemYear;
type = itemType;
stock = itemStock;
}
void Publication :: displayInfo()
{
cout << "Title: " << title << endl;
cout << "Publisher: " << publisher << endl;
cout << "Price: $" << price << endl;
cout << "Year: " << year << endl;
cout << "Type: " << type << endl;
cout << "Stock: " << stock << endl;
}
void Publication :: checkOut()
{
if (stock != 0)
{
stock = stock - 1;
}
}
void Publication :: checkIn()
{
stock = stock + 1;
}
string Publication :: getTitle()
{
return title;
}
int Publication :: getStock()
{
return stock;
};
And here is my source code
#include <iostream>
#include <string>
#include <fstream>
#include "Publication.h"
using namespace std;
int menu()
{
int select = 0;
cout << "Choice 1 - Display all publications" << endl;
cout << "Choice 2 - Display Publication titles" << endl;
cout << "Choice 3 - Find a Publication" << endl;
cout << "Choice 4 - Check Out" << endl;
cout << "Choice 5 - Check In" << endl;
cin >> select;
return select;
}
void getPublications(Publication p[], int &i)
{
string itemTitle;
string itemPublisher;
double itemPrice;
int itemYear;
PublicationType itemType;
int temp;
int itemStock;
ifstream f;
f.open("publications.txt");
for(int k = 0; k < 6; k++)
{
getline(f, itemTitle);
cout << itemTitle << endl;
getline(f, itemPublisher);
f >> itemPrice;
f >> itemYear;
f >> temp;
itemType = static_cast<PublicationType>(temp + 1);
f >> itemStock;
p[i].storePublication(
itemTitle,
itemPublisher,
itemPrice,
itemYear,
static_cast<PublicationType>(itemType - 1),
itemStock
);
cout << "test1" << endl;
i++;
cout << endl;
cout << i << endl;
}
f.close();
}
void showPublications(Publication p[], int i)
{
cout << i << endl;
for (int j = 0; j < i; j++)
{
p[j].displayInfo();
cout << endl;
cout << j << endl;
}
}
void showTitles(Publication p[], int i)
{
for (int j = 0; j < i; j++)
{
cout << p[j].getTitle() << endl;
cout << "test3" << endl;
}
}
int findPublication(Publication p[], int i, string find)
{
cout << "Publication Title" << endl;
getline(cin, find);
if (find == "Starting Out with C++")
{
return 1;
}
else if (find == "The World is Flat")
{
return 2;
}
else if (find == "Good to Great")
{
return 3;
}
else if (find == "You Cant Make this Stuff Up")
{
return 4;
}
else if (find == "Winners")
{
return 5;
}
else if (find == "The Wall Street Journal")
{
return 6;
}
else
{
return 10;
}
}
int main()
{
string find;
int interim;
int choice = 0;
Publication p[6];
int i = 0;
getPublications(p, i);
while (choice < 4)
{
choice = menu();
cout << endl;
cout << endl;
// Chooses menu option
switch (choice)
{
case 1:
showPublications(p, i);
break;
case 2:
showTitles(p, i);
break;
case 3:
interim = findPublication(p, i, find) - 1;
if (interim < 6)
{
p[interim].displayInfo();
cout << endl;
}
else
{
cout << "No Publication Found" << endl;
}
break;
default:
return 0;
}
}
return 0;
}
What I am trying to do is input a bunch of info from a text file, and put it into arrays, then later in the function call that information back in pieces.
Where I am getting a problem is after getPublications loops. The first object works perfectly, but the next 5 are all the same dud.
This is the text file I am reading from.
Starting Out with C++
Pearson
129.98
2018
0
25
The World is Flat
Farrar, Straus and Giroux
30.00
2006
0
12
Good to Great
Collins Business
29.99
2001
0
10
You Cant Make this Stuff Up
Atrai Books
25.00
2021
1
7
Winners
Dell
7.99
2013
1
6
The Wall Street Journal
News Corp
2.95
2021
2
1
This is the first array object, as you can see it works perfectly
Title: Starting Out with C++
Publisher: Pearson
Price: $129.98
Year: 2018
Type: 0
Stock: 25
But then the next 5 objects are all the same and all wrong
Title:
Publisher: The World is Flat
Price: $0
Year: 2018
Type: 0
Stock: 25
It deletes a title, moves a variable, clears a price, reuses the year, type, and stock.
I dont know if I am missing something or I added something unnecessary. This is my first programming class and I was doing well until classes.
Thank you for your help!