Combining Struct and Stack

So I have made a program for schoolwork which is supposed to be a bookshop's stock program. The program ought to have Struct, Stack, Pointer, Array, and a Searching Method. My program already has a struct like this (there are more variables in my actual program)
1
2
3
4
5
6
struct BookInfo
{
    int year;
    string author;
    string publisher;
};

My question is, is there any way I can make a stack of this struct, but with 1 book occupying 1 stack? (stack 1 consists of a BookInfo containing all the variables in it, instead of stack 1 only containing year, stack 2 contains author, etc.)

Thank you in advance! :)
EnderBoy
Last edited on
Normally you would create a stack of books like this.
1
2
#include<stack> 
stack<BookInfo> books;

However using a stack for the books is not the best way.
Can you post the complete assignment. Maybe I miss sth. here.
This is the input part of code that I have made. I have cut out all the output part like menus and stuff.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

struct BookInfo
{
    int price, year, stock;
    string title;
    string author;
};

void input_book(BookInfo data[]);

int main()
{
    BookInfo *data = new BookInfo[10];
    input_book();

    delete [] data;
    return 0;
}

void input_book(BookInfo data[])
{
    int iPrice, iYear, iStock;
    string iTitle, iAuth;

    //Forgot to put the loop here, too busy translating var names from my language lol
    cout << "  Book title     : " ; getline(cin, iTitle);
    cout << "  Price          : " ; cin >> iPrice;
    cout << "  Author         : " ; getline(cin, iAuth);
    cout << "  Year           : " ; cin >> iYear;
    cout << "  Stock          : " ; cin >> iStock;
 
    data[0].title = iTitle;  //all the zeroes should be i from the loop
    data[0].price= iPrice;
    data[0].author= iAuth;
    data[0].year= iYear;
    data[0].stock= iStock;
}


One more thing, my teacher said we can not use stack header for this project so we can understand more of the actual work behind stacks. She told us to create a 'manual' stack, so here is the example that she gave us:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Stack 
{
private: 
    int top; 
    int size;
    int* elt; 
public: 
    Stack();
    ~Stack() { delete [] elt;} 
    void push(int a);
    int pop();
    int isempty(){ if (top == -1)return 1; else return 0;};
    int isfull(){ if (top == 9) return 1; else return 0;};
}; 

Stack::Stack()
{
    top = -1;
    cout<<"Stack Size is 10" <<endl;
    size=10;
    elt = new int[size];
}

void Stack::push(int a)
{
    if(isfull() == 1)
    {
        cout << "stack is full" << endl;
        exit(1);
    }
    cout << "Pushing "<< a << endl;
    elt[++top] = a;  
    cout << "Top= "<< top << endl;
}

int Stack::pop()
{
    if(isempty() == 1)
    {
        cout << "stack is empty" << endl;
        exit(1);
    }
    cout << "Poping "<< elt[top] << endl;
    return elt[top--];
}


Is there anyway I can store the the *data in my code to this example she gave?
Last edited on
It's still not clear where the stack fits in. In main you create an array to store the BookInfos what makes sense. BTW you need to pass the array to the input_book function.
Why do you input only 1 book when the array has space for 10 ?
Topic archived. No new replies allowed.