Thread: Problem with loop.

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Question Problem with loop.

    I'm trying to code a basic calculator.
    I'm not sure what I've done wrong with this code; could someone please help?
    Thanks

    Code:
    #include <iostream>
    
    using namespace std;
    
    float add( float x, float y )
    {
          return x + y;
    }
    
    float subtract( float x, float y )
    {
          return x - y;
    }
    
    float multiply( float x, float y )
    {
          return x * y;
    }
    
    float divide( float x, float y )
    {
          return x / y;
    }
    
    int main()
    {
        int s, x, y, l;
        l = 0;
        while ( l = 0 )
        {
              cout<<"What would you like to do?\n\n";
              cout<<"1. Add, 2. Subtract, 3. Multiply, 4. Divide\n";
              cin>> s;
              cin.ignore();
              if ( s == 1 )
              {
                   cout<<"\nYou would like to add.\n\n";
                   cout<<"Enter two numbers.\n";
                   cin>> x >> y;
                   cin.ignore();
                   cout<<"\nAnswer... "<< add( x, y ) <<"\n";
                   l++;
              }
              else if ( s == 2 )
              {
                   cout<<"\nYou would like to subtract.\n\n";
                   cout<<"Enter two numbers.\n";
                   cin>> x >> y;
                   cin.ignore();
                   cout<<"\nAnswer... "<< subtract( x, y ) <<"\n";
                   l++;
              }
              else if ( s == 3 )
              {
                   cout<<"\nYou would like to multiply.\n\n";
                   cout<<"Enter two numbers.\n";
                   cin>> x >> y;
                   cin.ignore();
                   cout<<"\nAnswer... "<< multiply( x, y ) <<"\n";
                   l++;
              }
              else if ( s == 4 )
              {
                   cout<<"\nYou would like to divide.\n\n";
                   cout<<"Enter two numbers.\n";
                   cin>> x >> y;
                   cin.ignore();
                   cout<<"\nAnswer... "<< divide( x, y ) <<"\n";
                   l++;
              }
              else
              {
                   cout<<"\nInvalid selection.\n\n";
              }
              cin.get();
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What does your while loop actually do?

    Compare your use of = and ==
    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.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    I see my error.
    Thanks for your quick reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple loop problem I cant seem to solve
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 06:59 PM
  2. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  3. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  4. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  5. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM