I can see no syntactical or logical errors. Everything compiles fine. The problem?
My evaluate function won't evaluate any commands, except for "exit" which exits the program as it should. Even if I just make the case for "push" or "commands" a single cout line, it doesn't work. I'm baffled here. Does anyone have any idea what the problem is? It was working earlier, but now it's not, and I don't know why.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cassert>
#include "intstack.h"
usingnamespace std;
void command_list();
void evaluate(string com, IntStack* st);
IntStack create_stack(char capans, int cap);
int main()
{
string command;
//int st_num = 0;
char capans = 'y';
int cap;
cout << "Welcome to the stack program. To begin with, a stack must be created. \n";
cout << "Would you like to use the default capacity of 100? y/n \n";
cin >> capans;
if (capans == 'n'){
cout << "Enter capacity: \n";
cin >> cap;
}
IntStack st = create_stack(capans, cap);
int arr_size;
cout << "Now an array of integers needs to be created. How many integers will this array have? \n";
cin >> arr_size;
int arr[arr_size];
int el;
for (int i = 0; i < arr_size; i++){
cout << "Enter element " << i << " of the array \n";
cin >> el;
arr[i] = el;
}
cout << "Here are your command choices: \n";
cout << "push //to push ints to stack \n";
cout << "pop //to pop off elements \n";
cout << "top //to get top element of stack \n";
cout << "print //to print a stack \n";
cout << "size //to get the size of a stack \n";
cout << "capacity //to see the capacity fo a stack \n";
cout << "reset //reset stack \n";
cout << "commands //to display this command list \n";
cout << "exit //to exit the program \n\n";
//int result;
while(1==1){
cout << "> ";
cin >> command;
evaluate(command, &st);
}
return 0;
}
Line 144 of intstack.cpp: this block runs when com!="exit", not when it is different to each of the other conditions that were just evaluated, thus it always overwrites the contents of c except when com=="exit".
For example, if com=="size", the block on line 139 will run, correctly setting c to 4, and then the one on line 144 will run, setting it to 9.