converting from int to enum

后端 未结 4 1338
渐次进展
渐次进展 2021-01-20 08:31

I have declared the following enum :

  enum periods {one, five, ten, fifteen, thirty};

and now I want to pass it as a commandline argument

4条回答
  •  一生所求
    2021-01-20 09:18

    At a random guess, what you really want is something more like:

    periods mp;
    if (argc < 2) {
        mp=one; // default value
    } else if (strcmp(argv[1], "one")==0) {
        mp=one;
    } else if (strcmp(argv[1], "five")==0) {
        mp=five;
    } else if (strcmp(argv[1], "ten")==0) {
        mp=ten;
    } else if (strcmp(argv[1], "fifteen")==0) {
        mp=fifteen;
    } else if (strcmp(argv[1], "thirty")==0) {
        mp=thirty;
    } else {
        fprintf(stderr, "I can't possibly comprehend periods = '%s'\n", argv[1]);
        return -1;
    }
    

提交回复
热议问题