-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx_pattern.c
More file actions
62 lines (50 loc) · 1.5 KB
/
x_pattern.c
File metadata and controls
62 lines (50 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
// Function to clear input buffer
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
// Function to clear screen in a more portable way
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
printf("\n");
}
int main(void)
{
int row, column, height;
char choice = ' ';
do {
clearScreen();
printf("Please enter the height of X (positive integer): ");
// Input validation with error handling
if (scanf("%d", &height) != 1) {
printf("\a\nError: Invalid input. Please enter a number.\n");
clearInputBuffer();
continue;
}
clearInputBuffer(); // Clear any remaining input
printf("\n");
if (height > 0) {
// Print the X pattern
for (row = 1; row <= height; row++) {
for (column = 1; column <= height; column++) {
if (row == column || column == height - row + 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
} else {
printf("\a\nError: Please enter a positive integer.\n");
}
printf("\nPress 'q' to quit or any other key followed by Enter to continue: ");
choice = getchar();
} while (choice != 'q' && choice != 'Q');
return 0;
}