Skip to content

Commit 447654a

Browse files
committed
Input + a part of invalid input check done
1 parent e4816dc commit 447654a

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

For loop/52. Sum of GP series.c

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The numbers for the G.P. series:
1111
The Sum of the G.P. series : 93.000000
1212
1313
+ 1. Write tests (run tests function + body of a test)
14-
2. Input
14+
+ 2. Input
1515
3. Invalid input
1616
4. Finction to print out and find the sum
1717
5. Check the tests
@@ -21,17 +21,73 @@ The Sum of the G.P. series : 93.000000
2121

2222
#include <stdio.h>
2323

24+
enum is_valid_boolean{
25+
TRUE = 1,
26+
FALSE = 0
27+
};
28+
2429
void test(int first_number, int number_of_terms, int common_ratio, int expected_result, int test_number);
2530
void run_tests();
2631
int find_gp_series(int first, int number_of_terms, int common_ratio);
32+
int input_first_number(int *first_number);
33+
int input_number_of_terms(int *number_of_terms);
34+
int input_common_ratio(int *common_ratio);
35+
void print_invalid_input();
2736

2837
int main() {
29-
run_test();
38+
int first_number = 0;
39+
if (input_first_number(&first_number)) {
40+
int number_of_terms = 0;
41+
if (input_number_of_terms(&number_of_terms)) {
42+
int common_ratio = 0;
43+
if (input_common_ratio(&common_ratio)) {
44+
run_tests();
45+
} else {
46+
print_invalid_input();
47+
}
48+
} else {
49+
print_invalid_input();
50+
}
51+
} else {
52+
print_invalid_input();
53+
}
3054
return 0;
3155
}
3256

57+
int input_common_ratio(int *common_ratio) {
58+
int is_valid = TRUE;
59+
char endline = '\0';
60+
if (!scanf("%d%c", common_ratio, &endline) || endline != '\n') {
61+
is_valid = FALSE;
62+
}
63+
return is_valid;
64+
}
65+
66+
int input_number_of_terms(int *number_of_terms) {
67+
int is_valid = TRUE;
68+
char endline = '\0';
69+
if (!scanf("%d%c", number_of_terms, &endline) || endline != '\n') {
70+
is_valid = FALSE;
71+
}
72+
return is_valid;
73+
}
74+
75+
int input_first_number(int *first_number) {
76+
int is_valid = TRUE;
77+
char endline = '\0';
78+
if (!scanf("%d%c", first_number, &endline) || endline != '\n') {
79+
is_valid = FALSE;
80+
}
81+
return is_valid;
82+
}
83+
84+
void print_invalid_input() {
85+
printf("n/a");
86+
}
87+
3388
int find_gp_series(int first, int number_of_terms, int common_ratio) {
3489
int sum = 0;
90+
printf("%d%d%d",first, number_of_terms, common_ratio);
3591
return sum;
3692
}
3793

0 commit comments

Comments
 (0)