Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   newbie: get char input from keyboard (http://www.go4expert.com/forums/newbie-char-input-keyboard-t10492/)

cuongvt 9May2008 09:51

newbie: get char input from keyboard
 
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,

asadullah.ansari 9May2008 10:07

Re: newbie: get char input from keyboard
 
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?


All times are GMT +5.5. The time now is 23:50.