Thread: program not working.....

  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 program not working.....-capture-png 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    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.

  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
    39,665
    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.

  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
    39,665
    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.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,909
    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 11:47 AM.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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, 12:11 PM
  5. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM