Ok, I broke my program down into three files, but I am getting some compiler errors. First, my files:
myStack.h
Code:
/* myStack.h */
#include <string>
class myStack
{
public:
string (*aa);
int stackSize;
int cc;
void setStackSize(int);
void pop(int);
void push(int);
int displayStackSize();
};
myStack.cpp
Code:
#include <iostream>
#include <string>
#include "myStack.h"
using namespace std;
void myStack::setStackSize(int x)
{
stackSize = x;
if(!(aa = new(nothrow) string[stackSize+1]))
cerr << "Program cannot handle a stack of size " << stackSize << ".\n";
}
void myStack::pop(int p) {
if(p == 0)
cout << "Empty stack.\n";
else
cout << aa[p-1] << "\n";
}
void myStack::push(int p)
{
if(p == stackSize)
{
cerr << "Full stack.\n";
}
else
{
aa[p] = aa[p].substr(1, aa[p].length());
cc++;
}
}
stackDriver.cpp
Code:
#include <iostream>
#include <string>
#include <ctype.h>
#include <stdlib.h>
#include "myStack.h"
using namespace std;
int main(int argc, char *argv[]) {
myStack stack1;
int arg_length;
char current_digit;
if(argc < 2) {
cerr << "Bad command line argument.\n";
exit(1);
}
arg_length = strlen(argv[1]);
if(arg_length > 9) {
cerr << "Bad command line argument.\n";
exit(1);
}
for(int ii = 0; ii < arg_length; ii++) {
current_digit = argv[1][ii];
if(!(isdigit(current_digit))) {
cerr << "Bad command line argument.\n";
exit(1);
}
}
stack1.setStackSize(atoi(argv[1]));
stack1.cc = 0;
while(1 != 2) { // infinite loop
getline(cin, stack1.aa[stack1.cc]);
if(stack1.aa[stack1.cc].length() > 80)
cerr << "Bad input.\n";
else if(stack1.aa[stack1.cc][0] == '<')
stack1.pop(stack1.cc);
else if(stack1.aa[stack1.cc][0] == '>')
stack1.push(stack1.cc);
else if(stack1.aa[stack1.cc].length() == 0)
exit(0);
else
cerr << "Bad input.\n";
}
system("Pause");
return 0;
}
Sorry for the long code postings, but I thought you would probably want to see the whole thing. My compiler errors are this:
Code:
3 N:\USERS\bakewens\CS240\Program 2\myStack.cpp In file included from N:\USERS\bakewens\CS240\Program 2\myStack.cpp
7 N:\USERS\bakewens\CS240\Program 2\myStack.h ISO C++ forbids declaration of `string' with no type
7 N:\USERS\bakewens\CS240\Program 2\myStack.h expected `;' before '(' token
and then a lot more, but they all have to do with not having aa initialized, and that is because of the declaration of 'string' with no type error.
So, two questions: first, of course, what is the compiler complaining about "ISO C++ forbids a declaration of 'string' with no type'? What does that mean?
Second, is my file structure finally correct? :-p
Thanks for all of the help!