strange getopt_long problem: additional parameter gets stored only when other parameter is no integer

Hi,
in my test program below i use the getopt_long function for argument handling. i have a long option with a required argument that gets read in corectly in any case but when i use an integer argument instead of a floating point argument (i.e. 2 instead of 2.0) the remaining argument in the argv array is not get stored correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <sstream>
#include <getopt.h>

int main(int argc, char **argv) {
  int c, intvar;
  std::istringstream iss;
  std::string str;

  static struct option long_options[] = {
    {"test", required_argument, 0, 0},
    {NULL, 0, NULL, 0}
  };
  int option_index = 0;

  while ((c= getopt_long(argc, argv, "t", long_options, &option_index)) != -1)
    switch (c) {
      case 0:
        iss.str(optarg);
        iss >> intvar;
        break;
      default:
        std::cout<<"default";
    }
  }  
  if (optind >=argc || optind < (argc-1))
    std::cout<<"more or less than one additional argument given!"<<std::endl;
   else {
      iss.str(argv[optind]);
      iss >> str;
      std::cout<< "intvar=" << intvar << ", argv[optind]="
      << argv[optind] << ", str=" << str << std::endl;
   }
     
  return 0;
}


when i call the program this way:
./program --test=2.0 additional
i get this output
intvar=2, argv[optind]=additional, str=additional

but when i call it that way:
./program --test=2 additional
i get that output:
intvar=2, argv[optind]=additional, str=


as you can see, the only difference is the "2.0" in the first way and the "2" in the second.

But as you can see in the output, both the 2 or the 2.0 got correctly stored, and even the additional argument is correctly stored in argv[optarg] (in both cases).
So the problem is either
 
iss.str(argv[optind]);

or
 
iss >> str;


but i do not understand how the vartype in the test argument is affecting that.
Topic archived. No new replies allowed.