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="Mark44, post: 6614790, member: 147785"] The main thing wrong with this code is the declaration of accumulator as an uninitialized pointer. That means that after the first call to scanf(), the program will try to store the input number at some random location. When I attempt to compile your code using VS 2017, the compiler generates an uninitialized local variable error. To fix this problem, I made this change to main(): [code=c]int main(void) { float number, accumulator = 0.0f; // accumulator is no longer a pointer, and it's now initialized. char operator='\0'; printf("Begin Calculations\n"); while (operator != 'e') { scanf("%c %f", &operator,&number); calculate(&accumulator, operator,number); printf("= %.6f\n\n", accumulator); // I also changed this line. accumulator is no longer dereferenced. } return 0; }[/code] To address the problem you were having, it's usually a very bad idea to use scanf() to input to more than one variable, not to mention that the program does not issue a prompt that tells the user what should be entered. The use of scanf() also has been strongly discouraged in Visual Studio for many years, as it is prone to being used by hackers in buffer overrun attacks. The problem you are seeing arises directly from your use of scanf() to input a char and a number. In the first call to scanf(), if I enter, say, +[space]2.2[Enter], [B]operator[/B] will be set to '+' and [B]number[/B] will be set to 2.2. [U]The problem is that the input buffer still has a character in it; namely, the Enter character I typed[/U](actually ASCII 10, '\n'). A workaround for this problem is to get rid of the final character in the input buffer before the next call to scanf(). [code=c] while (operator != 'e') { scanf("%c", &operator); scanf("%f", &number); scanf("%c", &junk); // Gobble up the final newline character, '\n' calculate(&accumulator, operator,number); printf("= %.6f\n\n", accumulator); }[/code] Of course, you need to declare junk... [/QUOTE]
Insert quotes…
Post reply
Forums
Other Sciences
Programming and Computer Science
I'm not understanding how my C-calculator is working
Back
Top