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;
}
CodePudding user response:
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.