Thread: Please could someone help me with my 8-function calculator program?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    2

    Unhappy Please could someone help me with my 8-function calculator program?

    Hi C programmers,

    Can anyone help me with my 8-function calculator program? The 'do' loop creates problems (errors) in my program. I tried everything but to no success.

    Thank you.

    Geeth.

    The program is provided below


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

    /* This statement allows us to use the standard built in math functions */
    #include <math.h>

    /* Includes the separate functions (Log, Sine, Exp, Cosine, Addition.. */
    /* ..Subtraction, Multiplication, Division) */
    void main(void);
    void do_log(float);
    void do_sin(float);
    void do_exp(float);
    void do_cos(float);
    void do_add(float, float);
    void do_sub(float, float);
    void do_mult(float, float);
    void do_div(float, float);

    /* Introduction to program */
    void intro(void);

    /* Instructions for user */
    void instruct(void);

    /* Checks whether user has entered 'equal to', '=', sign */
    void equals_to(char);

    /* Basic operations */
    void basic(char, float, float);

    /* Scientific operations */
    void scientific(char, float);

    /*-----------------------------------------------------------------------------*/

    /* Beginning of main */
    void main(void)
    {
    /* Input numbers */
    float number1 = 9999.0, number2 = 9999.0;

    /* Selects the operation, either the basic (+, -, *, /)*/
    char operation = 'O';

    /* Equal sign */
    char equal = '^';

    /* Selects the scientific operation (Sine, Cosine, Exp, Log) */
    char soperation[3];

    /* Introduction to My Calculator program */
    intro();

    /* Instructions for user */
    instruct();

    /* Loop for the main program */
    do
    {
    printf(": ");

    /* Enter the calculation */
    scanf("%f %c %f%c", &number1, &operation, &number2, &equal);

    /* These statements check if the user has entered the '=' sign */
    /* Beginning of switch statement */
    equals_to(equal);

    /* End of switch statement */
    if (number1 != 9999.0)
    {

    /* Beginning of switch statement */
    /* Beginning of basic calculation */

    basic(operation, number1, number2);

    /* end of switch statement */
    }
    /* end of basic calculation...*/
    else
    {
    scanf("%3s %f %c",&soperation, &number1, &equal);

    /* These statements check if the user has entered the '=' sign */
    /* Beginning of switch statement */
    equals_to(equal);
    /* End of switch statement */

    /* Beginning of switch statement */
    /* Beginning of scientific calculation */
    scientific(soperation[0], number1);
    /* end of switch statement */
    }

    /* End of loop */
    /* Program terminates when the user has entered 'q' */
    }
    while (number1 != 'q');

    /* End of main */
    }

    /*-----------------------------------------------------------------------------*/

    /* Separate Functions */

    /* Introduction to My Calculator program */
    void intro(void)
    {
    printf(" My Calculator program \n");
    printf(" by Geeth Asokarajan \n");
    printf(" Version 1.0a, released 27/03/02 \n\n");
    }

    /*-----------------------------------------------------------------------------*/

    /* Instructions for user */
    void instruct(void)
    {
    printf("Please enter your calculation in either the following forms\n");
    printf(" : Number1 operation Number2 = \n");
    printf("or : operation number2 = \n");
    }

    /*-----------------------------------------------------------------------------*/

    void equals_to(char equal)
    {
    /* These statements check if the user has entered the '=' sign */
    /* Beginning of switch statement */
    switch(equal)
    {
    case'=':
    printf("Please wait! \n\n");
    break;

    default:
    printf("Invalid operator!, Please try again!\n");
    break;
    }
    }
    /* End of switch statement */

    /*-----------------------------------------------------------------------------*/

    /* Begininng of switch statement */
    /* Selecting appropriate basic functions */
    void basic(char operation, float number1, float number2)
    {

    switch(operation)
    {
    case '+':
    do_add(number1, number2);
    break;
    case '-':
    do_sub(number1, number2);
    break;
    case '*':
    do_mult(number1, number2);
    break;
    case '/':
    do_div(number1, number2);
    break;
    default:
    printf("Invalid calculation!, Please try again!\n");
    break;
    }
    }
    /* End of switch statement */

    /*-----------------------------------------------------------------------------*/

    /* Begininng of switch statement */
    /* Selecting appropriate scientific functions */
    void scientific(char soperation, float number1)
    {
    switch(toupper(soperation))
    {
    case 'Q':
    printf("My Calculator program terminated!\n");
    printf("Thank you for using My calculator!, Please try again!\n");
    exit(0);
    break;
    case 'C':
    do_cos(number1);
    break;
    case 'L':
    do_log(number1);
    break;
    case 'S':
    do_sin(number1);
    break;
    case 'E':
    do_exp(number1);
    break;
    default:
    printf("Invalid calculation!, Please try again!\n");
    break;
    }
    }
    /* End of switch statement */

    /*-----------------------------------------------------------------------------*/

    /* Beginning of Cosine function */
    void do_cos(float number1)
    {
    printf("%f\n", cos(number1));
    }
    /* End of Cosine function */

    /*-----------------------------------------------------------------------------*/

    /* Beginning of Log function */
    void do_log(float number1)
    {
    printf("%f\n", log(number1));
    }
    /* End of Log function */

    /*-----------------------------------------------------------------------------*/

    /* Beginning of Sine function */
    void do_sin(float number1)
    {
    printf("%f\n", sin(number1));
    }
    /* End of Sine function */

    /*-----------------------------------------------------------------------------*/

    /* Beginning of Exp function */
    void do_exp(float number1)
    {
    printf("%f\n", exp(number1));
    }
    /* End of Exp function */

    /*-----------------------------------------------------------------------------*/

    /* Beginning of Addition function */
    void do_add(float number1, float number2)
    {
    printf("%f\n", number1 + number2);
    }
    /* End of Addition function */

    /*-----------------------------------------------------------------------------*/

    /* Beginning of Subtraction function */
    void do_sub(float number1, float number2)
    {
    printf("%f\n", number1 - number2);
    }
    /* End of Subtraction function */

    /*-----------------------------------------------------------------------------*/

    /* Beginning of Multiplication function */
    void do_mult(float number1, float number2)
    {
    printf("%f\n", number1 * number2);
    }
    /* End of Multiplication function */

    /*-----------------------------------------------------------------------------*/

    /* Beginning of Division function */
    void do_div(float number1, float number2)
    {
    /* If the denominator is 0 then print "Division by 0 is not allowed!" */
    if (number2 !=0)
    printf("%f\n", number1 / number2);
    else
    printf("Division by 0 is not allowed!, Please try again!\n");
    }
    /* End of Division function */

    /*-----------------------------------------------------------------------------*/

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    do use_code_tags( source_code ); while( is_without_code_tags( source_code ) );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I take it you mean this while loop.
    >while (number1 != 'q');

    If I quoted this from your code:
    >float number1 = 9999.0 .......

    would you see what your problem is? What type of variable is number1? What are you comparing it to in the while test?

    And yes, please you code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM