Problem is that although everything else works fine, when the question "Do you want to continue ?(y or no)" is asked, it doesn't take in the answer and straight away goes ahead to provide the options again.Help!!!!!
That's because the char you entered is still in the buffer. It's a bug in scanf(), you shouldn't use it. Link
Also:
-the defenition of main is int main(void)
-What is that bracket on the first line?
- clrscr(); isn't standard nor is Conio.h
-main should end with a return 0;
-please use -codetags- the next time you post
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
>It's a bug in scanf(), you shouldn't use it.
It's not a bug in scanf, it's a bug in the code that uses scanf. But I do agree that if you don't know how to use something, you shouldn't use it until you do.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>Try put a space before the %c
In my experience, if you say "Try ", you don't really know what's going on. If you do know that your suggestion works and why it works, please be sure to explain it so that you don't encourage inserting random characters with the "try it and see" attitude.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Read this and the related articles for a better understanding of scanf.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
actually, i would recommend to use cin and cout, but i assume you are being taught to work with scanf and printf, so i will provide a secondary solution, which i wouldn't recommend to an experienced programmer...
printf("Do you want to continue?(y or no)\n");
getchar();
scanf("%c",&answer);
i works perfectly, as i said... but i wouldn't use it myself...
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
cin and cout don't belong to C language...
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
>actually, i would recommend to use cin and cout
Those tend not to work in C. Judging from the code, I'd wager the OP is not using C++.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I'd wager the OP is not using C++.
there's where i will not agree... I can bet the OP is using Turbo C++...That's exactly the same kind of code they were making me use when i started on Turbo... that's why i came out with that solution... (actually that's what i did when the buffer was filled up...)
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
>I can bet the OP is using Turbo C++...
*sigh*
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
lol
come on... give me a break... where's the competition spirit?? haha (not that i would like to compete with you... i'm certain you would beat the hell out of me...)
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
Some battles aren't worth winning. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Here is a code fragment which will assist you:
#include <stdio.h>
main()
{
#define ADD 1;
#define SUB 2;
#define MUL 3;
#define DIV 4;
char response[10];
char dummy[2]; /*** scanf trick buffer ***/
int first_op,
secnd_op,
operation;
/* query user */
printf("Please enter first operand.....\n");
printf("calc:==> ");
scanf("%d",&first_op);
gets(dummy);
printf("Here is the REAL value after tricking scanf:===> %d\n", first_op);
}
Do this iteration, scanf() then get(s), and you will be fine. (I had the same scanf problem in 1988)
Also, use the DEFIN
kxh29
Junior Poster in Training
56 posts since Apr 2007
Reputation Points: 10
Solved Threads: 1
>Here is a code fragment which will assist you
I doubt it. That code is awful.
>main()
int main ( void )
>#define ADD 1;
>#define SUB 2;
>#define MUL 3;
>#define DIV 4;
Ending a macro with a semicolon is the path to disaster and cryptic error messages.
>char dummy[2]; /*** scanf trick buffer ***/
If you're using a buffer to trick scanf anyway, why not just eschew scanf in favor of something more suitable?
>printf("calc:==> ");
If you don't print a newline, you need to flush stdout. Otherwise the prompt may not be visible by the time scanf blocks for input.
>gets(dummy);
gets is evil. Don't ever use it. There are no excuses.
>}
Even though you cleverly used an implicit int in defining main, it still returns int.
Try this on for size:
#include <stdio.h>
int main ( void )
{
char buffer[BUFSIZ];
printf ( "Enter an expression (number operator number): " );
fflush ( stdout );
if ( fgets ( buffer, sizeof buffer, stdin ) != NULL ) {
int a, b;
char op;
if ( sscanf ( buffer, "%d %c %d", &a, &op, &b ) == 3 ) {
switch ( op ) {
case '+': printf ( "%d\n", a + b ); break;
case '-': printf ( "%d\n", a - b ); break;
case '*': printf ( "%d\n", a * b ); break;
case '/':
if ( b != 0 )
printf ( "%d\n", a / b );
else
printf ( "Cannot divide by zero\n" );
break;
default:
printf ( "Unknown operation\n" );
break;
}
}
}
return 0;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
First of all , it is a C program
darn... that was a close one... :PYes i know char accepts only single and wht u suggested Gel is exactly wht I tried but it did not work.Why don't you try reading it as a string, even though it is a char? this should end with the buffer problem... this way:
scanf("%s",&answer);
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57