I need help with this Calculator Program ..

This is a discussion on I need help with this Calculator Program .. within the C++ Programming forums, part of the General Programming Boards category; Hi there .. Can someone help me with this Assignment .. I dunno what's the problem in it ><" There ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    Unhappy I need help with this Calculator Program ..

    Hi there ..
    Can someone help me with this Assignment .. I dunno what's the problem in it ><"
    There are no Errors.. But in every operation I use The output look like :

    Code:
    1
    You Choose Addition
    Enter two numbers to add : 3 5
    The sum = 3
    
    Thank You..
    Code:
    2
    You Choose Subtraction
    Enter two numbers to subtract : 5 7
    The difference = 5
    
    Thank You..
    In every option It seems like it took only the first number I enter ..

    Also I have Proplems in the Division Part .. :S

    Anyway .. This is the code I've written :

    First These are the operations :
    1.Addition
    2.Subtraction
    3.Multiplication
    4.Division
    5.Reminder
    6.Minimum
    7.Maximum
    8.Exit

    Using if_else_if .

    And this is the Code :

    Code:
     
    #include <iostream>
    #include <conio>
    
    int main ()
    {
    
    int x , y , opt , answer ;
    
    cout<< "*********************************"
        << "\n*  Welcome to my Calculator ^^  *"
        << "\n*  Choose What you want to do:  *"
        << "\n*    1.Addition                 *"
        << "\n*    2.Subtraction              *"
        << "\n*    3.Multiplication           *"
        << "\n*    4.Division                 *"
        << "\n*    5.Reminder                 *"
        << "\n*    6.Minimum                  *"
        << "\n*    7.Maximum                  *"
        << "\n*    8.Exit                     *"
        << "\n*********************************" << endl;
    
    cin>> opt;
    if ( opt==1)
    {
    cout<< "You Choose Addition " << endl;
    cout<< "Enter two numbers to add : " ;
    cin>> x , y ;
    answer = x + y ;
    cout<< "The sum = " << answer << endl;
    }
    else if ( opt==2 )
    {
    cout<< "You Choose Subtraction " << endl;
    cout<< "Enter two numbers to subtract : " ;
    cin>> x , y ;
    answer = x - y ;
    cout<< "The difference = " << answer << endl;
    }
    else if ( opt==3 )
    {
    cout<< "You Choose Multiplication " << endl;
    cout<< "Enter two numbers to multiply: " ;
    cin>> x , y ;
    answer = x * y ;
    cout<< "The product = " << answer << endl;
    }
    else if ( opt==4 )
    {
    cout<< "You Choose Division " << endl;
    cout<< "Enter two numbers to divide : " ;
    cin>> x , y ;
    if ( y!=0 ) 
    {
    answer = x/y ;
    cout<< "The quotient = " << answer << endl;
    }
    else
    {
    cout<< "No Division over 0 ";
    }
    }
    else if ( opt==5)
    {
    cout<< "You Choose Reminder " << endl;
    cout<< "Enter two numbers to get reminder : " ;
    cin>> x , y ;
    answer = x % y ;
    cout<< "The Reminder = " << answer << endl;
    }
    else if ( opt==6 )
    {
    cout<< "You Choose Minimum" << endl;
    cout<< "Enter two numbers to find the minimum : ";
    cin>> x , y ;
    if (x>y)
    cout<< "The minimum = " << x << endl ;
    else
    cout<< "The minimum = " << y << endl;
    }
    
    else if ( opt==7 )
    {
    cout<< "You Choose Maximum " << endl;
    cout<< "Enter two numbers to find the maximum : ";
    cin>> x , y ;
    if ( x > y )
    cout<< "The maximum = " << x << endl;
    else
    cout<< "The maximum = " << y << endl;
    }
    else if ( opt==8 )
    cout<< "You Choose Exit" << endl;
    else
    {
    cout<< "Error : Not Valid Option"  << endl;
    }
    cout<< "\nThank You.." << endl ;
    
      getch ();
      return 0 ;
    }
    Actully .. I'm in my first grade in Computer Science .. ^^"
    I'm also confused about if I can write if inside else if in Block ..

    Any help greatly appreciated..

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    comma is an operator that doesn't do what you expect it to.
    Read how to chain cin variables together in your book once more.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Salem is right. The , operator is used to separate expressions, then evaluate them left to right. This means y is undefined (uninitialized). try
    Code:
    cin >> x >> y;
    instead.
    I'm also confused about if I can write if inside else if in Block ..
    You can put anything you want inside an else if. For example, this is fine.
    Code:
    if(...) {
       if(...) {
       }
       else if(...) {
       }
       else {
       }
    }
    else if(...) {
    }
    else {
    }
    Also you should look at how to indent code. It makes it much easier to read.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    9
    omg .. It was noob mistake .. ^^"

    THANX ALOT .. Salem & linuxdude ..

    I really appreciate Your Help .. =)

  5. #5
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    For the division part of the program, you'll need to use to use floats or doubles to get it to work properly. At the moment, all ur variables are declared as ints, and so if you try working out something like 2/5, it'll churn out an answer of 0! Something similar can happen if you were to try and work out 2.4*3.7

    So you'll need to use a different type for those variables.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21