Hi. I am new to C++. But I am trying to write a basic program that uses the command line. But...
$ ./myscript -s 1 -e 8 -c billy -v 1 -o test
I get output like this...
Starting Length = 1 Ending Length = 8 Name = (null) Output File = (null) Verbose = 1
How come 'Name' and 'Output File' show (null)?
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
char *start_f;
char *end_f;
char *name_f;
char *out_f;
int verbose_f;
int help_f;
int arg;
while((arg = getopt(argc, argv, "s:e:n:o:vh")) != -1)
switch(arg) {
case('s'):
if(*optarg=='1'||*optarg=='2'||*optarg=='3'||*optarg=='4'||*optarg=='5'||*optarg=='6'||*optarg=='7'||*optarg=='8')
start_f = optarg;
else {
cout << "ERROR!" << endl;
return 0; }
break;
case('e'):
if(*optarg=='1'||*optarg=='2'||*optarg=='3'||*optarg=='4'||*optarg=='5'||*optarg=='6'||*optarg=='7'||*optarg=='8')
end_f = optarg;
else {
cout << "ERROR!" << endl;
return 0; }
break;
case('n'):
if(optarg=="billy")
name_f = optarg;
break;
case('o'):
out_f = optarg;
break;
case('v'):
verbose_f = 1;
break;
case('h'):
help_f = 1;
cout << "ERROR!" << endl;
return 0;
break;
default:
cout << "ERROR!" << endl;
return 0; }
printf("\nStarting Length = %s Ending Length = %s Name = %s Output File = %s Verbose = %d\n\n",
start_f, end_f, name_f, out_f, verbose_f);
return 0;
}