Skip to content

Commit 2c6920a

Browse files
committed
Completed invalid input. Tests are on their way
1 parent 06ae248 commit 2c6920a

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

For loop/49. Find the sum of an A.P. series.c

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The Sum of the A.P. series are :
1010
1 + 5 + 9 + 13 + 17 + 21 + 25 + 29 + 33 + 37 = 190
1111
1212
+ 1. Take input
13-
1.5 Invalid input - To be done later
13+
+ 1.5 Invalid input
1414
+ 2. Write a function to find elements
1515
+ 3. Output sum
1616
4. Test
@@ -20,19 +20,39 @@ The Sum of the A.P. series are :
2020

2121
#include <stdio.h>
2222

23-
int input_start();
24-
int input_number_of_items();
25-
int input_difference();
23+
enum is_valid {
24+
TRUE = 1,
25+
FALSE = 0
26+
};
27+
28+
int input_start(int *start);
29+
int input_number_of_items(int *number_of_items);
30+
int input_difference(int *difference);
2631
void find_and_print_result(int start, int number_of_items, int difference);
32+
void print_invalid_input();
2733

2834
int main() {
29-
int start = input_start();
30-
int number_of_items = input_number_of_items();
31-
int difference = input_difference();
32-
find_and_print_result(start, number_of_items, difference);
35+
int start = 0, number_of_items = 0, difference = 0;
36+
if (input_start(&start)) {
37+
if (input_number_of_items(&number_of_items)) {
38+
if (input_difference(&difference)) {
39+
find_and_print_result(start, number_of_items, difference);
40+
} else {
41+
print_invalid_input();
42+
}
43+
} else {
44+
print_invalid_input();
45+
}
46+
} else {
47+
print_invalid_input();
48+
}
3349
return 0;
3450
}
3551

52+
void print_invalid_input() {
53+
printf("n/a");
54+
}
55+
3656
void find_and_print_result(int start, int number_of_items, int difference) {
3757
printf("The Sum of the A.P. series are:\n");
3858
int sum = start, element = start;
@@ -45,23 +65,32 @@ void find_and_print_result(int start, int number_of_items, int difference) {
4565
printf("= %d", sum);
4666
}
4767

48-
int input_start() {
68+
int input_start(int *start) {
69+
int is_valid = TRUE;
70+
char endline = '\0';
4971
printf("Input the starting number of the A.P. series:\n");
50-
int start;
51-
scanf("%d", &start);
52-
return start;
72+
if (!scanf("%d%c", start, &endline) || endline != '\n') {
73+
is_valid = FALSE;
74+
}
75+
return is_valid;
5376
}
5477

55-
int input_number_of_items() {
78+
int input_number_of_items(int *number_of_items) {
79+
int is_valid = TRUE;
80+
char endline = '\0';
5681
printf("Input the number of items for the A.P. series:\n");
57-
int number_of_items;
58-
scanf("%d", &number_of_items);
59-
return number_of_items;
82+
if (!scanf("%d%c", number_of_items, &endline) || endline != '\n' || *number_of_items == 0) {
83+
is_valid = FALSE;
84+
}
85+
return is_valid;
6086
}
6187

62-
int input_difference() {
88+
int input_difference(int *difference) {
89+
int is_valid = TRUE;
90+
char endline = '\0';
6391
printf("Input the common difference of A.P. series:\n");
64-
int difference;
65-
scanf("%d", &difference);
66-
return difference;
92+
if (!scanf("%d%c", difference, &endline) || endline != '\n') {
93+
is_valid = FALSE;
94+
}
95+
return is_valid;
6796
}

0 commit comments

Comments
 (0)