Do you know what your For loop is doing?
Every time your code is reading a number, it is overwriting your previous read. so basically your num variable will only hold the last value entered. For example try this code (which is from your original code) below and see the output:
#include <iostream>
usingnamespace std;
//initialise
int sum = 0;
//function
int add(int n){
return sum = sum + n;
}
int main(){
int num;
for(int i=0;i<5;i++){
//prompt for user input
cout<<"enter number: ";
//read user input
cin>>num;
//call function to cumulative sum
add(num);
}
cout<<sum;
return 0;
}
#include <iostream>
int add(int num, int sum);
int main(){
int num, sum = 0;
for(int i = 0; i < 5; ++i) {
std::cout << "enter number: ";
std::cin >> num;
sum = add(num, sum);
}
std::cout << sum << std::endl;
return 0;
}
int add(int num, int sum){
return sum += num;
}