Jump to content

Simple calculator programming help.

- - - - -

  • Please log in to reply
11 replies to this topic

#1
ivan711

ivan711

    Newbie

  • Members
  • Pip
  • 5 posts
Guys i gt an error in my codes, it can build n compile bt it nt working properly..
N one more thing guys am really newbie in programming n to tis forum. hope u all can lead me a helping hand. Here is de codes.


#include <stdio.h>

int add( int, int);
int sub( int, int);
int product( int, int);
int divide( int, int);

int main()
{
    int num1, num2;
    char operation, choice;

    printf("\n\nSimple Calculator Program\n");

        printf("\nEnter a number =>\t");
        scanf("%d", &num1);
        printf("\nEnter your operator (+, -, *, /), Followed by another number =>\t");
        scanf("%d%c%d", &num1, &operation, &num2);

        if (operation == '+')
            printf("Current value:\t%d", add(num1,num2));

        if (operation == '-')
            printf("Current value:\t%d", sub(num1,num2));

        if (operation == '*')
            printf("Current value:\t%d", product(num1,num2));

        if (operation == '/')
            printf("Current value:\t%d", divide(num1,num2));

        printf("\nDo you wish to continue[y/n]\t");
        scanf("%s",&choice);
        if(choice=='y'||choice=='Y')
            main();
}

    int add( int num1, int num2)
    {
        return(num1+num2);
    }

    int sub( int num1, int num2)
    {
        return(num1-num2);
    }

    int product( int num1, int num2)
    {
        return(num1*num2);
    }

    int divide( int num1, int num2)
    {
        return(num1/num2);
    }

Edited by Alexander, 12 June 2011 - 08:39 PM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
How do you know it's not working properly? What inputs are producing incorrect results?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
robbiewoods05

robbiewoods05

    Newbie

  • Members
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell
#include <stdio.h>


int add( int, int);

int sub( int, int);

int product( int, int);

int divide( int, int);


int main()

{

int num1, num2;

char operation, choice;


printf("\n\nSimple Calculator Program\n");


printf("\nEnter a number =>\t");

scanf("%d", &num1);

printf("\nEnter your operator (+, -, *, /), Followed by another number =>\t");

scanf("%d%c%d", &num1, &operation, &num2);


if (operation == '+')

printf("Current value:\t%d", add(num1,num2));


if (operation == '-')

printf("Current value:\t%d", sub(num1,num2));


if (operation == '*')

printf("Current value:\t%d", product(num1,num2));


if (operation == '/')

printf("Current value:\t%d", divide(num1,num2));


printf("\nDo you wish to continue[y/n]\t");

scanf("%s",&choice);

if(choice=='y'||choice=='Y')

main();

}


int add( int num1, int num2)

{

return(num1+num2);

}


int sub( int num1, int num2)

{

return(num1-num2);

}


int product( int num1, int num2)

{

return(num1*num2);

}


int divide( int num1, int num2)

{

return(num1/num2);

}

isn't %d for doubles not ints?

#4
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
It seems that the problem is in the way of insertion.
First remove the additional insertion,

printf("\nEnter a number =>\t");

scanf("%d", &num1);


Now insert the expression combinedly and press enter at the end,

2+6 [enter]


By doing this you will get,

Current value:    8


Use a while loop instead of calling main() again; This will define the variables again -- occupying different memory location (Probably the same).
Try to create the same program in different ways, using if-else, switch.

Hope this helped!
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#5
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Another thing is to check for division by zero, unless you've figured out how to. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#6
ivan711

ivan711

    Newbie

  • Members
  • Pip
  • 5 posts
when i input first num den followed by operation with second number it nt calculating n produce result..

#7
ivan711

ivan711

    Newbie

  • Members
  • Pip
  • 5 posts
i do gt this method.. bt de task give to me to ask the user to put first input den second instruction to input operation together with second number..after that only produce result. Bt it nt showing any result bt direct the user to continue or nt..

#8
ivan711

ivan711

    Newbie

  • Members
  • Pip
  • 5 posts
Seriously i duno abt tat..

#9
ivan711

ivan711

    Newbie

  • Members
  • Pip
  • 5 posts
robbiewoods05 : as i checked %d is for int while &f(printf) n &lf(scanf) for double

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You should call add() or appropriate functions within your if statements for the operator. main() will call the input again or worse.

Your code seems alright for now, try doing this and give us your new code even if it does not work as expected so that you may learn from it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#11
robbiewoods05

robbiewoods05

    Newbie

  • Members
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell
i though %i was for an int but i'm probably wrong

#12
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

robbiewoods05 said:

i though %i was for an int but i'm probably wrong
i and d are synonymous, although i is more clear (an integral numeric.) decimal (or denary, %d) means in tens or base ten, including base ten fractions.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users