This program is not running..please help anyone. Thanks
This program is not running..please help anyone. Thanks
It should be
Oh, and next time, copy/paste your code between [code][/code] tags (like I have done).Code:if ( scanf("%d",&number) == 1 ) { // success } else { // not a number, remove some characters (say upto the next \n) // and try again }
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.
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.
Sir,I mean to ask why scanf("%c",&ch) isnt working?
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.
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; }
I beg to differ. For starters, the original code has better indentation than your "model code"Originally Posted by CruelSoulz
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.Originally Posted by CruelSoulz
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Taking bets on "Let Us C".
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; }