# Thread: Calculator Program To Allow User 3 Inputs

1. ## Calculator Program To Allow User 3 Inputs

I know I'm probably writing questions along the same thing every time, but there is still stuff I don't get.

Code:
```#include<stdio.h>

int main()

{
int num1, num2, sum, count; /*Assign int variables of num1, num2, sum, count*/
count = 0;
char dummy, operation; /*Assign char variables of dummy, operation*/

printf("Enter first number:"); /*Ask user for 1st number*/
printf("Enter operation (+ or - or * or /):"); /*Ask user for operation*/
scanf("%c%c%c", &dummy, &operation, &dummy); /*Read operation*/
printf("Enter second number:"); /*Ask user for 2nd number*/

while (count <= 2); /*While count is smaller than or equal to 2 program loops*/

{

if ( operation == '+' ) /*If condition if operation is +*/
{
sum = num1 + num2; /*Sum if addition*/
printf("The sum of %d and %d is %d" ,num1, num2, sum); /*Print answer to screen*/
}

if ( operation == '-' ) /*If condition if operation is -*/
{
sum = num1 - num2; /*Sum if subtraction*/
printf("Subtracting %d from %d is %d" ,num1, num2, sum); /*Print answer to screen*/
}

if ( operation == '*' ) /*If condition if operation is **/
{
sum = num1 * num2; /*Sum if multiplication*/
printf("Multiplying %d by %d is %d" ,num1, num2, sum); /*Print answer to screen*/
}

if ( operation == '/' ) /*If condition if operation is /*/
{
sum = num1 / num2; /*Sum if division*/
printf("Dividing %d by %d is %d" ,num1, num2, sum); /*Print answer to screen*/
}

count = count + 1; /*count adds one*/
}

return 0;
}```
This will compile, and will go so far as the second number input but then the program just stalls.

I think if I crack this one I might be on the way to doing the others because whatever error I'm committing here is probably holding me back elsewhere.

I understand somebody told me to use else but I tried that with last week's assignment and the basic program (without allowing 3 inputs) wouldn't work so I guessed I had to use four ifs. To do otherwise now would lead to me being even more confused I think.

2. Go to line 20 and remove semicolon.
Placing semicolon right after while(),means that loop and do nothing.

I haven't read the rest but you may have issues when reading chars instead of ints. (line 15)

3. You should clear the inputbuffer after every scanf.

4. Program now works fine, except it doesn't loop. Its like the while condition isn't there???

5. Do you know how to use your debugger? You can walk through your code and see what it going on.

EDIT: works for me, why are you looping anyways? Do you want this program to perform 3 different calculations?

6. Originally Posted by Interista
Program now works fine, except it doesn't loop. Its like the while condition isn't there???

7. Code:
`scanf("%c%c%c", &dummy, &operation, &dummy); /*Read operation*/`
What are you trying to do with this dummy character stuff? There's probably a better way.

Code:
`while (count <= 2); /*While count is smaller than or equal to 2 program loops*/`
Ditch that semicolon, it's causing an infinite loop.

Code:
`if ( operation == '+' ) /*If condition if operation is +*/`
I applaud your attempt to properly document your code, but stating the obvious in your comments is almost as bad as not commenting at all. Comments should only explain what is not obvious from reading the code. You can actually remove every single comment from your program, and it would still be easily understood by anybody with even a passing knowledge of C.

8. Originally Posted by rmatze
Do you know how to use your debugger? You can walk through your code and see what it going on.
Excuse me, but what's a debugger?

Believe me, the course I'm on, if they can teach something they don't. They prefer to tell us a bunch of **** that has no relevance to the assignments we have to do then leave us drown.

9. Originally Posted by Interista
Excuse me, but what's a debugger?
You're excused . A debugger is probably the most useful tool in a programmer's belt. It allows you to step through your program one instruction at a time, and examine all kinds of stuff about the program while it's running, like what values are in certain variables. A good debugger does much, much more, but those are the most basic and useful features. You probably have one on your system. It often comes with the compiler.

10. I'll definitely have a look at that.

But is there any quick way to fix my code?

I'm really up against the deadline for this week's assignment and need to get my stuff in quick. I still have 5 of 10 programs to write and am not getting any less confused.

11. Originally Posted by Interista
But is there any quick way to fix my code?
See my response in post #7.

12. Its now telling me its expecting a declaration at the end of my statement.

I've been at these 5 assignments for about 15 hours in total. I'm at breaking point. I really think I'll quit the whole course to be honest. Is not understandable at all. That's not an insult to you, you've been really helpful, but if I don't get this quick then my run is over.

13. Did you remove the semicolon at the end of your while statement? That was the big error. When I did that, it compiled with no errors or warnings, and produced the expected output. You need to post your new code and your exact error message (copy-paste the full message with the line number).

14. It compiles and runs, but instead of offering three opportunities to input number, operation, number, it just prints the answer 3 times.

15. Well, look at the structure of your code. Where do you ask for input? Where is your loop? Learning to correlate program behavior with what the source code says is a fundamental skill for writing and debugging code.