Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:43:54+00:00 2026-05-30T03:43:54+00:00

I am practicing with functions on the vector class. i have written a function

  • 0

I am practicing with functions on the vector class. i have written a function ‘sumVector’ to add up all elements of vector ‘ages’, but it is not returning the total
i am copying my code block; i know the STL provides a sum function but this is for my own practice. i would appreciate if someone would point out what i am doing wrong

    #include <vector>
    #include <fstream>
    #include <sstream>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    using namespace std;
    template<typename T>
    int sumVector(const vector<T> &integers2)
    {
         typename vector<T>::const_iterator constIterator;
         //display vector elements using constIterator
         for 

         (constIterator=integers2.begin();                                             
         constIterator!=integers2.end();++constIterator);
         int sum=0;
         sum+=*constIterator;
         return sum;
         cout<<sum;
    }




    int main ()
    {
    vector<int> ages;
    vector<float>var;
    ifstream inFile;
    inFile.open("data.txt");
    if(!inFile)
    {
        cout<<"didn't work";
        cin.get();
        exit (1);
    }

    string line;
    getline(inFile, line);
    istringstream in(line);
    copy(std::istream_iterator<int>(in),
      std::istream_iterator<int>(),
      std::back_inserter(ages));
    cout<<ages.at(2)<<endl;
    getline(inFile, line);
    istringstream gin(line);
    copy(istream_iterator<float>(gin),
                                     istream_iterator<float>(),
                                     back_inserter(var));
    cout<<var.at(0)<<endl;
    cout<<"whats the sum";
    sumVector (ages);

    cin.get();

    inFile.close();
    cin.get();

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:43:56+00:00Added an answer on May 30, 2026 at 3:43 am

    The for loop is incorrect and the cout << sum occurs after the return from sumVector(). The return type (and type of sum) should also be T otherwise sumVector() is restricted to vectors of elements of int (or types convertible to ints).

    Something like the following I think is what your looking for:

    template<typename T>
    T sumVector(const vector<T> &integers2)
    {
        typename vector<T>::const_iterator constIterator;
        T sum=0;
        //display vector elements using constIterator
        for (constIterator=integers2.begin();
             constIterator!=integers2.end();
             ++constIterator)
        {
            sum+=*constIterator;
        }
    
        cout<<sum;
        return sum;
    }
    

    Consider not printing the value of sum within sumVector() as (IMO) this is clearer:

    cout << "whats the sum: " << sumVector(ages);
    

    It may also be required to store the result of sumVector() in a variable and not write anything to standard output at the point the sum is calculated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so im practicing the STL string class, but i can not figure out why
I was practicing the dynamic programming problem on SPOJ. But I have no idea
I am practicing with PHP and AJAX but I have some problems! I'm trying
I have been practicing TDD and (some) XP for a few years now and
I have been practicing TDD in C# for several years now and recently have
I've been practicing for an upcoming programming competition and I have stumbled across a
I'm practicing Jquery and I've written this simple Jquery statement: var someText = $(table
I'm practicing PyQt and (Q)threads by making a simple Twitter client. I have two
I am new to PHP and I am practicing the file_put_contents() function, and I
Been practicing with those system calls, but I stucked into this code: #include <stdio.h>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.