Looking at your code mate, i think you're really just always going to get the sum as (1 +
the last value that was entered by the user), that's because the program is not actually 'using' or 'storing' the other values that are entered by the user because the value of num keeps changing, and in two ways
1. The next value entered by the user becomes the new value of num
2. The "num++" increments the previous value entered by the user
The compiler is confused, i suggest you use a different variable as your 'loopcounter variable' i.e
the variable used in the 'for loop' ,and use your 'num' variable to keep the values entered by the user, that way, the compiler won't be too confused.
Next, you really want to be adding the values entered as soon as they are entered by the user, you cannot first enter all the values and then add them all up, because you are only using one variable ('num'), and it can only store one value at a time, so all the other values entered are actually lost,
I HOPE i'm not confusing you
So something like this should worK;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include<iostream>
using namespace std;
int main()
{
int total = 0, num = 0, sum = 0;
cout << "Enter a positive number between 1 and 10" <<endl;
cin >> total;
cout << "Now we're going to add that many numbers together.....\n Enter first number" <<endl;
for (int loopcounter /*or anything else*/ = 1; loopcounter <= total; loopcounter++)
{
if (loopcounter /*or what you chose*/ !=1)
{
cout << "Enter next number" <<endl;
}
cin >> num;
sum += num; // OR just use "sum = num + sum;" , it's really the same
}
cout << "The sum of your numbers are:" << sum <<endl;
return 0;
}
|