I've created a push-down stack and have a question. on line 83 is there a better way of recognizind that the first 5 letters are push( so i dont have to write out the entire thing in 5 conditions? i cant use strcmp because its not the whole string im comparing, just the first 5 letters. thank you!!
#include <iostream>
#include <cstring>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;
constint max = 100; // max number of digits in the stack
class pushPop
{
public:
void pop(); // pop the first number
void push(int number); // push on a new number in the first slot
void print(); // print out the stack
private:
staticint elements; // number of elements
int stack[max]; // stack of numbers
};
int pushPop::elements = 0;
void pushPop::print()
{
for (int i = 0; i < elements; i++)
cout << stack[i] << endl;
return;
}
void pushPop::pop()
{
if (elements == 0) // if 0 elements give an error and end the function
{
cout << "There are zero elements, cannot pop any more!" << endl;
return;
}
for (int i = 0; i < elements - 1; i++) // otherwise pop off the first element
stack[i] = stack[i + 1];
elements--;
return;
}
void pushPop::push(int number)
{ // if the maximum amout of numbers are already stored display an error and end the function
if (elements == max)
{
cout << "There are ten elements, cannot push any more!" << endl;
return;
}
int copyStack[max]; // otherwise push the new element in the first slot
for (int i = 1; i < elements + 1; i++)
copyStack[i] = stack[i - 1];
copyStack[0] = number;
for (int i = 0; i < elements + 1; i++)
stack[i] = copyStack[i];
elements++;
return;
}
int main(void)
{
pushPop stack;
char operation[max];
cout << "\t\t\tPUSH-DOWN STACK" << endl
<< "Enter \"print\" to view elements" << endl
<< "Enter \"pop\" to pop elements" << endl
<< "Enter \"push(number)\" to add elements" << endl
<< "Enter anything else to quit" << endl;
for (;;)
{
cin.getline(operation, max, '\n'); // get the users operation if the want to push, pop or print
if (strcmp(operation, "print") == 0)
stack.print();
elseif (strcmp(operation, "pop") == 0)
stack.pop();
elseif(operation[0] == 'p' && operation[1] == 'u' && operation[2] == 's' && operation[3] == 'h' && operation[4] == '(')
{
int number = 0; // extract the number from paranthese if the user wants to push
for (int i = 5; isdigit(operation[i]); i++)
{
number *= 10;
number += operation[i] - '0';
}
stack.push(number);
}
else
exit(1);
}
cout << endl;
return 0;
}
You are using C++, so I don't see why you don't use getline() and an std::string to read in the users command. You could then use find() to look for a substring.
If you really want to use C, I believe strstr() does what you are looking for.
#include <iostream>
#include <cstring>
#include <cctype>
using std::cout;
using std::endl;
using std::cin;
constint max = 100; // max number of digits in the stack
class pushPop
{
public:
void pop(); // pop the first number
void push(int number); // push on a new number in the first slot
void print(); // print out the stack
private:
staticint elements; // number of elements
int stack[max]; // stack of numbers
};
int pushPop::elements = 0;
void pushPop::print()
{
for (int i = 0; i < elements; i++)
cout << stack[i] << endl;
return;
}
void pushPop::pop()
{
if (elements == 0) // if 0 elements give an error and end the function
{
cout << "There are zero elements, cannot pop any more!" << endl;
return;
}
for (int i = 0; i < elements - 1; i++) // otherwise pop off the first element
stack[i] = stack[i + 1];
elements--;
return;
}
void pushPop::push(int number)
{ // if the maximum amout of numbers are already stored display an error and end the function
if (elements == max)
{
cout << "There are ten elements, cannot push any more!" << endl;
return;
}
int copyStack[max]; // otherwise push the new element in the first slot
for (int i = 1; i < elements + 1; i++)
copyStack[i] = stack[i - 1];
copyStack[0] = number;
for (int i = 0; i < elements + 1; i++)
stack[i] = copyStack[i];
elements++;
return;
}
int main(void)
{
pushPop stack;
char operation[max];
cout << "\t\t\tPUSH-DOWN STACK" << endl
<< "Enter \"print\" to view elements" << endl
<< "Enter \"pop\" to pop elements" << endl
<< "Enter \"push(number)\" to add elements" << endl
<< "Enter anything else to quit" << endl;
for (;;)
{
cin.getline(operation, max, '\n'); // get the users operation if the want to push, pop or print
if (strcmp(operation, "print") == 0)
stack.print();
elseif (strcmp(operation, "pop") == 0)
stack.pop();
elseif(strncmp(operation, "push(", 4) == 0)
{
int number = 0; // extract the number from paranthese if the user wants to push
for (int i = 5; isdigit(operation[i]); i++)
{
number *= 10;
number += operation[i] - '0';
}
stack.push(number);
}
else
exit(1);
}
cout << endl;
return 0;
}
why careful there, that was exactly what i needed and the program works now.
and whats all this hoobla about me mixing C and C++? what am i doing wrong lol :)
You are using C-style char arrays instead of C++ std::strings which are basically easier and safer. I was wondering what the point was when you clearly have access to C++ features.