This is a very simple app I am trying to do. How else would sum up the total of the number the user entered? Example if the user enters 10, the loop will find the sum of 1,2,3,4,5.....10. Here is my code and I spent few hours on to come up with this simple code.

//this will add up the number you entered
# include <iostream>
using namespace std;

int main()
{
	int num; //to hold the number to be sumed up
	int input; //this is an accumulator
	int maxNum; //the maximum number the user entered
	int total; //this will hold the total of the number the user typed in
 
cout << "Enter the number and I will add them up<<\n";
cin >> maxNum;

for (num=1; maxNum=1; input++)
{
	cout <<"You entered" <<maxNum<< endl;
	cout <<"See the total below" << endl;
	cout << (total += input) << endl;
	maxNum++;
}
	return 0;
}

the book says I can use the for loop if the number of iteration is known. In this case the iteration depends on the number the user entered which is stored in maxNum variable. Which operator I am using is wrong here or the whole structure of my code is messed up? Please help!

>> How else would sum up the total of the number the user entered?

#include <iostream>
#include <iterator>
#include <numeric>

int main()
{
   std::cout << std::accumulate(std::istream_iterator<int>(std::cin),
                                std::istream_iterator<int>(),
                                0);
   return 0;
}

Only kidding :) Ignore that!!

Now.
>> for (num=1; maxNum=1; input++)
Do you know the proper format for a for loop?
for ( INIT; CONDITION; OPERATION )
so you want for num=0; num<maxNum; num++ )

Also, a good habit to get into is to initialise your variables to a certain value. So:

int num = 0;
int total = 0;

as they are not guranteed to be defaulted to one. If you look at the variabel total it's not set to any value so if you add something to it... the sum may not be correct.

>> maxNum++;
Why did you do that?

I simply dot get it. heres my understanding

user enter a number
number entered get stored in variable maxNum
then get that number and sum up the total. HOW?
step1: initialize 0 to num
step2: compare if num is less than or equal to maxNum (the number the user entered)
step3: increment the maxNum from 1 to whatever number the user entered.
step4: total up the sum of maxNum (maxNum +=)
cout the total! is there a missing step? grrrrrr!!! pls help me understand! this i my homework and due tonight at 11...

Here's what the code should look like. You made a lot of errors throughout, I tried this and it does work.

int main(){
int num; //to hold the number to be sumed up
int maxNum; //the maximum number the user entered
int total = 0; //this will hold the total of the number the user typed in


cout << "Enter the number and I will add them up: \n";
cin >> maxNum;


cout <<"You entered " <<maxNum<< endl;
cout <<"See the total below" << endl;
for(num=1; num<maxNum; num++)
{


total = total += num;
cout << total << endl;


}
return 0;
}

Edited 3 Years Ago by happygeek: fixed formatting

this doesn't work either. an example is if the user enter 3 the total should display 6 (1+2+3 = 6). What I dont understand is how the initializer really works on the first parameter of for loop?? we have to have an accumulator or a counter some place so that as the number entered by the user increments, it also adds it up.

Oh sorry, it should read: (num=1; num<=maxNum;num++)

The num variable acts as the counter or accumulator. You're initializing num to 1 in the for condition, and incrementing it until it is equal to maxNum.

so num IS the accumulator? why do we have to assign something in it? why can't it just grab the value of maxNum and increment then store the value to total? I don't clearly understand but I thank you so much for helping me. Still studying the code you provided.

This question has already been answered. Start a new discussion instead.