Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi im very new to c and i decided to write this program, can anyone tell me why its not counting down from the number entered to 0?
C++
#include <stdio.h>

int printloop(int k )
{
    int b;
        for (b = k; b < 0; b--)
     printf("%d\n",b);
}

int main()
{
int c;

scanf("%d",&c);
if( c > '0' && c < '6')

printloop(c);

return 0;
}
Posted
Updated 25-Sep-12 8:19am
v2

To start with:
C++
if( c > '0' && c < '6')

c is an integer defined but you compare it with a character '0' & '6'. Remove quotes around 0 & 6
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Sep-12 14:26pm    
One bug nailed down, a 5.
--SA
Sandeep Mewara 25-Sep-12 14:29pm    
Thanks SA.
fjdiewornncalwe 25-Sep-12 14:32pm    
+5 for the "other" issue.
Sandeep Mewara 25-Sep-12 14:39pm    
:)
ridoy 25-Sep-12 15:29pm    
+5
Because in your for (b = k; b < 0; b--), you aren't allowing it to do any work.
The second clause "b<0" indicates that this loop will only run if b < 0.
Since you set b == k which is greater than 0, the loop will not run.
Your solution is for (b = k; b >= 0; b--)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Sep-12 14:26pm    
Another bug nailed down, a 5.
--SA
fjdiewornncalwe 25-Sep-12 14:32pm    
That error was simply too common a mistake that rookies make. I've learned a long time ago to always check the for loop args before anything else when someone says the loop isn't doing anything. Thanks.
Sergey Alexandrovich Kryukov 25-Sep-12 17:41pm    
Sure...
--SA
Sandeep Mewara 25-Sep-12 14:29pm    
5! Marcus!

:)
fjdiewornncalwe 25-Sep-12 14:32pm    
Thanks Sandeep.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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