-1
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void run_calc();
void scandata(char *op, double * operand);
void do_next_op(char op, double operand, double *akku);

int main()
{
run_calc();
return 0;
}

void run_calc(){
 double akku = 0, operand = 0;
char op, answer;
printf("Press enter to use the calculator");
printf("\n");
scanf("%c", &answer);

while(answer='q'||answer!='Q');
{
     if(answer=='q'||answer=='Q')
        {
            printf("The last result was %1.2f\n", akku);
            exit(0);
        }

        else if (answer=='h'||answer=='H')
        {
    printf("%s\t%s\n", "+" , "add");
    printf("%s\t%s\n", "-" , "subtract");
    printf("%s\t%s\n", "*" , "multiply");
    printf("%s\t%s\n", "/" , "divide");
    printf("%s\t%s\n", "^" , "power");
    printf("%s\t%s\n", "q" , "quit");
        }
        else
        {
printf("Enter an operator (+,-,/,#,^,*) and optional operand.Enter 'h' for help. Enter 'q' to exit the program.");
scandata(&op, &operand);
do_next_op(op, operand, &akku);
printf("Result so far is: %1.2lf \n", akku);
printf("\n");

        }
}
}



void scandata(char *op, double *operand) {
scanf("%c", op);
 scanf ("%lf", operand);
}

void do_next_op(char op, double operand, double *akku)
{
    switch(op)
    {

    case '+': *akku += operand; break;
    case '-': *akku -= operand; break;
    case '*': *akku *= operand; break;
    case '/': *akku /= operand; break;
    case '#': *akku = sqrt(*akku); break;
    case '^': *akku = pow(*akku,operand); break;
    case '%': *akku *=  -1; break;
    case '!': *akku =  1 / *akku; break;
    case '@': *akku = log(*akku); break;
    case 'q': printf(" The final value of akku is %1.2lf \n", *akku);  exit(0);
}
}

After i run the program and press enter or any letter. it just stucks . can someone help me please? This program is a calculator that i try to do it. This is for my homework which is due to next week. I am still new this programming. i need help and alot of improvement. Can someone point it out what i did wrong?

I think there is something wrong in run_Calc but i dont know what is wrong with it

  • 1
    `answer='q'` Is obviously a typo. But I'm not sure what you really mean that to be because that whole condition looks odd. What exactly do you intend the loop termination condition to be? – kaylum Oct 17 '20 at 04:56
  • `scanf("%c", &answer);` --> `scanf(" %c", &answer);` – chux - Reinstate Monica Oct 17 '20 at 06:19
  • Does this answer your question? [Why non-equality check of one variable against many values always returns true?](https://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true) – Sotirios Delimanolis Oct 30 '21 at 21:30

1 Answers1

2
while(answer='q'||answer!='Q');
{

This is an infinite loop. For a start, the semi-colon at the end of the first line disassociates the opening brace(a) with the while statement meaning that, if the condition is true, it will simply do nothing, forever.

And the condition is true, since answer='q' will set the answer variable to 'q' and then use that as an expression. Since 'q' is non-zero, it's considered true.

However, even if you correct this by changing the = to a != (assuming you only want to continue until user enters quit), you should use && rather than ||. No character can be both q and Q at the same time so using || would also result in the condition always being true.

I suspect what you should have had would be more along the lines of (comparison rather than assignment, get rid of the semicolon, and fix the conjunctive):

while(answer != 'q' && answer != 'Q')
{

There may well be other issues but that's the one causing your apparent "stuck" behaviour.


(a) A "naked" braced section can be used to create a new, isolated scope, such as:

{
    int xyzzy = 42;
    // Can use xyzzy here.
}
// But not here.

That's what your semicolon is doing in the code you posted.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953