Q:

Write a program to process a vector<string>s whose elements represent integral values. Produce the sum of all the elements in that vector. Change the program so that it sums of strings that represent floating-point values

0

Write a program to process a vector<string>s whose elements represent integral values. Produce the sum of all the elements in that vector. Change the program so that it sums of strings that represent floating-point values.

All Answers

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

#include <string>
#include <vector>
#include <iostream>

int main() {
  std::vector<std::string> vs;
  for (std::string number; std::cin >> number; vs.push_back(number)) {}
  //int sum = 0;
  double sum = 0;
  for (const auto &s : vs)
    //sum += stoi(s);
    sum += stod(s);
  std::cout << "The sum is " << sum << std::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