Insights Blog
-- Browse All Articles --
Physics Articles
Physics Tutorials
Physics Guides
Physics FAQ
Math Articles
Math Tutorials
Math Guides
Math FAQ
Education Articles
Education Guides
Bio/Chem Articles
Technology Guides
Computer Science Tutorials
Forums
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Trending
Featured Threads
Log in
Register
What's new
Search
Search
Search titles only
By:
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Menu
Log in
Register
Navigation
More options
Contact us
Close Menu
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Other Sciences
Programming and Computer Science
I'm not understanding how my C-calculator is working
Reply to thread
Message
[QUOTE="Eclair_de_XII, post: 6614640, member: 538457"] [B]TL;DR Summary:[/B] I am tasked to write a calculator. It will accept user input in the form: (operator) (operand). The range of operations include: set (s), end (e), and the usual arithmetic operations. [CODE=c]#include <stdio.h> void calculate(float *accumulator,char operator,float number){ switch(operator){ case 's': *accumulator=number; break; case '+': *accumulator+=number; break; case '-': *accumulator-=number; break; case '*': *accumulator*=number; break; case '/': if (number != 0) *accumulator/=number; else printf("Stop dividing by zero!\n"); break; } } int main(void){ float number,*accumulator; char operator='\0'; printf("Begin Calculations\n"); while (operator != 'e') { scanf("%c %f",&operator,&number); calculate(accumulator,operator,number); printf("= %.6f\n\n",*accumulator); } return 0; }[/CODE] When I run this code, it seems to read the print statement twice on subsequent calculations. Moreover, when I need to add or subtract things, I will need to type in the appropriate operator symbols twice. I do not know why that is. EDIT: For some reason, it seems to function properly if I transpose the order of the scanf arguments; but it does go on in an infinite loop if I give erroneous input. But this does not answer my question. [/QUOTE]
Insert quotes…
Post reply
Forums
Other Sciences
Programming and Computer Science
I'm not understanding how my C-calculator is working
Back
Top