Sep 1, 2014 at 12:34am UTC
Here is my code:
#include <iostream>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
int main(int argc, char **argv)
{
char *nvalue = "World";
int c=0;
int tvalue = -1;
while ((c = getopt (argc, argv, "nt:")) != -1)
switch(c)
{
case 'n':
nvalue = optarg;
break;
case 't':
tvalue = atoi(optarg);
break;
}
for (int i = 0; i < tvalue; i++)
{
cout<<"["<<i+1<<"] Hello "<<nvalue<<"!\n";
}
return 0;
}
when I typed in cmd: ./hello -t 3 it should appear:
[1] Hello World!
[2] Hello World!
[3] Hello World!
and when type: ./hello -t 3 -n Bob it should appear:
[1] Hello Bob!
[2] Hello Bob!
[3] Hello Bob!
however, it is not working. any help please?
Last edited on Sep 1, 2014 at 12:50am UTC
Sep 1, 2014 at 1:41am UTC
It worked well if i type ./hello -t 3. But if i add -n bob, it doesnt work
Sep 1, 2014 at 1:46am UTC
Repeat: not "nt:"
, but "n:t:"
(colon after n and after t )
Sep 1, 2014 at 1:48am UTC
I did change it and it is still now working