Hey,
I have a method that takes in the command line argument argv[1] and runs some checks on it - making sure it is not null, then making sure that it is an integer. However, we do not know at first what argv[1] is. It might be a string, null, or a character. This is where I am running into a problem - how do I appropriately declare my method to take in argv[1] when I don't know what type it is? Should I have my method take in a string, since that could cover everything, and then after the tests are done cast argv[1] as an int? That is what I have done so far (declare it as a string), but after I run the checks I am having trouble casting the string, which is no determined to be a number, as an int. Here is some code:
Code:
int main(int argc, char *argv[]) {
myStack stack1;
stack1.setStackSize(argv[1]);
Code:
class myStack {
public:
int stackSize;
void setStackSize(string);
int displayStackSize();
};
Code:
void myStack::setStackSize(string carg) {
int arg_length;
char current_digit;
if(carg == "") {
cerr << "Bad command line argument.\n";
exit(1);
}
arg_length = carg.length();
for(int ii = 0; ii < arg_length; ii++) {
current_digit = carg[ii];
if(!(isdigit(current_digit))) {
cerr << "Bad command line argument.\n";
exit(1);
}
}
stackSize = atoi(carg);
}
I am getting this error:
40 N:\USERS\bakewens\CS240\myStack.cpp cannot convert `std::string' to `const char*' for argument `1' to `int atoi(const char*)'
on my line stacksize = atoi(carg)