Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cctype>
- #include <cstdlib>
- using namespace std;
- bool ifNumbers(char array1[])
- {
- bool case1 = true;
- int k = 0;
- while (array1[k] != '\0'&& case1 == true)
- {
- if (isdigit(array1[k]))
- {
- case1 = true;
- }
- else
- {
- case1 = false;
- }
- k ++;
- }
- return case1;
- }
- bool ifLessthanzero(char array1[])
- {
- bool case1 = true;
- int a = 0;
- while (array1[a] != '\0'&& case1 == true)
- {
- if (isdigit(array1[a]))
- {
- case1 = true;
- }
- else if(array1[a] == '-')
- {
- case1 = false;
- }
- a ++;
- }
- return case1;
- }
- bool inBetween(int val)
- {
- bool case1 = true;
- if (val >= 1 && val <= 105)
- {
- case1 = true;
- }
- else
- {
- case1 = false;
- }
- return case1;
- }
- int main()
- {
- char nums[10000];
- cout <<"How many scores would you like to enter?: "<<endl;
- cin >> nums;
- int counts = atoi(nums);
- bool reminder = false;
- while(reminder == false)
- {
- if(!ifNumbers(nums))
- {
- cout << "Your input is invalid. Please enter a valid number: "<<endl;
- cin >> nums;
- counts = atoi(nums);
- }
- else if (!inBetween(counts))
- {
- cout << "The score is out of range. Please retry. "<<endl;
- cin >> nums;
- counts = atoi(nums);
- }
- else
- {
- reminder = true;
- }
- }
- char num[counts];
- int unit = 0;
- double total = 0;
- double number = 0;
- while (unit < counts)
- {
- cout << "Enter "<<unit+1<<": ";
- cin >> num;
- number = atoi(num);
- if(!ifLessthanzero(num))
- {
- cout<<"That was a negative number. Please retry."<<endl;
- num[unit] = '\0';
- unit --;
- }
- else if(!ifNumbers(num)) {
- cout<<"You entered character in the score. Please retry."<<endl;
- num[unit] = '\0';
- unit --;
- }
- else if(!inBetween(number))
- {
- cout<<"The score was out of range. Please retry."<<endl;
- num[unit] = '\0';
- unit --;
- }
- else
- {
- number = atoi(num); total = total +number;
- }
- unit ++;
- }
- double average = 0;
- average = total/counts;
- cout << "You entered "<<counts<<" scores."<<endl;
- cout << "The sum of the numbers you entered is "<<total<<"."<<endl;
- cout << "The average of the numbers you entered is "<<average<<"."<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement