Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "errorProgramAST.h"
- /*
- */
- int getNum(void)
- {
- /* the array is 121 bytes in size; we'll see in a later lecture how we can improve this code */
- char record[121] = { 0 }; /* record stores the string */
- int number = 0;
- int checkLoopValue = 0;
- int loop = 0;
- int errorLevel = 0;
- /* NOTE to student: indent and brace this function consistent with your others */
- /* use fgets() to get a string from the keyboard */
- fgets(record, 121, stdin);
- /* extract the number from the string; sscanf() returns a number
- * corresponding with the number of items it found in the string */
- checkLoopValue = strlen(record);
- if (checkLoopValue == 1)
- {
- errorLevel = 1;
- number = errorChecker(errorLevel);
- }
- else
- {
- while (loop <= checkLoopValue - 1)
- {
- if (isdigit(record[loop])) //Checking to see if the values in the strings are letters.
- {
- loop++;
- }
- else if (loop == checkLoopValue - 1) // If all characters have been checked, brek the loop.
- {
- break;
- }
- else if (record[loop] == '-')
- {
- errorLevel = 3;
- number = errorChecker(errorLevel);
- break;
- }
- else //If a value in the string is not a character, break the loop and return NULL.
- {
- errorLevel = 2;
- number = errorChecker(errorLevel);
- break;
- }
- }
- }
- if (sscanf(record, "%d", &number) != 1 && number != NULL)
- {
- /* if the user did not enter a number recognizable by
- * the system, set number to NULL*/
- number = NULL;
- }
- return number;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement