Problems with Stacks
Nov 26, 2015 at 4:23am UTC
Hi I could use some help. Everything seems to work only for the first value. But it doesn't display the other values correctly until I pop a value. Just a hint would be appreciated Thanks.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#include <iostream>
#include <stdlib.h>
#include <string>
#include <exception>
using namespace std;
const int MAX = 5; //Max Array Size
class Errors {
/* Desc: Exception Class Used To return Messages On Following Error
Functions: Two Constructors That Sets The Message And A what function that returns the message*/
public :
Errors() {
message = "Error: Invalid Menu Option" ;
}
Errors(int code){
if (code == 0){
message = "Error: Stack is Empty" ;
}
else if (code == 1){
message = "Error: Stack is Full" ;
}
else {
message = "Error: Invalid Input" ;
}
}
string what () {
return message;
}
private :
string message;
};
class myStack{
/* Desc: Stack Class
One Constructor That Intialize The stackPointer at 0
Functions: Push- Adds Values To The Stack, Pop- Removes From The Stack, Disp- Displays Menu And Stack */
public :
myStack() {
stackPointer = 0;
}
~myStack(){}
void push(int values){
stack[stackPointer]=values;
stackPointer++;
}
int pop(){
stackPointer--;
return stack[stackPointer+1];
}
void disp(){
cout << "\nStacks: " ;
for (int i=0;i<stackPointer;i++){
if (i == 0) {
cout << stack[0];
}
else {
cout << ", " << stack[stackPointer];
}
}
cout << "\nMenu: " << endl;
cout << "1. Push Element" << endl;
cout << "2. Pop Element" << endl;
cout << "3. Exit" << endl;
}
int size(){
return stackPointer;
}
private :
int stackPointer;
int stack[MAX];
};
int main(){
myStack stack1; //Class Object
int option; //Menu Option
int values; //Value To be Pushed
stack1.disp();
while (option != 3){
try {
cout <<"Enter: " ;
cin >> option;
if (option == 1) {//Catches Menu Option 1
try {
cout << "Enter Value: " ;
cin >> values;
if (cin.fail()){ //Invalid Values
cin.clear();
cin.ignore(1000000,'\n' );
throw Errors(2);
}
if (stack1.size()== MAX){ //If The Stack is At Max Size
cin.clear();
cin.ignore(1000000,'\n' );
throw Errors(1);
}
else {
stack1.push(values);
}
}
catch (Errors catch1){
cout << catch1.what() << endl;
}
}
else if (option == 2) { //Catches Menu Option 2
try {
if (stack1.size()== 0){ //If Stack is Empty
throw Errors(0);
}
else {
cout << "Popped Element: " << stack1.pop() << endl;
}
}
catch (Errors catch2){
cout << catch2.what() << endl;
}
}
else if (option == 3) { //Catches Menu Option 3
cout << "Exiting..." << endl;
return 0;
}
else { //Any Other Invalid Menu Option
cin.clear();
cin.ignore(1000000,'\n' );
throw Errors();
}
}
catch (Errors catch3){ //Exception For Invalid Menu
cout << catch3.what() << endl;
}
stack1.disp();
}
}
Nov 26, 2015 at 8:24am UTC
The loop on line 54 is simply:
1 2 3 4 5 6 7 8
for (int i=0;i<stackPointer;i++){
if (i == 0) {
}
else {
cout << ", " << stack[stackPointer]; // stackPointer is beyond the end
}
cout << stack[i ]; // Note: i is the index of an element of the array
}
Last edited on Nov 26, 2015 at 8:26am UTC
Nov 27, 2015 at 3:29am UTC
Wow thank you.
Topic archived. No new replies allowed.