MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / main

Function main

Examples/NoModules/Chapter 17/Ex17_04B/Ex17_04B.cpp:9–47  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7#include <string>
8
9int main()
10{
11 std::string words[]{ "The", "quick", "brown", "fox", "jumps" };
12 Stack<std::string> wordStack; // A stack of strings
13
14 for (const auto& word : words)
15 wordStack.push(word);
16
17 Stack<std::string> newStack{ wordStack }; // Create a copy of the stack
18
19 // Display the words in reverse order
20 while (!newStack.isEmpty())
21 std::cout << newStack.pop() << ' ';
22 std::cout << std::endl;
23
24 // Reverse wordStack onto newStack
25 while (!wordStack.isEmpty())
26 newStack.push(wordStack.pop());
27
28 // Display the words in original order
29 while (!newStack.isEmpty())
30 std::cout << newStack.pop() << ' ';
31 std::cout << std::endl;
32
33 std::cout << std::endl << "Enter a line of text:" << std::endl;
34 std::string text;
35 std::getline(std::cin, text); // Read a line into the string object
36
37 Stack<const char> characters; // A stack for characters
38
39 for (size_t i{}; i < text.length(); ++i)
40 characters.push(text[i]); // Push the string characters onto the stack
41
42 std::cout << std::endl;
43 while (!characters.isEmpty())
44 std::cout << characters.pop(); // Pop the characters off the stack
45
46 std::cout << std::endl;
47}

Callers

nothing calls this directly

Calls 3

pushMethod · 0.45
isEmptyMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected