DaveSimmons
Elite Member
- Aug 12, 2001
- 40,730
- 670
- 126
Did you buy a C++ book? looking up the syntax for "for" will tell you why they don't compile.
Why did you put in an "if" to begin with, what condition were you testing the truth of?
In your two "for" loops, what range of numbers are you trying to loop through?
It might help you to write out what you are trying to do in English before you translate it into code.
Here is how you would add / sum up all the numbers from 1 to n:
get number n
set total to 0
loop from 1 to n
{
add current loop value to total
}
write out total
then I translate it to code (only part is shown):
. . .
for ( i = 1 ; i <= n ; i++ )
{
total = total + i ;
}
cout << "total " << total ;
Why did you put in an "if" to begin with, what condition were you testing the truth of?
In your two "for" loops, what range of numbers are you trying to loop through?
It might help you to write out what you are trying to do in English before you translate it into code.
Here is how you would add / sum up all the numbers from 1 to n:
get number n
set total to 0
loop from 1 to n
{
add current loop value to total
}
write out total
then I translate it to code (only part is shown):
. . .
for ( i = 1 ; i <= n ; i++ )
{
total = total + i ;
}
cout << "total " << total ;
