#include <iostream>
#include <cmath>
usingnamespace std;
constint NUM = 100;
int main(){
// init variable
int sum = 0;
// init array
int arr[NUM];
for (int i = 0; i < NUM; i++)
arr[i] = 0;
// Welcome and Prompt
cout << "Welcome to the line adder\n"
<< "Please enter the numbers you wish to be added" << endl;
// getting the numbers into the array
for ( int i = 0; i < NUM; i++){
if (cin.peek() != '\n'){
cin >> arr[i];
for (int j = 0; j < arr[i]; j++){
sum = sum + arr[i];
}
}
else{
break;
}
}
cout << sum;
// pause and exit
getchar();
getchar();
return 0;
}
Your problem is that you're double, triple, quadruple... counting becase of your for-loop on line 25. You only want to add the number last inputted to sum, but instead you loop through your whole arr again.
I thought it might have something to do with that. However, if i place that loop anywhere else arr[i] isn't identified. So i have no idea where to put it. Should i make another array and copy arrr[i] into it?
You do not need a loop. You only need to add a value to sum every time you get a value from the user. You only need the statement in the loop, not the actual loop surrounding it.