Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,557 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,600 people online right now. Registration is fast and FREE... Join Now!




Simple Calculator

 
Reply to this topicStart new topic

Simple Calculator, In this program i want to input +, -, * and divide and then the come r

salman
21 Nov, 2007 - 12:13 AM
Post #1

New D.I.C Head
*

Joined: 21 Nov, 2007
Posts: 19


My Contributions
CODE
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main ()
{
    int a, b, c, choice;
    clrscr ();
    printf ("\t\t\t\t ** Calculation Program ** ");
    printf ("\n\n\n\nEnter First Value: ");
    scanf ("%d", &a);
    printf ("\n\nEnter Second Value: ");
    scanf ("%d", &b);
    printf ("\n\n\t1. Addition");
    printf ("\n\n\t2. Subtraction");
    printf ("\n\n\t3. Multiplication");
    printf ("\n\n\t4. Divide");
    printf("\n\n\t5. Exit");
    printf ("\n\n\nSelect Your Choice: ");
    scanf ("%d", &choice);
    switch (choice)
    {
        case 1:
            c = a + b;
            break;
        case 2:
            c = a - b;
            break;
        case 3:
            c = a * b;
            break;
        case 4:
            c = (float)a / b;
            break;
        case 5:
            printf ("\n\nThank Your For Using This Program");
            getch ();
            exit (0);
        }
        if(choice>=6)
        {
            printf("\n\nInvalid Key!!!");
            printf("\n\n\nTry Again Later.");
            getch ();
            exit (0);
        }

    printf ("\n\nResult = %d", c);
    printf ("\n\n\nThank You!!!!!");
    getch ();
}


mod edit: added code tags: code.gif
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Simple Calculator
21 Nov, 2007 - 09:30 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
what is this?
Looks like it should work, so it isn't a question?
Are you just posting the code?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Simple Calculator
21 Nov, 2007 - 09:33 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
You might want to do some validation on the numbers in the divide case. Dividing by zero causes some severe headaches.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Simple Calculator
21 Nov, 2007 - 09:35 AM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,127



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Wow, this really takes me back to the days of learning C on my 486 Dell Laptop running Slackware.

These kinds of programs are great ego builders, where you get to see instant success & get that warm glowish feeling all-around.

warnings: 0
errors: 0

YAY!


QUOTE(Amadeus @ 21 Nov, 2007 - 11:33 AM) *

Dividing by zero causes some severe headaches.

That's just a myth
User is online!Profile CardPM
+Quote Post

salman
RE: Simple Calculator
21 Nov, 2007 - 10:16 AM
Post #5

New D.I.C Head
*

Joined: 21 Nov, 2007
Posts: 19


My Contributions
CODE

#include <stdio.h>
#include <conio.h>
void main()
{
    clrscr ();
    float a, b, c;
    char choice;
    printf("Enter the first number");
    scanf("%f",&a);
    printf("Enter the second number");
    scanf("%f",&b);
    printf("Enter Your Operator: ");
    scanf ("%c", &choice);
if (choice == '+')
{
    c = a + b;
}
else if (choice =='-')
{
    c = a - b;
}
else if (choice =='*')
{
    c = a * b;
}
else if (choice =='/')
{
    c = (float)a / b;
}
else
    c = 0;

    printf("Result = %.2f", c);
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Simple Calculator
21 Nov, 2007 - 10:27 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
Please elaborate as to the problem you are encountering.

Topics merged. Please try to avoid duplicate topics.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:04PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month