hello, i'm working on a code where i have to make a calculator. i hope you guyes can check out my code and help. I would greatly appricate any help (: .

here is what i have to do,

calculator. this calculator keeps track of a

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

double calculator ();

int main()
{
    float result, finalResult, number2;
    char operation, choise, i;
    printf("Calculator is on.\n"); /* displays message */
    result = 0.0; /* set to zero */




    for(i = operation ; i != 'r' || 'R' ;  )
    {

      printf("result = %lf\n", &result);
      fflush(stdin);
      scanf("%c%f\n", &operation, &number2);


      if (operation == '+') /* addition */
      {
          result+=number2;
          printf("result + %f = %f\n", number2, result);


       }
       if (operation == '-') /* subtraction */
       {
          result-=number2; 
          printf("result - %f = %f\n", number2, result);

       }
       if (operation == '*') /* multiplication */
       {
           result*=number2;
           printf("result * %f = %f\n", number2, result);

        }
        if (operation == '/') /* division */
        {
            result/=number2;
            printf("result / %f = %f\n", number2, result);

        }
        if (operation != '+' || '-' || '*' || '/') /* unknown operation display */
        {
            printf("Unknown Operator\n");
            printf("Re-Enter, your last line.\n");


        }

    }

        finalResult = result;

          printf("Final result = %d\n", &finalResult);







     return 0;
}



result and that starts out as 0.0. Each cycle 
allows the user to rpeatedly add, subtract, 
multiply, or divide by a secound number. the
calculation ends when the user enters the letter 
R for "result" (either in uppercase or lowercase)
. The user is allowed to do another calculation
from the beginning as often as he or she wants. 
Use the scanf for inputs.

The input format is shown in the following 
sample dialog. if the user enters any 
operator symbol other than +.-.*, or /,
then display message "UnknownOperatorException is"
thrown and the user is asked to reenter that line of
input..

Calculator is on.
        result = 0.0
        +5  
        result + 5.0 = 5.0
        result = 5.0
        *2.2
        result * 2.2 = 11.0
        result = 11.0
        % 10
        % is an unknown operation
        Reenter, your last line:
        * 0.1
        result * 0.1 = 1.1
        result = 1.1
        r
        Final result = 1.1
        Again?  (y/n)
      Yes
 Calculator is on
      result = 0.0
      +10
      result + 10.0 =10.0
      result = 10.0   
      /2
      result / 2.0 = 5.0
      updated result = 5.0
      r
      Final result = 5.0
      Again? (y/n)
      N
      End of Program    

Hello,
you have not tell us what is the problem are you facing... that is what the people will respond to... thanks

If normal Caculator, it should have two value to work on... Value1 and Value2,
if result is zero, and I do multiplication caculation, it will always be Zero... division will give you an infinite (Error ) Result... May be you should explain to us better...

Edited 1 Year Ago by jnneson

Thank you for the response, I thought i put in what was my problem but i just noticed that i did not. I made some changes to my code and my problem is that for some reason the last else if statement is ignored. When you type the letter 'R' its suppose give you the final result, but in my program you would have type "rr" instead of just one "r". For some reason, you could type any letter and it will take you out of the loop and give you the final result and ask you if you would like to do it agian, and if you type 'n' (to exit the program after you got the final result ) it will take you back to start of the program, So for some reason that if statement is ignored as well and you can type any letter and it will take you back to the start again. This program compiles fine with no errors. i would appricate any help and thank you for reading.

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


void calc(double result);

int main (double result)
{

    char operation; 


    printf("Calculator is on.\n");
    result = 0.0;










   calc(result);



    return 0;
}

void calc(double result)
{
    double number2,final;
    char operation,choice; 


    while(1){


    printf("Result = %lf\n", result);
    fflush(stdin); 
    scanf("%c%lf", &operation, &number2);



    if (operation == '+'){

        result+=number2;
        printf("Result + %lf = %lf\n", number2, result);
    }
    else if(operation == '-'){
        result-=number2;
         printf("Result - %lf = %lf\n", number2, result);
    }
    else if (operation == '*'){
        result*=number2;
        printf("Result * %lf = %lf\n", number2, result);
    }
    else if (operation == '/'){
        result/=number2;
        printf("Result / %lf = %lf\n", number2, result);
    }
    else if (operation == 'r' || 'R'){

        final = result;
        printf("Final result = %lf \n", final);
        break;
    }
    else if (operation != '+' || '-' || '*' || '/' || 'r' || 'R'){

        printf("%c is an unknown operation\n", operation);
        printf("please re-enter your last line:\n");    

    }

   }

    printf("Again? (y,n)\n");
    scanf("%c", &choice);
    getchar();
    if (choice != 'n' || 'N')
    {
        main(result);
    }

    exit(0);


}

One of your problems is here:

scanf("%c%lf", &operation, &number2);

Separate this into 2 scanf's and only do the second one if a r or R wasn't entered. Something like this:

scanf("%c", &operation);
if(operation != 'r' && operation != 'R')
{
    scanf("%f", &number2);
    ... and so on

On a side note it's very bad form to keep re-using main with a parameter like that. You would be better off keeping the loops inside main and calling any outside functions as needed. I would also recommend using a switch/case block instead of a series of if/else blocks and calling a different function for each operation, as this makes your code not only more concise but much easier to maintain. Consider the extra steps involved if you had to add more operators. Something like this would work:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

float add(float num1, float num2);
float subtract(float num1, float num2);
float multiply(float num1, float num2);
float divide(float num1, float num2);
int main()
{
    char rerun = '\0';
    bool end = false;
    while (!end)
    {
        float number2;
        char operation = '\0';
        float result = 0.0; /* set to zero */
        printf("Calculator is on.\n"); /* displays message */
        while (operation != 'r' && operation != 'R')
        {
            bool badOperator = false;
            printf("result = %.2f\n", result);
            fflush(stdin);
            scanf("%c", &operation);
            if(operation != 'r' && operation != 'R')
            {
                scanf("%f", &number2);
                switch (operation)
                {
                case '+':
                    result = add(result, number2);
                    break;
                case '-':
                    result = subtract(result, number2);
                    break;
                case '*':
                    result = multiply(result, number2);
                    break;
                case '/':
                    result = divide(result, number2);
                    break;
                default:
                    badOperator = true;
                    break;
                }
                if (badOperator)
                {
                    printf("Unknown Operator\n");
                    printf("Re-Enter, your last line.\n");
                }
                else
                {
                    printf("result %c%.2f = %.2f\n", operation, number2, result);
                }
            }
        }
        fflush(stdin);
        printf("Final result = %.2f\nAgain (Y/N)", result);
        scanf("%c", &rerun);
        if (rerun == 'N' || rerun == 'n')
        {
            printf("End of Program");
            end = true;
        }
    }
    return 0;
}

Notice the .2 in the printf format string, this limits the precision to 2 decimal places.

Edited 1 Year Ago by tinstaafl

Comments
great example and explanation!

i reall appricate the information and examples. I have learned so much on what you described to me and i am going to go ahead and try it on my own.

thank you (: .

Please remember to mark this solved. This lets someone doing a search identify valid answers and find the information he/she needs more efficiently. Thanks.

yeah will do. Here is the my code that i edited and works beautifully.

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



void repeat(double result);


int main (double result)
{

    char operation; 
    double num2,final;


    printf("Calculator is on.\n");
    result = 0.0;



    while(operation != 'r' || operation != 'R')
    {


    printf("Result = %.2lf\n", result);
    fflush(stdin); 
    scanf("%c", &operation);
      if (operation == 'r' || operation == 'R') 
      {
          repeat(result);
      }
     else

     scanf("%lf",&num2);
     switch (operation)
        {
                case '+':
                    result+=num2;
                    break;
                case '-':
                    result-=num2;
                    break;
                case '*':
                    result*=num2; 
                    break;
                case '/':
                    result/=num2;
                    break;
                default:
                   printf("Unknown operator.\n");
                   printf("please enter last line.\n");
                    break;

        }

    }

    repeat(result);

    return 0;
}




void repeat( double result)
{
    char choice;


    printf("Final result = %.2lf\n",result);
    printf("again? (y,n)\n");
    fflush(stdin);
    scanf("%c",&choice);

    if ( choice == 'n' || choice == 'N')
    {
        printf("exiting program.");
        system("pause");
        exit(0);
    }
    else
        main(result);



}
This question has already been answered. Start a new discussion instead.