newbie: get char input from keyboard

cuongvt's Avatar, Join Date: May 2008
Newbie Member
I'm very new to C language, I have the following code:
Code:
#include <stdio.h>
#include <stdlib.h>
int number;
int total;
int nums=0;
int i;
char c;
int main()
{

for(;;)
{
printf("please enter number here, and character k to quit:\n");
fflush(stdin);
scanf("%d",&number);
total +=number;
nums++;

}

printf("average value: %d\n",total/nums);
return(0);
}
Now I want is that when user type 'k' from keyboard, above loop will break.
Could anyone modofy my source code so that I can learn from you?
Thanks and regards,

Last edited by shabbir; 9May2008 at 11:28.. Reason: Code block
0
asadullah.ansari's Avatar, Join Date: Jan 2008
TechCake
Code:
char ch;
for(;
{
printf("Want to quit then Press k  Key\n");
fflushstdin);
scanf("%c",&ch);
if(ch=='k')
  break;
printf("please enter number here:\n");
fflush(stdin);
scanf("%d",&number);
total +=number;
nums++;
}

I dont know what you want. lot of different ways you can doit. Just tell If you want something different?

Last edited by shabbir; 9May2008 at 11:28.. Reason: Code block