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

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....
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 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 =...
C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
In the space provided below write a C++ program that asks the user to enter their...
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a file.
Write a C program that prompt the user to enter 10 numbers andstores the numbers...
Write a C program that prompt the user to enter 10 numbers and stores the numbers in an array. Write a function, smallestIndex, that takes as parameters an int array and its size and return the index of the first occurrence of the smallest element in the array.The main function should print the smallest number and the index of the smallest number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT