Hello guys, i need help. I just began to learn c++, and i have a problem. How to write a program, which count numbers in a interval from k to n. I now the program is simple but i just can t bring it to work. Im using the loop for here what i write:

#include <iostream.h>
void main()
{
int suma = 0;
int k;
int n;
int x;
cout << "k";
cin >> k;
cout << "n";
cin >> n;

for ( x = 0; x > k; x < n)
{
cout += x;
x++;
}}
I now the problem is in cout and in x. Can anyone help!

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by Dex02

what does your for suposed to do?
I don't think you get how the for works for(statement; condition; statement2)
for bucle will do the first statement at the begining. Then it will do the inside function (the code inside the for till the condition returns false.
at the end of every bucle cycle the second statement wil be done... sorry for my inglish.

I now the problem is in cout and in x. Can anyone help!

You're not using `cout` properly in the loop. It looks like you want `cout << x` . Here are some examples.

Also, the `for` loop isn't right. Here's a description of how they work. What you want is:

``````for (x = k; x <= n; x++)
{
cout << x;
}``````

Explanation:

• `x = k` : Initial statement. You want to start counting with `k` , so start `x` there.
• `x <= n` : Conditional expression. You want to count all the way to `n` , so keep looping as long as `x` is less than or equal to `n` . You don't want `x < n` because that would stop your loop at `n - 1` .
• `x++` : Loop statement. Add one to `x` each time around the loop. You don't need an extra `x++` inside the loop.

Edited by gusano79: Forgot to remove the extra &quot;x++&quot;

THX!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.