Skip to content

Commit cc4915b

Browse files
committed
Input and invalid input done
1 parent 4cc7878 commit cc4915b

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

For loop/51. Octal to decimal.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,45 @@ The equivalent Decimal Number : 485
99
1010
+ 1. Figure out howo to convert octal to decimal
1111
745 = (7 * 8^2) + (4 * 8^1) + (5 * 8^0) = 485
12-
1.5 Input
12+
+ 1.5 Input
1313
2. Pow function
1414
3. Function to count amount of digits
1515
4. Write function to iterate over digits and return sum of powers
16-
5. Invalid input
16+
+ 5. Invalid input
1717
6. Test
1818
7. Cpplint test
1919
8. Add and push
2020
*/
2121

2222
#include <stdio.h>
23-
23+
24+
enum boolean_for_validation {
25+
TRUE = 1,
26+
FALSE = 0
27+
};
28+
29+
int input_octal(int *octal);
30+
void print_invalid_input();
31+
2432
int main() {
25-
33+
int octal = 0;
34+
if (input_octal(&octal)) {
35+
printf("%d", octal);
36+
} else {
37+
print_invalid_input();
38+
}
2639
return 0;
2740
}
41+
42+
void print_invalid_input() {
43+
printf("n/a");
44+
}
45+
46+
int input_octal(int *octal) {
47+
int is_valid = TRUE;
48+
char endline = '\0';
49+
if (!scanf("%d%c", octal, &endline) || endline != '\n') {
50+
is_valid = FALSE;
51+
}
52+
return is_valid;
53+
}

0 commit comments

Comments
 (0)