f3.cpp
1 /* 2 * FILE : f3.cpp 3 * PROJECT : SENG1000 - FOCUSED ASSIGNMENT 3 4 * PROGRAMMER : Tian Yang 5 * FIRST VERSION : 2024-02-01 6 * DESCRIPTION : 7 * This program calls a function to get a number from the user and displays whether the 8 * number is greater than or less than or equal to 1000. 9 */ 10 11 #include <stdio.h> 12 #include <stdbool.h> 13 14 int getNum(void); 15 bool isGreaterThan(int); 16 17 int main(int argc, char* argv[]) 18 { 19 printf("Please enter a number: "); 20 int num = getNum(); 21 22 if (isGreaterThan(num)) 23 { 24 printf("%d is greater than 1000.\n", num); 25 } 26 else 27 { 28 printf("%d is less than or equal 1000.\n", num); 29 } 30 31 return 0; 32 } 33 34 // 35 // FUNCTION : isGreaterThan 36 // DESCRIPTION : 37 // This function compares a number with 1000. 38 // PARAMETERS : 39 // int num : this num is compared with 1000. 40 // RETURNS : 41 // bool : if is num greater than 1000. 42 // 43 bool isGreaterThan(int num) 44 { 45 return num > 1000; // Check if the input number is greater than 1000. 46 } 47 48 // 49 // FUNCTION : getNum 50 // DESCRIPTION : 51 // This function gets user-entered number. 52 // PARAMETERS : 53 // void : nothing input. 54 // RETURNS : 55 // int : return the number that user entered. 56 // 57 #pragma warning(disable: 4996) // required by Visual Studio 58 int getNum(void) 59 { 60 /* the array is 121 bytes in size; we'll see in a later lecture how we can 61 improve this code */ 62 char record[121] = { 0 }; /* record stores the string */ 63 int number = 0; 64 /* NOTE to student: brace this function consistent with your others */ 65 /* use fgets() to get a string from the keyboard */ 66 fgets(record, 121, stdin); 67 /* extract the number from the string; sscanf() returns a number 68 * corresponding with the number of items it found in the string */ 69 if (sscanf(record, "%d", &number) != 1) 70 { 71 /* if the user did not enter a number recognizable by 72 * the system, set number to -1 */ 73 number = -1; 74 } 75 return number; 76 }