getopt不更改价值c
此代码应该使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抛开这是一个不完整的程序,绊倒您的事情是,您将其放入
while
循环中。循环之后,在
之后移动它,我想您会发现它的工作原理。
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.Move it after the
while
loop and I think you'll find it works as intended. Theprintf()
in thedefault:
case is unnecessary.