# Thread: Can someone help to fix my simple calculator program?

1. ## Can someone help to fix my simple calculator program?

I am new to programming, so correct me if I am in the wrong. Can someone please me to solve the problem below. I am creating a simple calculator.

#include <stdio.h>

void main()
{
char opr;
float ans;
float num;
int counter=0;

do{
counter++;

do{

scanf("%f", &num);

if(counter > 0)
{
if(opr=='+')
ans += num;
if(opr=='-')
ans -= num;
if(opr=='*')
ans *=num;
if(opr=='/')
ans /= num;
}

else

scanf("%[+-*/=]", &opr);
if(counter==1)
{
ans=num;
}
counter++;
}while(opr!='=');

if(opr=='=')
printf("Ans is %f", ans);

}while(getchar()!='Q');
if (getchar()=='Q')
{
printf("Bye\n");
}

}

2. ## Re: Can someone help to fix my simple calculator program?

Originally posted by Aven
I am new to programming, so correct me if I am in the wrong. Can someone please me to solve the problem below. I am creating a simple calculator.
What problem? You need to tell us whats wrong....

Please use code tags when posting code (see my sig).

>void main()
is wrong, use
>int main(void)

3. The program keeps on asking for input value non stop

>scanf("%f", &num);
You ask for an int, but scanf() for a float. Make up your mind which one!

>if(opr=='+')
at this point, opr has not been assigned a value, so you cannot test it and guarantee it the correct response.

When you layout your code correctly, you can see a logic problem here:
Code:
```if(counter > 0)
{
if(opr=='+')
ans += num;
if(opr=='-')
ans -= num;
if(opr=='*')
ans *=num;
if(opr=='/')
ans /= num;
}
else scanf("%[+-*/=]", &opr);```
counter is always greater than 0, therefore you won't be scanf()'ing for opr.

There maybe some more, but it'd be best if you sort the logic out first....

5. The meaning of this program is first the compiler goes in for the first time, it checks for the condition of the if(counter>0), but instead the conuter starts from counter=0, so it will skip this part since it is false.

After that it will go to the else statement instead.Here it will ask the user to input an operator. Nest, assign the value from num to ans and the counter will be incremented.

Now, the compiler will go in the second time, it will ask the user to input the second number. This time counter is set to 1, so if(counter>0) which is 1>0, so the +,-,*,/ operators which the user input previously will be used to perform the calculation. If the users decide to insert the = operators, then the ans will display the real answer.

The counter variable is initialised to 0 here
>int counter = 0;
but the next thing you do is this
>counter++;
So counter is now 1. This is (one of) your problems.

Also, you don't reset counter to 0, so the user will not get the chance to enter another opr. I don't know if this is what you intended or not? (I presume not, as it must at some point be set to the equals sign)

7. Now, if I get ride of counter++ after do{, I wil still continue to have the which is my pogram keeps asking to insert value.

Code
-----------------------------------------------------------------------------------
do{

do{

scanf("%f", &num);

if(counter > 0)
{
if(opr=='+')
ans += num;

if(opr=='-')
ans -= num;

if(opr=='*')
ans *=num;

if(opr=='/')
ans /= num;
}

else

scanf("%[+-*/=]", &opr);

if(counter==0)
{
ans=num;
}
counter++;
}while(opr!='=');

__________________________________________________ __

8. I think you should print a prompt for the user to enter the operator:

Code:
```{
printf ("Enter operator: ");
scanf("%[+-*/=]", &opr);
}```
Personally, I wouldn't use scanf() in this manner though. I'd probably use getchar() or fgets(), and validate the input myself.

Anyway, adding this prompt will show you that the program does ask you for the opr, but appears to jump straight over it. This is because of the characters left in the input stream from the previous call the scanf(). You can try flushing the input buffer with
>while (getchar() != '\n');