Stacks and Queues

Hi, I'm very inexperienced with C++ and coding in general and need some help with the following task:

1) Change the code so that only the names beginning with the letter J are added to the Stack.
2) Output the number of items in both the queue and the stack.

This is what I have so far:

//StacksQueues.cpp
#include <iostream>
#include <string>
#include <queue>
#include <stack>

using namespace std;

int main() {
string name;
queue<string> myQueue;
stack<string> myStack;

myQueue.push("Tom");
myQueue.push("James");
myQueue.push("Dick");
myQueue.push("John");
myQueue.push("Harry");

cout << "FIFO queue order:\n";
while (myQueue.size() > 0) {
name = myQueue.front();
myQueue.pop();
cout << name << "\n";
myStack.push(name);
}//while

cout << "\nLIFO stack order:\n";
while (myStack.size() > 0) {
cout << myStack.top() << "\n";
myStack.pop();
}//while

cout << endl;
system("pause");
return 0;
}//main



Then, I have to create a new program using the stack template class in C++ STL, which needs to:
1) ask user to input decimal int and save int in a variable
2) convert the decimal number into binary and display the result

Thanks in advance
so that only the names beginning with the letter J are added to the Stack


You need to check for this
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
#include <iostream>
#include <string>
#include <queue>
#include <stack>

using namespace std;

int main()
{
    string name;
    queue<string> myQueue;
    stack<string> myStack;

    myQueue.push("Tom");
    myQueue.push("James");
    myQueue.push("Dick");
    myQueue.push("John");
    myQueue.push("Harry");

    cout << "Queue size: " << myQueue.size() <<'\n';
    cout << "FIFO queue order:\n";
    while (myQueue.size() > 0)
    {
        name = myQueue.front();
        cout << name << "\n";
        if(name[0] == 'J')
        {
            myStack.push(name);
        }
    myQueue.pop();
    }//while

    cout << "\nStack size: " << myStack.size() <<'\n';
    cout << "LIFO stack order:\n";
    while (myStack.size() > 0)
    {
        cout << myStack.top() << "\n";
        myStack.pop();
    }//while
}
Last edited on
Topic archived. No new replies allowed.