I'm currently having a problem with my code specifically the If-else part. If my year-level is 1, the if-else is satisfied but if the year-level I'll input is 2, 3 or 4, the if-else for these are satisfied HOWEVER, it always displays the if-else for yrep1 (It always prints "Year-level Representative: Lady Carmel") except if the year-level is 1.
int year, yrep1, yrep2, yrep3, yrep4;
char gov1[] = "Juan Dela Cruz";
...
char fiy1[] = "Lady Carmel";
printf("\nEnter Year Level: ");
scanf("%d", &year);
if(year==1){
printf("\n\n\t\t\t CANDIDATES FOR FIRST YEAR LEVEL REPRESENTATIVE ");
printf("\n\n1. Lady Carmel");
printf("\n\n2. Zia Alonzo");
printf("\n\nEnter your vote(Ex. 1): ");
scanf("%d", &yrep1);
printf("\nVote has been recorded!");\
}else if (year==2){
printf("\n\n\t\t\t CANDIDATES FOR SECOND YEAR LEVEL REPRESENTATIVE ");
printf("\n\n1. Lita Cordova");
printf("\n\n2. Mariel Geronimo");
printf("\n\nEnter your vote(Ex. 1): ");
scanf("%d", &yrep2);
printf("\nVote has been recorded!");
}else if (year==3){
printf("\n\n\t\t\t CANDIDATES FOR THIRD YEAR LEVEL REPRESENTATIVE ");
printf("\n\n1. Carmel De Leon");
printf("\n\n2. Helen Sy");
printf("\n\nEnter your vote(Ex. 1): ");
scanf("%d", &yrep3);
printf("\nVote has been recorded!");
}else if (year==4){
printf("\n\n\t\t\t CANDIDATES FOR FOURTH YEAR LEVEL REPRESENTATIVE ");
printf("\n\n1. Stephen Cortes");
printf("\n\n2. Olivia Yu");
printf("\n\nEnter your vote(Ex. 1): ");
scanf("%d", &yrep4);
printf("\nVote has been recorded!");
}
if(yrep1==1){
printf("\n\n\tYear-level Representative: %s", fiy1);
}else if(yrep1==2){
printf("\n\n\tYear-level Representative: %s", fiy2);
}
if(yrep2==1){
printf("\n\n\tYear-level Representative: %s", sy1);
}else if(yrep2==2){
printf("\n\n\tYear-level Representative: %s", sy2);
}
if(yrep3==1){
printf("\n\n\tYear-level Representative: %s", ty1);
}else if(yrep3==2){
printf("\n\n\tYear-level Representative: %s", ty2);
}
if(yrep4==1){
printf("\n\n\tYear-level Representative: %s", foy1);
}else if(yrep4==2){
printf("\n\n\tYear-level Representative: %s", foy2);
}
printf("\n\n\t\t\t\t\tBALOT RECEIPT");
printf("\n\n\tNAME: %s", name);
printf("\t\t\t\t\tDATE: %s", ctime(&t));
printf("\n\tYEAR LEVEL: %d", year);
printf("\n\n\tYEAR SECTION: %s", section);
printf("\n\n\t\t\tYOU'VE VOTED FOR THE FOLLOWING CANDIDATES:");
The problem is that these variables
int year, yrep1, yrep2, yrep3, yrep4;
are not initialized and have indeterminate values.
You need to initialize them for example with zeroes.
int year, yrep1 = 0, yrep2 = 0, yrep3 = 0, yrep4 = 0;
Another approach is to enclose if-else statements in another if statement as for example
if(year==1)
{
if(yrep1==1){
printf("\n\n\tYear-level Representative: %s", fiy1);
}else if(yrep1==2){
printf("\n\n\tYear-level Representative: %s", fiy2);
}
}