'Why won't my C code complie when thier is no error shown?
Ok, so i was working on a code that would calauate the penalties for intoxicated driving having 3-90 days of peanlity for # of peanlites commited and the fine from $200-500. But i wanted to ask why is my code not compliing below? Any explanations on what i got wrong? Because an error wont show up...
//al = alcohol and n = warning number
void process_breathalyzer_results(double al, double n);
#include <stdio.h>
int main() {
process_breathalyzer_results(0.08, 1);
return 0;
}
void process_breathalyzer_results(double al, double n){
if (al < 0.05){
printf("PASS\n");
}else
if ((al >= 0.05) && (al < 0.08)){
if (n == 0){
printf("Invaild Input\n");
}else
if (n == 1){
printf("WARNING penalty: $200, driving suspension: 3
days\n");
}else
if (n == 2){
printf("WARNING penalty: $300, driving suspension: 7
days\n");
}else
if (n >= 3){
printf("WARNING penalty: $400, driving suspension: 30
days\n");
}else
if (al >= 0.08){
printf("FAIL penalty: $500, driving suspension:
90 days\n");
}
}
}
the PASS and WARNING works but the FAIL portion won't work....
Solution 1:[1]
Neither the first if nor the second if conditions are evaluated to logical true
if (al <= 0.05){
printf("PASS\n");
}else
if ((al > 0.05) && (al < 0.08))
for the passed arguments. As a result inner if statements do not get the control.
So the program outputs nothing.
Solution 2:[2]
in process_breathalyzer_results(0.08, 1); change values for the ones that prints something to console. example process_breathalyzer_results(0.06, 2); .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | WojciechCode |
