Saturday, April 22, 2017

Programming Challenge 10.8 - Sum Of Digits

/* Sum Of Digits - This program asks the user to enter a series of
    single digit numbers with nothing separating them. The input is
    read as a string object. The program displays the sum of all the
    single-digit numbers in the string. For example, if the user enters
    2514, the program displays 12, which is the sum of 2, 5, 1, and 4.
    It also displays the highest and lowest digits in the string. */

#include "Utility.h"

/* Calculates the sum-total of numbers in a string class object,
    returns this number */
int sumOfNumbers(string);

/* Finds the smallest number in a string class object, returns
    this number */
int findLowest(string);

/* Finds the highest number in a string class object, returns
    this number */
int findHighest(string);

/* Asks the user if he or she wants to repeat the process,
    returns the decision */
char tryAgain();

int main()
{
    int     total = 0,
             lowestNum = 0,
             highestNum = 0;
    char   again = ' ';
    string numbers = " ";
   
    do
    {
        cout << "\n\n\t\tSUM OF DIGITS\n\n"
              << "\tEnter a series of numbers without spaces in between\n"
              << "\tand I will calculate the sum of numbers for you.\n\n\t"
              << "Please enter your numbers: ";
        getline(cin, numbers);

        cout << "\n\n\tThe sum of your numbers is: "
              << (total = sumOfNumbers(numbers)) << "\n\n\t";

        cout << "This is the lowest number found: " << setw(2) << right
              << (lowestNum = findLowest(numbers)) << "\n\t";

        cout << "This is the highest number found: "
              << (highestNum = findHighest(numbers)) << "\n\n\t";

        again = tryAgain();

        if (again == 'N')
        {
            cout << "\n\tHave a nice day!\n\n";
        }

    } while (again == 'Y');

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: sumOfNumbers

    This function accepts a string class object as argument.
    It calculates the sum of numbers in the string. The sum-
    total is returned.
   ********************************************************** */

int sumOfNumbers(string numbers)
{
    int total = 0;

    for (unsigned int index = 0; index < numbers.size(); index++)
    {
        total += numbers[index] - '0';
    }

    return total;
}

/* **********************************************************
   Definition: findLowest

    This function accepts a string class object as argument.
    It determines the lowest number and returns it.
   ********************************************************** */

int findLowest(string numbers)
{
    int lowestNum = numbers[0] - '0';

    for (unsigned int index = 0; index < numbers.size(); index++)
    {
        if (lowestNum > numbers[index] - '0')
        {
            lowestNum = numbers[index] - '0';
        }
    }

    return lowestNum;
}

/* **********************************************************
   Definition: findHighest

    This function accepts a string class object as argument.
    It determines the highest number in a string and returns
    it.
   ********************************************************** */

int findHighest(string numbers)
{
    int highestNum = numbers[0] - '0';

    for (unsigned int index = 0; index < numbers.size(); index++)
    {
        if (highestNum < numbers[index] - '0')
        {
            highestNum = numbers[index] - '0';
        }
    }

    return highestNum;
}

/* **********************************************************
   Definition: tryAgain

    This function asks the user if he or she wishes to try
    again. This decision is returned.
   ********************************************************** */

char tryAgain()
{
    char again = ' ';

    cout << "Do you wish to try this again? ";
    cin >> again;
    cin.ignore();

    /* Input validation */
    while (toupper(again) != 'Y' && toupper(again) != 'N')
    {
        cout << "Do you wish to try this again? ";
        cin >> again;
        cin.ignore();
    }

    return toupper(again);
}

Example Output:




No comments:

Post a Comment