Click here to Skip to main content
15,915,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm making a programme to visualize the next 10 number of n. I know, is very easy but i'm new in this.

Here is my programme. I can't visualize the 10 numbers after n. I always get one number (n+10). For exemple, if a put n=5, i need to visualize 6,7,8,9,10,....,15.
Why i always get just 15?

C++
#include<stdio.h>
void numeros_sumados(int n)
{
int i=0;
int M;

	for (i=n+1; n+10;i++)
	{

		fprintf(stdout,"%d\n",i);
		
	}
		fprintf(stdout,"\n");
}

int main (void)
{
int n;

	fprintf(stdout,"Tapez le numero n:\n");
	fscanf(stdin,"%d",&n);
	numeros_sumados(n);
}

Thanks
Posted

Take a look at this line:

C++
for (i=n+1; n+10;i++)


Remember how a for-loop works?

The format is:

for (initial condition; exit condition; step amount)

So you have the initial condition set (i = n + 1)
But the exit condition is just n+10, which evaluates to true (greater than 0), and only loops once.

You need something like this:

C++
for (i=n+1; i < n + 10; i++)


Which will loop until i is greater than or equal to n+10.
 
Share this answer
 
Comments
alonetmr 15-Nov-13 15:25pm    
Hi, thanks for you answer but it doesn't work. I always get one value and the loop never end.
Albert Holguin 15-Nov-13 16:23pm    
This should work fine unless you did something else wrong.
Albert Holguin 15-Nov-13 16:22pm    
+5... I like that you took the time to explain his problem (vs just doing it right for him)
Andreas Gieriet 15-Nov-13 18:01pm    
The code is right, the explanation is wrong, sorry.
The condition is "loop while the condition is true" - the condition is the *opposite* of an exit condition.
for(init;cond;inc) { ... } is equivalent to init; while(cond) { ... inc; }
Cheers
Andi
Richard MacCutchan 16-Nov-13 4:48am    
Actually, the test condition of n+10 means it will loop for ever.
Simple: your termination condition in the for loop isn;t a condition - it's a non-zero value, which is true as far as C is concerned...
Try changing:
C++
for (i=n+1; n+10;i++)
To:
C++
for (i=n+1; i < n+10;i++)
And see what happens!
 
Share this answer
 
Comments
alonetmr 15-Nov-13 15:25pm    
Hi, thanks for you answer but it doesn't work. I always get one value and the loop never end.
OriginalGriff 15-Nov-13 15:58pm    
Copy and paste *exactly* the code you are trying - complete with the change I recommended.

Because I think you typed it wrong... :laugh:
alonetmr 15-Nov-13 16:03pm    
I followed your suggestion. I put n=2 and I got this:
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12
12^C

Why? is always the same number. thank you

By the way, here is the programme with the modification:

#include<stdio.h>
void numeros_sumados(int n)
{
int i=0;

for (i=n+1; i < n+10;i++)
{

fprintf(stdout,"%d\n",i);

}
//fprintf(stdout,"\n");
}

int main (void)
{
int n;

fprintf(stdout,"Tapez le numero n:\n");
fscanf(stdin,"%d",&n);
numeros_sumados(n);
}
OriginalGriff 15-Nov-13 16:18pm    
No it isn't - that won't even compile!
There is no 'n' in scope of your method: your parameter is called 'p'
So now look at the version that compiles... :laugh:
Albert Holguin 15-Nov-13 16:27pm    
Guessing you and Ron responded at the same time... +5 anyway, :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900