1

I have a question regarding this program. I am a beginner when it comes to programming and c++, and I'm trying to figure out two things.

  1. why is this program not compiling (error: Using uninitialized memory 'total' - I have it defined as a variable??).

  2. Could someone explain how the function outside of the main (sumUpTo) works? Specifically & vec and total, as I've never seen them before. Thanks.

/* 1) read in numbers from user input, into vector    -DONE
    2) Include a prompt for user to choose to stop inputting numbers - DONE
    3) ask user how many nums they want to sum from vector -
    4) print the sum of the first (e.g. 3 if user chooses) elements in vector.*/

#include <iostream>
#include <string>
#include <vector>
#include <numeric> //for accumulate

int sumUpTo(const std::vector<int>& vec, const std::size_t total)
{
    if (total > vec.size())
        return std::accumulate(vec.begin(), vec.end(), 0);

    return std::accumulate(vec.begin(), vec.begin() + total, 0);
}

int main()
{

    std::vector<int> nums;
    int userInput, n, total;

    std::cout << "Please enter some numbers (press '|' to stop input) " << std::endl;
    while (std::cin >> userInput) {

        if (userInput == '|') {
            break; //stops the loop if the input is |.
        }

        nums.push_back(userInput); //push back userInput into nums vector.
    }

    std::cout << "How many numbers do you want to sum from the vector (the numbers you inputted) ? " << std::endl;
    std::cout << sumUpTo(nums, total);

    return 0;
}
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
  • 2
    You have declared `total`, and you are using it, but you've never given it any particular value. Your program then exhibits undefined behavior by way of accessing an uninitialized object. – Igor Tandetnik Aug 08 '20 at 19:17
  • 2
    What about `sumUpTo` is unclear to you? You seem to be saying you've never before seen a function taking parameters. Surely your favorite C++ textbook contains examples of such. – Igor Tandetnik Aug 08 '20 at 19:19
  • @IgorTandetnik I have seen them taken parameters but not such with that deal with pointers which i believe is what & is about. And calling functions outside of main hasn't been discussed yet hence my question sir. – matiboy212121 Aug 08 '20 at 19:24
  • 2
    `&` here indicates a reference, not a pointer. Your favorite C++ textbook should have a section on passing parameters by reference. And a section on defining and calling functions. – Igor Tandetnik Aug 08 '20 at 19:25
  • Yeah i believe it does but the book is for complete beginners and i think he is trying to force us to make use of simple coding skills (simple functions etc), I will look into it though, thank you. – matiboy212121 Aug 08 '20 at 19:27

1 Answers1

6

Errors in your code -

  • int userInput, n, total;
    .
    .
    .
     std::cout << sumUpTo(nums, total);
    

    Here you are declaring total and directly using it as a parameter to sumUpTo function. In that function you are using it in an comparison ( if (total > vec.size()) ). But, since you have never initialized it while declaration nor have you assigned it any value anywhere in the code, the compiler doesn't know what to make of that comparison that you are making since total doesn't have any value.


could someone explain how the function outside of main (sumUpTo) works? Specifically '& vec' and 'total'

sumUpTo has the declaration as - int sumUpTo(const std::vector<int>& vec, const std::size_t total).

Here, you are expecting the function to take a vector of integers as parameters. But you probably have doubt with & that's before vec. That symbol just specifies that you are going to pass the vector as a reference and not by making a complete copy of the vector. In our regular passing, the vector we pass to function will get passed as a copy of our origin vector. But in this case, the vector is getting passed as a reference and not a copy of the original vector.

Do note that I have used the term reference and not pointers. If you are coming from C background, you could feel both are the same and in some cases, they might function a bit similar but there are few differences(some good answers on SO - 1, 2, 3 ) between them which you can read many good resources available online. Just understand that in this case, it prevents making a copy of the vector when it is passed to the function. If the function declaration wouldn't have mentioned that parameter to be const, you could also make changes in the vector which would also reflect in the original one as well (while they wouldn't have if you passed it normally rather than as reference).

std::size_t is a type that is used to represent the size of objects in bytes. It is an unsigned datatype and used whenever you are dealing with sizes of objects. You can also refer this if you are not sure about the difference between std::size_t and int ( which is what you might have been expecting total to be).

Lastly, it's obvious that const is being used in the function to ensure that the parameters that we are passing to function aren't getting modified in the function.

Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32