can't understand this
|
can someone help with my TURBO C programming?? the problem is.. Write a program using while loop statement that will let the user input integers 20 times and except zero. At the end of these conditions, the program will display the: 1.the Number of Integers entered; 2.the Number of Positive Integers entered; 3.the Number of Negative Integers entered. I have here my code but whenever i try to run it, i can't input any integers or character..help me pls.. here's my code.. #include #include int num,integer; main() { clrscr(); integer=0; num=1; while(num>20){ num++; integer+=num; printf("Integer Entered: ",num); } getch(); } note: I'm a newbie |
Re: can't understand this
Link |
by
on 2010-10-06 00:21:50
|
Try this code #include< stdio.h > #include< conio.h > main() { int num,integer=0; //(private initialization) num=1; // initialization while(num<=20){ //will run if "num" under 20 num++; // num increment printf("\nInput integer : "); scanf("%i",&integer); // integer input if(integer == 0)continue; // 0 pass printf("Integer Entered: %i\n",integer); // show output } getch(); } |
Re: can't understand this
|
@lanster: tnx for the code..but the "}" sign should go above the "if(integer==0);" statement so that the Integer Entered printf won't display unless 20 integers were entered... oh and how should i continue with the code and make my "if statement" if i were to input negative or positive integers and display the words printf("Positive Integers Entered") or printf("Negative Integers Entered") ?? |
Re: can't understand this
Link |
by
on 2010-10-06 03:47:56
|
You mean this one? main(){ int num, integer=0; //(private initialization) num=1; // initialization while(num<=20){ //will run if "num" under 20 num++; // num increment printf("\nInput integer : "); scanf("%i",&integer); // integer input if(integer == 0)continue; // 0 pass else{ if(integer>0)printf("Positive Integer Entered"); else printf("Negative Integer Entered"); } } getch(); } |
Re: can't understand this
|
what i meant was..if i entered 20 negative integers, the display would be "Negative Integers Entered: #" but if i entered 20 positive numbers, the display would be "Positive Integers Entered: #" i tried and experimented several times but it the display is still the first printf("Integers Entered") |
Re: can't understand this
Link |
by
on 2010-10-06 05:18:24
|
this one? #include< stdio.h> #include< conio.h> #include< string.h> void main(){ int x[21],y=1,positive[21],negative[21],flagA=0,flagB=0; while(y<=20){ printf("Input #%i = ",y); fflush(stdin); scanf("%i",&x[y]); if(x[y]==0){ printf("Must not 0"); continue; } else{ if(x[y]>0){ positive[flagA++] = x[y]; } else{ negative[flagB++] = x[y]; } } y++; // move this code to upper line if you want to skip continue } y=1; printf("The number integer entered = %i :",flagA+flagB); //count while(y <= flagA+flagB){ printf("%i, ",x[y]); y++; }printf("\b\b\n\n"); y=0; printf("The number positive integer entered = %i :",flagA); //count while(y < flagA){ printf("%i, ",positive[y]); y++; }printf("\b\b\n\n"); y=0 ; printf("The number negative integer entered = %i :",flagB); //count while(y < flagB){ printf("%i, ",negative[y]); y++; }printf("\b\b\n\n"); } |
Re: can't understand this
Link |
by
on 2010-10-06 10:34:44
|
change if(positive > 0) ---> if(integer > 0) if(negative < 0) ---> if(integer < 0) If this one is wrong, then I surrender *preparing white flag* |
Re: can't understand this
|
I already tried that too..but failed..anyway..thanks for the help man..it's just my gut tells me that the problem lies in the "if statement"..hope i get this code right before afternoon or I'm toast >.< |