代码之家  ›  专栏  ›  技术社区  ›  ZenBa2

getopt未更改值C

  •  0
  • ZenBa2  · 技术社区  · 2 年前

    这段代码应该使用getopt过滤学生列表。它根据学生的分数筛选CS、数学和物理学生。 这个switch case中的所有内容都使用getopt accept case v工作,它只需要将值设置为1,并过滤学生列表,以便只有CS和Math学生会在列表中。我不知道为什么它不起作用。我在书中找到了许多这样做的例子。

    很抱歉,我知道代码很难看,我试着把它做得更好。

    #include <stdio.h>
    #include <stdlib.h>
    #include <getopt.h> 
    #include "zulassung.h"
    
    int main(int argc, char *argv[]){
    
    int percentageNeededToPassCS = 50; 
    int percentageNeededToPassSecondSubject = 50;
    int passingMinCS = 0;
    int passingMinSecongSubject = 0;
    int maxPoints = atoi(argv[1]);
    int points;
    
    int studentID;
    
    int CheckV = 0;
    
    char subject[20];
    char option;
    
    
    while((option = getopt(argc, argv,"i:m:v")) != EOF){
        switch(option){
            case 'i':
            percentageNeededToPassCS = atoi(optarg);
            break;
            case 'm':
            percentageNeededToPassSecondSubject = atoi(optarg);
            break;
            case 'v':
            checkV = 1;
            break;
            default: //The default dosen't work yet it's not needed
            printf("Unknown Option: '%s'\n", optarg);
            break;
        }
        argc -= optind;
        argv += optind;
    }
    
    passingMinCS = calculate_pointsneededtopass(maxPoints,percentageNeededToPassCS);
    passingMinSecongSubject=calculate_pointsneededtopass(maxPoints,percentageNeededToPassSecondSubject);
    
    printf("\t %d\n", CheckV);
    while(scanf("%d %s %d", &studentID, subject, &points) == 3){
        if(checkV == 1){
            if(subject_is_cs_or_math(subject) == 1){
            if(filter_student(passingMinCS,passingMinSecongSubject,checkV,studentID,subject,points) == 1){
            printf("%d\n", studentID);
                }
            }
        }else{
        if(filter_student(passingMinCS,passingMinSecongSubject,checkV,studentID,subject,points) == 1){
            printf("%d\n", studentID);
        }
        }
    }
    
    return 0;
    }
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   Erwin    2 年前

    撇开它是一个不完整的程序不谈,绊倒你的是你把它放在了 while

        argc -= optind;
        argv += optind;
    

    将其移动到 虽然 循环,我想你会发现它按预期工作。这个 printf() default: 不需要案例。