0

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!

  • Start your program in your debugger and step line by line through the code until you find the exact location of this problem. If you're not able to solve it provide a [mcve] of this problem and describe it as detailed as possible. Don't dump your whole and expect others to debug it for you. – Thomas Sablik Dec 12 '20 at 22:21
  • How do you access the debugger, I am on visual studio – Coderman Dec 12 '20 at 22:22
  • Set a breakpoint and run the code. It will automatically start the debugger. [Breakpoints](https://learn.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019) [Debugger](https://learn.microsoft.com/en-us/visualstudio/debugger/?view=vs-2019) – Thomas Sablik Dec 12 '20 at 22:28
  • Well I know where the problem is, but I cannot figure out the why. I don't know why the store Publication info is not updating properly. – Coderman Dec 12 '20 at 22:28
  • In which line the actual behavior differs from your expectations? – Thomas Sablik Dec 12 '20 at 22:29
  • I ran the debugger, but it only told me what I knew, that the variables are not updating properly. Is there something I did not code in to update them? – Coderman Dec 12 '20 at 22:29
  • The lines under for(int k = 0; k < 6; k++), specifically the getline(f,itemTitle) do not update with the variable that I want them to. Then they never update after that – Coderman Dec 12 '20 at 22:30
  • Does this answer your question? [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) TLDR; `operator>>` doesn't remove the line breaks from input stream. `getline` reads empty lines after `operator>> – Thomas Sablik Dec 12 '20 at 22:32
  • I did try using cin.ignore() but that totally stopped any input from the files which is worse than what I have now – Coderman Dec 12 '20 at 22:35
  • The problem is the file input stream, not the console input stream. You need `f.ignore()` – Thomas Sablik Dec 12 '20 at 22:44
  • Genius! Thanks so much! – Coderman Dec 12 '20 at 22:49
  • Notice that if you had a slightly different input file, where the first book was _Oliver Twist_ instead of _Starting Out with C++_, if you tried to use menu choice 3 to look up _Starting Out with C++_ the program would instead show you the entry for _Oliver Twist_. Do you see why? Generally if you're reading input from a file, you shouldn't assume you know exactly what the input will be. – David K Dec 12 '20 at 23:50

0 Answers0