I was working on an assignment for class, and I think I got the program working properly, but now I would like to make some modifications to it just to better understand assert. The code is below -
#include <iostream>
#include <stdlib.h>
#include <assert.h>
using namespace std;
// Sample program that shows how command line arg works, in Unix g++
// Note argc and argv
// Also shows use of system call, that can launch any program
// system launches 'ls' to display files in the dir
void runAssert(int);
int main(int argc, char *argv[])
{
cout << "Number of inputs: " << argc << endl;
cout << "1st argument: " << argv[0] << endl;
system ("ls");
cout << "hello world" << endl;
runAssert(argc);
return 0;
}
void runAssert(int argc)
{
assert(argc > 4);
}
So the program is supposed to keep track of the arguments passed into main through command line. The professor specified that it should take 4 arguments. This code works, as far as I can tell, but I don't know what 4 commands to pass it? I do g++ assignment.cpp -o assignment
and then ./assignment
-- But this last command only counts as one argument so the assert triggers. If I change the function to >= 1
then it works.
Another question I have is, how can I make it display an error message when it doesn't meet the requirements?
I have tried assert("Not the right amount of arguments", argc > 4)
but then I get an error message about too many arguments being passed into main.
Thanks for any help, and sorry if my formatting is wrong. First time posting.