I have code which does the following :
const char* filename = argc >= 2 ? argv[1] : "stuff.jpg";
which reads in a photo as a command line argument and displays it.
I now want to take in two photo’s, I tried this code :
const char* filename = argc >= 3 ? argv[1] : "stuff.jpg", argv[2] : "tester.jpg";
But I get an error like this :
error: expected initializer before ‘:’ token
Anybody know whats wrong? is there a similer way to do this input programmatically?
You’re dealing with a ternary if-operator here. Have a look at this page. It’s basically an inline if-statement.
Code that would do what you’re looking for, looks something a little like this:
That leaves you with two filename variables, storing either the supplied argument or the default values (
stuff.jpgandtester.jpg, respectively).