Question

please I need help with the question: Problem: Write a C++ program that reads each character...

please I need help with the question: Prob

please I need help with the question:

Problem: Write a C++ program that reads each character from a user input, extracts the character if it is a digit, and calculates the sum of all the digits. The program stops reading the user input when the new line character ‘\n’ is encountered. For the output of the program, you should print each digit extracted from the user input and the sum of all the digits.
(*The main difference between “cin >> c” and “cin.get(c)” is that “cin >> c” skips the whitespace characters such as a tab (\t), blank, newline (\n). But “cin.get(c)” extracts every character including the whitespace characters.)

What to use:
- cin.get(c)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, Plesae find my program.

Please let me know in case of any issue:

#include <iostream>
#include <ctype.h> // isdigit(c) function

using namespace std;

int main(){

   char c; // char variable to take user input

   int sum = 0; // to stroe sum of all character digits

   cin.get(c); // taking first input

   while( c != '\n'){

       if(isdigit(c)){
           sum = sum + (c - '0');
       }
       // when you enter any character and hit enter then '\n'(newline) character also get stored in buffer
       // so before taking any other character from user you need to clear unnecessary new line character from buffer
       // otherwise it reads new line character from buffer as next input instead from user
       cin.ignore(); // ignoring current new line character
       // getting other character input
       cin.get(c);
   }

   cout<<"Sum of all digit characters: "<<sum<<endl;

   return 0;
}

Add a comment
Know the answer?
Add Answer to:
please I need help with the question: Problem: Write a C++ program that reads each character...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration...

    I need help with this exercise. Than you for the help. Language is C++ 2 Enumeration Data Types Reformat is the shell of a program designed to read characters and process them in the following way Lowercase character Uppercase character Digit Blank Newline Any other character Converts to uppercase and writes the character Writes the character Writes the digit Writes a blank Writes newline Does nothing / Program Reformat reads characters from file DataIn and // writes them to DataOut...

  • Write a C program named space_to_line.c that features a while loop that continuously reads input from...

    Write a C program named space_to_line.c that features a while loop that continuously reads input from the user one character at a time and then prints that character out. The exception is that if the user inputs a space character (‘ ‘), then a newline character (‘\n’) should be printed instead. This will format the output such that every word the user inputs is on its own line. Other than changing the spaces to newlines, the output should exactly match...

  • C++ Stack Program Write a program that uses stacks to evaluate an arithmetic expression in infix...

    C++ Stack Program Write a program that uses stacks to evaluate an arithmetic expression in infix notation. The program should NOT convert the infix to postfix and then evaluate the postfix. The program takes as input a numeric expression in infix notation, such as 3+4*2, and outputs the result. 1a)   Operators are +, -, *, / 1b) Assume that the expression is formed correctly so that each operation has two arguments. 1c) The expression can have parenthesis, for example: 3*(4-2)+6...

  • MIPS programming question Problem 1: Write a program that asks the user to input a string...

    MIPS programming question Problem 1: Write a program that asks the user to input a string (or no more than 50 characters). Your program should then output the length of the string. The string length should be determined using a separate function strlen that will accept the address of the string and return its length. For the purposes of this exercise, the length of a string will be defined as the number of non-null and non-newline characters until either the...

  • You're to write a C++ program that analyzes the contents of an external file called data.txt....

    You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...

  • I really need help with the code for this, in C++ please, I know there's a...

    I really need help with the code for this, in C++ please, I know there's a similar question but the answer isn't correct :[ A palindrome is a string that reads the same forwards and backwards (ignoring spaces, punctuation, and capitalization). Examples are the familiar ``If I had a hi-fi,'' the grander ``A man, a plan, a canal, Panama,'' and ``Some men interpret nine memos.'' So the goal: Design, implement, document, and test a program that reads a line of...

  • Write a Java program that reads a series of strings from a user until STOP is...

    Write a Java program that reads a series of strings from a user until STOP is entered. Each input consists of information about one student at the ABC Professional School. The input consists of the student’s last name (the first 15 characters with extra blanks on the right if the name is not 15 characters long), the student’s number (the next 4 characters, all digits, a number between 1000 and 5999), and the student’s program of study (one character, either...

  • c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two...

    c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two interesting programming challenges that will require a bit of character and/or string related processing with functions. (Carefully read the specifications for each programming challenge and select ONE.) (1) Sum of Digits in a String Write a 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...

  • In this project you will write a C++ program that simulates the purchase of a single...

    In this project you will write a C++ program that simulates the purchase of a single item in an online store. What to do Write a C++ program that: 1. Prints out a welcome message. 2. Prompts the user for the following information: (a) Their first name (example: Roger) (b) Their last name (example: Waters) (c) The name of the product (example: Brick) (d) The unit price of the product (example: 1.99) (e) The quantity to buy (example: 200) (f)...

  • Hello, I need help with my assignment; write a c program that reads a bunch of...

    Hello, I need help with my assignment; write a c program that reads a bunch of characters from the keyboard from 'a' through 'z', space, newline and linefeed characters ( using get char), and Keep counts each of the letters 'a' through 'z'.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT