I'm new to C++, and I do not understand stacks that well. I tried following some tutorials to sort Stack1 using recursion, but nothing has been working or it is solely focused on arrays containing integers. I also tried sorting the Names array directly, but it does not work. How do I go about sorting my Names array for my Stack1 so in a non-complex manner (since I am new and do not understand it that well)?
Here's my code:
//Necessary libraries
#include <iomanip>
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
//Main CPP Function
int main() {
//Name and Surname
std::string Names[] = { "Sam", "John", "Simon", "Sarah", "Mat", "Nick", "Isaac", "Anna", "Daniel", "Aaron", "Jack", "Kathrine " };std::sort(std::begin(Names), std::end(Names));
std::string Surnames[] = { "Williams", "Phoenix", "Johnson", "Khosa", "Jackon", "Roberts", "Wayne", "Mishima", "Rose", "Black", "Mohamed", "Bruckner" };
//Score Array
int Score[] = { 60, 85, 75, 81, 38, 26, 74, 34, 64, 83, 27, 42 };
//Variable decleration needed for if statements
int l;
int k;
//Calculates array size
l = sizeof(Names) / sizeof(Names[0]);
k = sizeof(Surnames) / sizeof(Surnames[0]);
//------------------------------- Stack One --------------------------------------//
//var declaration
stack <string> Stack1;
stack <int> Score1;
cout << "Stack One" << endl;
//sort stack one alphabetically by name
//whilst our Names array is not empty, we will push it into the stack
for (int i = 0; i < l; i++) {
//pushes Names, Surnames and Scores to first stack
Stack1.push(Names[i]);
Stack1.push(Surnames[i]);
Score1.push(Score[i]);
//prints Names, Surnames and Scores for the first stack
while (!Stack1.empty()){
cout << "\t" << setw(20) << Stack1.top() << "\t" << Score1.top() << endl;
//stops the printing from printing in an endless loop
Stack1.pop();
Score1.pop();
}
}
return 0;
}
As mentioned in the comments, it is required by an assignment that I sort my stacks. Here is the assignment text for more understanding:
