# Problem with loop.

• 10-12-2010
Squish
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();
}
}

• 10-12-2010
Salem
What does your while loop actually do?

Compare your use of = and ==
• 10-12-2010
Squish
I see my error.