Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include "errorProgramAST.h"
  2.  
  3. /*
  4.  
  5. */
  6.  
  7. int getNum(void)
  8. {
  9. /* the array is 121 bytes in size; we'll see in a later lecture how we can improve this code */
  10. char record[121] = { 0 }; /* record stores the string */
  11. int number = 0;
  12. int checkLoopValue = 0;
  13. int loop = 0;
  14. int errorLevel = 0;
  15. /* NOTE to student: indent and brace this function consistent with your others */
  16. /* use fgets() to get a string from the keyboard */
  17. fgets(record, 121, stdin);
  18. /* extract the number from the string; sscanf() returns a number
  19. * corresponding with the number of items it found in the string */
  20. checkLoopValue = strlen(record);
  21. if (checkLoopValue == 1)
  22. {
  23. errorLevel = 1;
  24. number = errorChecker(errorLevel);
  25. }
  26. else
  27. {
  28. while (loop <= checkLoopValue - 1)
  29. {
  30. if (isdigit(record[loop])) //Checking to see if the values in the strings are letters.
  31. {
  32. loop++;
  33. }
  34. else if (loop == checkLoopValue - 1) // If all characters have been checked, brek the loop.
  35. {
  36. break;
  37. }
  38. else if (record[loop] == '-')
  39. {
  40. errorLevel = 3;
  41. number = errorChecker(errorLevel);
  42. break;
  43. }
  44. else //If a value in the string is not a character, break the loop and return NULL.
  45. {
  46. errorLevel = 2;
  47. number = errorChecker(errorLevel);
  48. break;
  49. }
  50. }
  51. }
  52.  
  53. if (sscanf(record, "%d", &number) != 1 && number != NULL)
  54. {
  55. /* if the user did not enter a number recognizable by
  56. * the system, set number to NULL*/
  57. number = NULL;
  58. }
  59. return number;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement