1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

newbie: get char input from keyboard

Discussion in 'C' started by cuongvt, May 9, 2008.

  1. cuongvt

    cuongvt New Member

    Joined:
    May 5, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: May 9, 2008
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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 a moderator: May 9, 2008

Share This Page