getopt不更改价值c

发布于 2025-02-08 19:10:45 字数 1761 浏览 2 评论 0原文

此代码应该使用GetOpt过滤学生列表。它根据他们的观点过滤的CS,数学和物理学生。 此开关情况中的所有内容都使用GetOpt Accept Case V有效,该案例V仅将值设置为1并过滤学生列表,因此只有CS和数学学生才会在列表中。我不知道为什么它不起作用。我在书籍中发现了多个示例,也以同样的方式进行。

对不起,我知道代码很丑陋,我试图使帖子更好。

#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;
}

This code is supposed to filter a list of students using getopt. It filter's CS, Math and Physics students based on their points.
Everything in this switch case works using getopt accept case v which is just supposed to set the value to 1 and filter the list of students so only CS and Math students will be in the list. I don't know why it wouldn't work. I've found multiple examples in books doing it the same way.

I'm sorry I know the code is ugly I tried to make it better for the post.

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

留一抹残留的笑 2025-02-15 19:10:45

抛开这是一个不完整的程序,绊倒您的事情是,您将其放入while循环中。

    argc -= optind;
    argv += optind;

循环之后,在之后移动它,我想您会发现它的工作原理。 printf()默认值中:案例是不必要的。

Setting aside that it's an incomplete piece of program, the thing that is tripping you up is that you've put this inside the while loop.

    argc -= optind;
    argv += optind;

Move it after the while loop and I think you'll find it works as intended. The printf() in the default: case is unnecessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文