where is "ch=getchar" or "scanf("%c",&ch)??

This is a discussion on where is "ch=getchar" or "scanf("%c",&ch)?? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void call(int); static int s=0; int main(void) { int i; int flag=1; int count=1; char ch; while(flag>0) ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    22

    where is "ch=getchar" or "scanf("%c",&ch)??

    Code:
    #include <stdio.h>
    
    void call(int);
    static int s=0;
    int main(void)
    {
        int i;
        int flag=1;
        int count=1;
        char ch;
        while(flag>0)
        {
          printf("\ninput i: ");
          scanf("%d",&i);
          printf("%d times.",count++);
          call(i);
          printf("type Y to continue,N exit...");
          scanf("%c",&ch);
          //ch=getchar();
          if(ch=='Y')
            flag=0;
          else
            continue;
        }
        return 0;
    }
    
    void call(int a)
    {
       s=s+a;
       printf("s: %d\n",s);
    }
    I only want to test the key word "static",but the problem occured at here suddenly.where is my "ch=getchar()"??

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    where is my "ch=getchar()"??
    • Don't ask, where is a line in your code that you commented out or deleted.
    • For reading in one char, getchar() is good and scanf() is bad.
    • Ask C questions in C board.
    • What is your actual problem?
    Last edited by maxorator; 11-02-2006 at 03:39 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Thread moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Oh,sorry,it is "scanf("%s"...)".
    But,the problem was occured either used "scanf" or used "getchar".

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    I'm not sure I understand what you're asking, do you want to know if you should use getchar instead of scanf?.....either way you should be testing return values.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21