Question

in C++ Extract and Add a Series of Numbers: Write a program that will extract a...

in C++

Extract and Add a Series of Numbers: Write a program that will extract a series of numbers (type double) from an input sentence and then add them. EXAMPLE: Suppose the sentence entered is “Give me the sum of 25.25 and 13.50. ”The program should print to the screen: The sum = 38.75 NOTE: The numbers can be of any value. Don’t hard code to the values shown in the example. In this problem take advantage of the input options presented in class. Particularly cin, cin.get, cin.ignore, and peek. The functions in Appendix C.2 may also be helpful.

Test the program with both of these examples:

TEST CASE 1. “Please tell me the sum of 18.0 plus 25.5.”
OUTPUT: "The sum is 43.5
TEST CASE 2. “If I add 24.5, 18.0 and 17.2 what do I get?
OUTPUT: "The sum is 59.7

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

//Language: C++

#include<iostream>

#include<sstream>

using namespace std;

int main(){

double sum = 0;

double num;

string temp, tempStr;

getline(cin, tempStr);

istringstream ss(tempStr);

while(ss>>temp){

try{

sum += stof(temp);

}

catch(std::invalid_argument){

sum += 0;

}

}

cout<<"The sum is "<<sum<<endl;

return 0;

}

Let me know if you have any clarifications/errors. Thank you

Add a comment
Know the answer?
Add Answer to:
in C++ Extract and Add a Series of Numbers: Write a program that will extract a...
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
  • and then add them. You Program 1. Write a program that will extract two numbers (type...

    and then add them. You Program 1. Write a program that will extract two numbers (type double) from a sentence a can set the sentence as a string constant or you can enter it from the keyboard EXAMPLE: Suppose the string entered (or constant) is "The sum of 25.25 and 13.50 is. The program should print to the screen: 25.25 plus 13.50 is 38.75 NOTE: The two numbers can be any value. Don't hard c ode to the values shown...

  • Write a machine language program to input two one-digit numbers ranging from 0 to 4 (using...

    Write a machine language program to input two one-digit numbers ranging from 0 to 4 (using the character input instruction), add them, and then output the single digit sum (using the character output instruction). Execute your program in the PEP/8 simulator. Example Input: 43 (this is the numbers 4 and 3. Remember, this is two ASCII characters when inputted into your program) Output: 7 (ASCII character 55dec)

  • I need help with this C++ assignment. Create a program which will implement the sum of...

    I need help with this C++ assignment. Create a program which will implement the sum of the numbers between 1 and n where n is an integer. When completed, the program will allow the user to input a positive integer (n) then the program will add all numbers between 1 and n (including n) and output the sum. For example, if the user enters 8 then the program will sum the numbers 1 through 8 and will output the sum....

  • Please write a C++ Program for the following problem >> Vector: Calculation the sum of each...

    Please write a C++ Program for the following problem >> Vector: Calculation the sum of each two adjacent Numbers.... >>【Description】 Read integer numbers from keyboard and store them into a vector. Calculation the sum of each two adjacent Numbers, store the result into another vector. Then output the result inversely. For example, if the input is 1 2 3 4 5 then the output is 9 7 5 3 【 Input】 There are 5 test cases. For each case, there...

  • write the C++ program to do the following 1. read in 2 numbers as ints 2....

    write the C++ program to do the following 1. read in 2 numbers as ints 2. calculate the sum, difference, product, and quotient 3. print out the four calculated numbers in the following       format           the two input numbers are ??? and ???              sum is             ????              difference is     ????              product is        ????              quotient is       ?????.?????? where ??? represents the ints and ????.???? represents the decimals. You can have any number of decimal places Example: Assume...

  • Write a function to calculate the sum of the reciprocals of a series of odd numbers....

    Write a function to calculate the sum of the reciprocals of a series of odd numbers. The function will have one input and no output, with the input being the ending value for the series of odd values. Write the function definition statement Initialize a variable to zero. This variable will contain the sum of all the values. Create a for loop that loops over all odd numbers from 1 to the specified ending value. Inside the loop, add the...

  • .using while loops(matlab) Write a program to display the sum of the series [ 9 +...

    .using while loops(matlab) Write a program to display the sum of the series [ 9 + 99 + 999 + 9999 ...] Test Data : Input the number or terms :5 Expected Output : 9 99 999 9999 99999 The sum of the series = 111105

  • Python 3.6 "While Loops" Write a program that adds up a series of numbers entered by...

    Python 3.6 "While Loops" Write a program that adds up a series of numbers entered by the user. The user should enter each number at the command prompt. The user will indicate that he or she is finished entering the number by entering the number 0.   Example This program will sum a series of numbers. Enter the next number (enter 0 when finished) > 5 Enter the next number (enter 0 when finished) > 2 Enter the next number (enter...

  • Please write a C++ program that will accept 3 integer numbers from the user, and output...

    Please write a C++ program that will accept 3 integer numbers from the user, and output these 3 numbers in order, the sum, and the average of these 3 numbers. You must stop your program when the first number from the user is -7. Design Specifications: (1) You must use your full name on your output statements. (2) You must specify 3 prototypes in your program as follows: int max(int, int, int); // prototype to return maximum of 3 integers...

  • Hello, can you please show me how to do this program with COMPLETE INPUT VALIDATION so...

    Hello, can you please show me how to do this program with COMPLETE INPUT VALIDATION so the computer tells the user to enter ints only if the user enters in floating point numbers or other characters? Write a method called evenNumbers that accepts a Scanner reading input from a file containing a series of integers, and report various statistics about the integers to the console. Report the total number of numbers, the sum of the numbers, the count of even...

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
ADVERTISEMENT