Just use printf to build the format string and then - scanfSo you have to apply petty syntax tricks to make it work.
Just use printf to build the format string and then - scanfSo you have to apply petty syntax tricks to make it work.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I do not buy that argument since the syntax is less complicated than many regex rules in use today. It basically specifies a list of characters to match, with the ^ negating it to not match any in the list of characters. That a preceding number specifies the number of characters to match is also easy to grasp.Whatever you say, but I can remember %s, but not %[\n..], whatever.
In my opinion, what makes scanf() less desirable for reading in a string as compared to fgets() is that it is more cumbersome to specify the length of the string. You either have to hardcode it into the format string, or you build the format string with sprintf() as noted by vart. With fgets(), you just pass the length of the string as an argument.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Too complex to me. Doesn't matter if it's less complex to X, if I can't remember it, then the function is really no use to me, is it (emphasis on me, here)?
That's why we usually pass size as an argument instead.
Disclaimer: this only applies to me in y humble opinion.
ok is there anything with this now.
Code:#include<stdio.h> #include<stdlib.h> int main() { int c; char operation; float x; float y; float answer; printf(" Welcome to TJ's Calculator.\n"); printf(" Please type the operation, and then the two numbers.\n"); scanf("%c",&operation); scanf("%f",&x); scanf("%f",&y); while((c = getchar()) != '\n' && c != EOF); { if(operation='+') { answer=x + y; } else if (operation='*') { answer=x * y; } else if (operation='-') { answer=x - y; } else { answer=x / y; } } printf(" The answer is %f!\n",answer); printf(" Thankyou for using TJ's Calculator.\n"); while((c = getchar()) != '\n' && c != EOF); return(0);
Missing indentation
missing closing breket
missing space in thankyou
printing of uninitialized answer when operation is different from one of supported
using of if/else sequence where you should really use switch
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
To continue . . .
- use of single-equals assignment rather than double-equals comparison in if statements;
- unnecessary block enclosing the if/else construct;
- no check for zero in division, so division by zero is a real possibility;
- unnecessary header file <stdlib.h> included. (Okay, that's really being pedantic.)
Ah, but did you have a look at the original program? There's a world of a difference. In the positive direction.Missing indentation
Not to mention extra spaces at the beginning of each printf() . . . .missing space in thankyou
Ah, not quite. Look closely: division is the default.printing of uninitialized answer when operation is different from one of supported
A switch might be more suited to this particular situation, but of course if/else works as well.using of if/else sequence where you should really use switch
But on the whole -- it's much better than your first attempt. The only serious language issues are the use of '=' instead of '==' in the if statements and the lack of a final closing curly brace, though that could have been a copy-and-paste error. Everything else is pretty minor . . . with the possible exception of /0.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.