A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a C++ program to sum of all positive integers in a sentence
Q:

Write a C++ program to sum of all positive integers in a sentence

0

Write a C++ program to sum of all positive integers in a sentence

Sample Output:

Input number: 12 chairs, 15 desks, 1 blackboard and 2 fans 
Sum of all positive integers: 30

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;
 
int main()
{
    string str1;
    int sum_num = 0, num;
     
    while (getline(cin, str1)) {
        for (int i = 0; i < (int)str1.size(); i++) {
            if (isdigit(str1[i])) continue;
            else {
                str1[i] = ' ';
            }
        }
 
        stringstream abc(str1);
        while (abc >> num) {
            sum_num += num;
        }
    }
    cout << "Sum of all positive integers: " << sum_num << endl;
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now