1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstring>
using namespace std;
int charToInt(char numberString, int LENGTH);
int calcSum(int numArray[], int count1);
int charToHighest(int numArray[], int count1);
int charToLowest(int numArray[], int count1);
bool findSpace(char numberString, int count1, bool valid);
int main()
{
const int LENGTH = 40;
char numberString[LENGTH];
int numArray[LENGTH];
int spaceCounter = 0;
int sum = 0;
int count1 = 0;
int highest = 0;
int lowest = 0;
bool valid = true;
do
{
cout << "Enter a series of digits with no spaces between them.\n";
cin.clear();
cin.ignore();
cin.getline(numberString, LENGTH);
while(numberString[count1] != '\0' && valid)
{
numArray[count1] = charToInt(numberString[count1], LENGTH);
count1++;
}
}while(!valid);
sum = calcSum(numArray, count1);
highest = charToHighest(numArray, count1);
lowest = charToLowest(numArray, count1);
cout << "The sum of those digits is " << sum << "\n";
cout << "The highest digit is " << highest << "\n";
cout << "The lowest digit is " << lowest;
return 0;
}
int charToInt(char numberString, int LENGTH)
{
int num = 0;
int newLength = sizeof(numberString);
for(int lengthCount = 0; lengthCount < newLength; lengthCount++)
{
if(isdigit(numberString))
{
// Char math to convert char to int
num = numberString - '0';
}
else
{
cout << "Incorrect input....?";
cout << endl;
cout << "Enter a series of digits with no spaces between them.\n";
cin.getline(numberString, LENGTH);
return num;
}
}
return num;
}
int calcSum(int numArray[], int count1)
{
int sum = 0;
for(int count2 = 0; count2 < count1; count2++)
{
sum += numArray[count2];
}
return sum;
}
int charToHighest(int numArray[], int count1)
{
int highest = numArray[0];
for(int highCount = 0; highCount < count1; highCount++)
{
if(numArray[highCount] > highest)
{
highest = numArray[highCount];
}
}
return highest;
}
int charToLowest(int numArray[], int count1)
{
int lowest = numArray[0];
for(int lowCount = 0; lowCount < count1; lowCount++)
{
if(numArray[lowCount] < lowest)
{
lowest = numArray[lowCount];
}
}
return lowest;
}
|