[英]getopt not changing value C
此代碼應該使用 getopt 過濾學生列表。 它根據分數過濾 CS、數學和物理學生。 此 switch 案例中的所有內容都使用 getopt accept case 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;
}
撇開它是一個不完整的程序不談,讓你絆倒的事情是你把它放在了while
循環中。
argc -= optind;
argv += optind;
在while
循環之后移動它,我想你會發現它按預期工作。 default:
情況下的printf()
是不必要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.