Question

In: Computer Science

Write a C++ program that asks the user to enter a series of single-digit numbers with...

Write a C++ program that asks the user to enter a series of
single-digit numbers with nothing separating them. Read the input
as a C-string or a string object. The program should display the
sum of all the single-digit numbers in the string. For example, if
the user enters 2514, the program should display 12, which is the
sum of 2, 5, 1, and 4. The program should also display the highest
and lowest digits in the string.

It is recommended that you include a function such as int charToInt(char)

The charToInt function
accepts a char and returns the char's value as an integer digit. If
the char is not a digit, the function returns 0.

Remember to use proper formatting and code documentation.

Sample screen output (user input is in bold) :

Enter a series of digits with no spaces between them.
1 2 3 4 5
Incorrect input....?

Enter a series of digits with no spaces between them.
1234 4321
Incorrect input....?

Enter a series of digits with no spaces between them.
1234 srjc
Incorrect input....?

Enter a series of digits with no spaces between them.
srjc 1234
Incorrect input....?

Enter a series of digits with no spaces between them.
987654321
The sum of those digits is 45
The highest digit is 9
The lowest digit is 1

Process returned 0 (0x0)   execution time : 71.074 s
Press any key to continue.

Solutions

Expert Solution

Solution:

Look at the code and comments for better understanding............

Screenshot of the code:

Output:

Code to copy:

#include<iostream>
using namespace std;
//a function that checks if the input is valid or not
bool check(string input)
{
        for(int i=0;input[i]!='\0';i++)
        {
                //if the input contains spaces or characters other than digits we return false
                if(input[i]==' ' || input[i]<'0' || input[i]>'9')
                {
                        return false;
                }
        }
        return true;
}
//converting a char to digit
int charToInt(char ch)
{
        return ch-'0';
}
int main()
{
        //loop untill a valid input is given
        while(1)
        {
                string input;
                cout<<"Enter a series of digits with no spaces between them."<<endl;
                getline(cin,input);
                //check the input
                if(check(input)==false)
                {
                        cout<<"Incorrect input....?"<<endl<<endl;
                        continue;
                }
                int sum=0,low=9,high=0;
                for(int i=0;input[i]!='\0';i++)
                {
                        int digit = charToInt(input[i]);
                        //finding sum , lowest and highest digit 
                        sum += digit;
                        if(digit < low)
                                low = digit;
                        if(digit > high)
                                high = digit;
                }
                //printing the digits
                cout<<"The sum of those digits is "<< sum << endl;
                cout<<"The highest digit is "<< high << endl;
                cout<<"The lowest digit is "<< low << endl;
                break;
        }
}

I hope this would help................................:-))


Related Solutions

Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
C++. Write a program that asks the user to enter a single word and outputs the...
C++. Write a program that asks the user to enter a single word and outputs the series of ICAO words that would be used to spell it out. The corresponding International Civil Aviation Organization alphabet or ICAO words are the words that pilots use when they need to spell something out over a noisy radio channel. See sample screen output for an example: Enter a word: program Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike The specific requirement...
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Design a complete program that asks the user to enter a series of 20 numbers. The...
Design a complete program that asks the user to enter a series of 20 numbers. The program should store the numbers in an array and then display each of the following data: I. The lowest number in the array II. The highest number in the array III. The total of the numbers in the array IV. The average of the numbers in the array *PYTHON NOT PSUEDOCODE AND FLOW CHART!!!!*
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
Write a python program that asks the user to enter a string containing numbers separated by...
Write a python program that asks the user to enter a string containing numbers separated by commas, e.g., s = '1.23,2.4,3.123', Your program should then calculate and print the sum of the numbers entered. Hint: you need to iterate over the string searching for the commas, i.e. their index. The first number is obtained by slicing between the start of the string and the index of the first comma. The second number is between the last comma and the next...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT