I have to do a 4 function calculator for class with the do...while loop. With the option of ending the program after the while by either entering 'y' or 'n'. However the program automatically loops without seeking input to restart or not.

/* This is a four function calculator with multiplication, addition, substraction and division*/

#include<stdio.h>
#include<stdlib.h>

main()

     { 
         float a,b,c;
         char sign,answer;
         do
              {
                 printf("Enter your choice of operator\n"); 
                 printf("+ for addition\n"); 
                 printf("- for substraction\n");
                 printf("* for multiplication\n");
                 printf("/ for division\n");
                 scanf("%c",&sign);
                 printf("Enter two numbers\n");
                 scanf("%f%f",&a,&b);
                 switch(sign)
                             {
                                 case '+':
                                   {
                                     c=a+b;
                                     printf("The addition is %.1f\n",c);
                                     break;
                                   }  
                                 case '-':
                                   {
                                     c=a-b;
                                     printf("The substraction is %.1f\n",c);
                                     break;
                                   }  
                                 case '*':
                                   {
                                     c=a*b;
                                     printf("The multiplication is %.1f\n",c);
                                     break;
                                   }  
                                 case '/':       
                                   {
                                    c=a/b;
                                     printf("The division is %.1f\n",c);
                                     break;
                                   }  
                             } 
                            printf("Do you want to continue? (y or n)\n");
                            scanf("%c",answer);
                         }while (answer !='N');
              system ("pause");
         }

In C++, the string checking with == or != is case sensitive. In line 50, it will be true if and only if "answer" is not equal to 'N'. If you enter 'n', it will still loop because answer!='N'. If you want it to be case in-sensitive, you should do...

while ((answer!='N') && (answer!='n'))

I changed it and tried to run it, but by the time I get to the question. The program crashes/becomes not responding. Do you know what the problem may be?

Err... I don't know... I don't know what "crash" is cause by because I do not have your program. Please try to be more specific. Remember that I am not standing next to you and look at the same screen you are working on. I cannot see what you are seeing... That's why the forum rules state that you should explain how you get the error step-by-step, show the code, and give all detail of error statement produced by the program (either compiling or run time).

I changed the code to:

/* This is a four function calculator with multiplication, addition, substraction and division*/

#include<stdio.h>
#include<stdlib.h>

main()

     { 
         float a,b,c;
         char sign,answer;
         do
              {
                 printf("Enter your choice of operator\n"); 
                 printf("+ for addition\n"); 
                 printf("- for substraction\n");
                 printf("* for multiplication\n");
                 printf("/ for division\n");
                 scanf("%c",&sign);
                 printf("Enter two numbers\n");
                 scanf("%f%f",&a,&b);
                 switch(sign)
                             {
                                 case '+':
                                   {
                                     c=a+b;
                                     printf("The addition is %.1f\n",c);
                                     break;
                                   }  
                                 case '-':
                                   {
                                     c=a-b;
                                     printf("The substraction is %.1f\n",c);
                                     break;
                                   }  
                                 case '*':
                                   {
                                     c=a*b;
                                     printf("The multiplication is %.1f\n",c);
                                     break;
                                   }  
                                 case '/':       
                                   {
                                    c=a/b;
                                     printf("The division is %.1f\n",c);
                                     break;
                                   }  
                             } 
                            printf("Do you want to continue? (y or n)\n");
                            scanf("%c",answer);
                         }while ((answer!='N') && (answer!='n'));
              system ("pause");
         }

Noting the change from:

}while (answer !='N')

to:

}while ((answer!='N') && (answer!='n'))

Then I run the program.
It asks me to perform a function.
I enter the function.
It asks me to enter two numbers.
I enter two numbers.
The program performs the function, be it addition, subtraction, division or multiplication.
Then the programs asks me I would like to continue, either press y or n (n for it stop, y for it to continue).
But after I push N or the request to continue shows up, the program screen goes gray and says that it is not responding. So I have to close it.

Comare your input for sign and answer. Do you see anything missing?

^
I was thinking it was the '%c' because the input for:

scanf("%c",&sign);

would be -, +, * or /.

But the input for:

scanf("%c",answer);

would be n or N.

But I'm not sure.

scanf("%c",&sign);
scanf("%c",answer);

Then something else is wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.