-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd.c
49 lines (37 loc) · 1.13 KB
/
add.c
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
#include <stdio.h>
#include <stdlib.h>
// Function to validate user input
int getNumber(const char *prompt) {
char buffer[256];
char *end;
long number;
// Prompt the user for input
printf("%s", prompt);
// Read the user's input
if (!fgets(buffer, sizeof(buffer), stdin)) {
// If fgets fails, exit the program
// fprintf(stderr, "Error: Unable to read user input\n");
exit(EXIT_FAILURE);
}
// Convert the user's input to a number
number = strtol(buffer, &end, 10);
// Check if the user's input was a valid number
if (end == buffer || *end != '\n') {
fprintf(stderr, "Error: Invalid input\n");
exit(EXIT_FAILURE);
}
// Return the number
return (int)number;
}
int main() {
int num1, num2, sum;
// Get the first number from the user
num1 = getNumber("Enter the first number: ");
// Get the second number from the user
num2 = getNumber("Enter the second number: ");
// Calculate the sum of the two numbers
sum = num1 + num2;
// Print the result
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}