program not working.....

This is a discussion on program not working..... within the C Programming forums, part of the General Programming Boards category; This program is not running..please help anyone. Thanks...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    5

    program not working.....

    This program is not running..please help anyone. Thanks
    Attached Images Attached Images  

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    It should be
    Code:
    if ( scanf("%d",&number) == 1 ) {
      // success
    } else {
      // not a number, remove some characters (say upto the next \n)
      // and try again
    }
    Oh, and next time, copy/paste your code between [code][/code] tags (like I have done).
    Don't attach a screen capture.
    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.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    5
    Quote Originally Posted by Salem View Post
    It should be
    Code:
    if ( scanf("%d",&number) == 1 ) {
      // success
    } else {
      // not a number, remove some characters (say upto the next \n)
      // and try again
    }
    Oh, and next time, copy/paste your code between tags (like I have done).
    Don't attach a screen capture.
    yeah i tried that but i want to know WHY it doesn't work with characters

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    Why what doesn't work "with characters"?

    > if ( scanf("%d",&number) == 1 )
    If you have "%d" in your scanf, and you type in "hello", then it just isn't going to scan anything.
    scanf() will return 0 (which you can test for)
    "hello" will still be on the input stream (which you need to deal with at some point)
    The contents of number are UNDEFINED, so there's no point trying to inspect number to figure out what went wrong.
    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.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    5
    Sir,I mean to ask why scanf("%c",&ch) isnt working?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,825
    Oh,

    If you mean to read the next NON-SPACE character, then you should have
    scanf(" %c",&ch);

    Your code takes the next char, unconditionally. Usually, this is the newline at the end of your input of your number (say 123\n)
    The leading space causes scanf to skip the newline and take the next character you type.


    You can also type this line (into your existing code)
    123y456y789y
    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.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    14
    You should write your code like this
    Code:
    #include <stdio.h>
    
    
    int main ()
    
    
    {
        int n;
        char ch;
        do
        {
            printf("Enter a number\n");
            scanf("%d", &n);
            {
            if (n!=0)
            printf("This ia a non-zero number\n");
            else
            printf("This is zero\n");
            }
            printf("Do you want to test more number(Y/N) : ");
            fflush(stdin);/*You need a fflush because %c will read 
                                the new line as character.*/
            scanf("%c", &ch);
        }
        while (ch=='Y'||ch=='y');
        return 0;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Quote Originally Posted by CruelSoulz
    You should write your code like this
    I beg to differ. For starters, the original code has better indentation than your "model code"

    Quote Originally Posted by CruelSoulz
    Code:
    fflush(stdin);/*You need a fflush because %c will read
                        the new line as character.*/
    The problem is that fflush(stdin) results in undefined behaviour because fflush is not defined for input streams. Refer to Salem's post #6 for a possible solution.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    14
    Quote Originally Posted by laserlight View Post
    I beg to differ. For starters, the original code has better indentation than your "model code"
    Sorry for that because i'm still a newbie to this forum. I won't do it again.

    The problem is that fflush(stdin) results in undefined behaviour because fflush is not defined for input streams. Refer to Salem's post #6 for a possible solution.
    fflush(stdin) is not defined for input streams? Can you explain more about fflush(stdin)? scanf(" %c",&ch);is not the same function as fflush(stdin)? Maybe the Cprogramming book that i read is confusing me or something.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by CruelSoulz View Post
    fflush(stdin) is not defined for input streams? Can you explain more about fflush(stdin)? scanf(" %c",&ch);is not the same function as fflush(stdin)? Maybe the Cprogramming book that i read is confusing me or something.
    Check our FAQ.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by CruelSoulz View Post
    Maybe the Cprogramming book that i read is confusing me or something.
    What's the title of the book (so that other beginners can avoid it)?

    Bye, Andreas
    Last edited by AndiPersti; 07-02-2012 at 12:47 PM.

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Taking bets on "Let Us C".

  13. #13
    Registered User Unbr0ken's Avatar
    Join Date
    Jul 2012
    Location
    Venezuela
    Posts
    1
    Quote Originally Posted by CruelSoulz View Post
    You should write your code like this
    Or like this:

    Code:
    #include <stdio.h>
    #define flushBuffer() while (getchar() != '\n');
    
    int main ()
    {
        int n;
        char c;
    
        do
        {
            printf("Enter a number: ");
            scanf("%d", &n);
    
            if (n != 0) printf("\nThis is a non-zero number.");
            else printf("\nThis is zero.");
    
            printf("\n\nDo you want to test more numbers (Y/N): ");
            flushBuffer();
            scanf("%c", &c);
    
        } while (c == 'Y' || c == 'y');
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is my program not working?
    By Sourabh Verma in forum C Programming
    Replies: 5
    Last Post: 02-19-2012, 11:19 AM
  2. Why is my program not working ?
    By swansea in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2010, 04:40 PM
  3. Program not working
    By HAssan in forum C Programming
    Replies: 5
    Last Post: 01-11-2009, 02:45 AM
  4. gpa program not working
    By matthughes in forum C Programming
    Replies: 8
    Last Post: 05-20-2007, 01:11 PM
  5. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 04:21 AM

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