Back | Reverse | Quick Reply | Post Reply |

can't understand this
Link | by anime on 2010-10-05 11:27:02
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

sig avi

Re: can't understand this
Link | by Lanster 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();
}

Reinforce

Re: can't understand this
Link | by anime on 2010-10-06 03:00:57
@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")

??

sig avi

Re: can't understand this
Link | by Lanster 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();
}

Reinforce

Re: can't understand this
Link | by anime on 2010-10-06 04:02:45
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")

sig avi

Re: can't understand this
Link | by Lanster 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");
}

Reinforce

Re: can't understand this
Link | by anime on 2010-10-06 05:30:09 (edited 2010-10-06 05:31:26)
I'll give you a screenshot of what i meant...

Photobucket

if i want to enter integers, postive integers,negative integers..the printf's in there should be the display..but instead when i run the prog..the display is always "Integer Entered"

sig avi

Re: can't understand this
Link | by Lanster 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*

Reinforce

Re: can't understand this
Link | by anime on 2010-10-06 16:57:36
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 >.<

sig avi

Back | Reverse | Quick Reply | Post Reply |

Copyright 2000-2025 Gendou | Terms of Use | Page loaded in 0.0028 seconds at 2025-01-16 12:26:03