Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <cctype>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. bool ifNumbers(char array1[])
  9. {
  10. bool case1 = true;
  11. int k = 0;
  12.  
  13. while (array1[k] != '\0'&& case1 == true)
  14. {
  15. if (isdigit(array1[k]))
  16.     {
  17. case1 = true;
  18.     }
  19. else
  20.     {
  21. case1 = false;
  22.     }
  23. k ++;
  24. }
  25.  
  26. return case1;
  27. }
  28.  
  29.  
  30. bool ifLessthanzero(char array1[])
  31. {
  32.  
  33. bool case1 = true;
  34. int a = 0;
  35. while (array1[a] != '\0'&& case1 == true)
  36.     {
  37. if (isdigit(array1[a]))
  38.         {
  39. case1 = true;
  40.         }
  41. else if(array1[a] == '-')
  42.         {
  43. case1 = false;
  44.         }
  45. a ++;
  46.     }  
  47. return case1;
  48. }
  49.  
  50.  
  51. bool inBetween(int val)
  52. {
  53.  
  54. bool case1 = true;
  55. if (val >= 1 && val <= 105)
  56. {
  57. case1 = true;
  58. }
  59. else
  60. {
  61. case1 = false;
  62. }
  63. return case1;
  64. }
  65.  
  66.  
  67. int main()
  68. {
  69.  
  70. char nums[10000];
  71. cout <<"How many scores would you like to enter?: "<<endl;
  72. cin >> nums;
  73.  
  74. int counts = atoi(nums);
  75.  
  76. bool reminder = false;
  77. while(reminder == false)
  78. {
  79. if(!ifNumbers(nums))
  80. {
  81. cout << "Your input is invalid. Please enter a valid number: "<<endl;
  82. cin >> nums;
  83. counts = atoi(nums);
  84. }
  85. else if (!inBetween(counts))
  86. {
  87. cout << "The score is out of range. Please retry. "<<endl;
  88. cin >> nums;
  89. counts = atoi(nums);
  90. }
  91. else
  92.     {
  93. reminder = true;
  94.     }
  95. }
  96. char num[counts];
  97.  
  98. int unit = 0;
  99. double total = 0;
  100. double number = 0;
  101.  
  102. while (unit < counts)
  103. {
  104. cout << "Enter "<<unit+1<<": ";
  105. cin >> num;
  106. number = atoi(num);
  107.  
  108. if(!ifLessthanzero(num))
  109. {
  110. cout<<"That was a negative number. Please retry."<<endl;
  111. num[unit] = '\0';
  112. unit --;
  113. }
  114. else if(!ifNumbers(num)) {
  115. cout<<"You entered character in the score. Please retry."<<endl;
  116. num[unit] = '\0';  
  117. unit --;
  118. }
  119. else if(!inBetween(number))
  120. {
  121. cout<<"The score was out of range. Please retry."<<endl;
  122. num[unit] = '\0';  
  123. unit --;
  124. }
  125. else
  126. {
  127. number = atoi(num); total = total +number;
  128. }
  129. unit ++;
  130. }
  131.  
  132. double average = 0;
  133.  
  134. average = total/counts;
  135.  
  136. cout << "You entered "<<counts<<" scores."<<endl;
  137. cout << "The sum of the numbers you entered is "<<total<<"."<<endl;
  138. cout << "The average of the numbers you entered is "<<average<<"."<<endl;
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement