Sunday, August 2, 2009

C++ calculator program?

I made this c++ calculator program, but for some reason it only multiplies. How can this be fixed? Here's the Program:


#include %26lt;iostream%26gt;


using namespace std;





double operate (double x, double y)


{


return (x*y);


}





float operate (float m, float n)


{


return (m/n);


}





int operate (int a, int b)


{


return (a+b);


}





long operate (long c, long d)


{


return (c-d);


}


int main(int argc, char *argv[])


{


double x, y;


float m, n;


int a, b;


long c, d;


char operation;


cout %26lt;%26lt; "Choose operation" %26lt;%26lt;endl;


cout %26lt;%26lt; "1: *, 2:/, 3:+, 4: - "%26lt;%26lt;endl;


cin %26gt;%26gt; operation;





while (operation = 1)


{


cout %26lt;%26lt; "enter x:" %26lt;%26lt;endl;


cin %26gt;%26gt; x;


cout %26lt;%26lt; "enter y:" %26lt;%26lt;endl;


cin %26gt;%26gt; y;


cout %26lt;%26lt; operate (x,y) %26lt;%26lt; endl;


cout %26lt;%26lt; "Choose operation" %26lt;%26lt;endl;


cout %26lt;%26lt; "1: *, 2:/, 3:+, 4: - "%26lt;%26lt;endl;


cin %26gt;%26gt; operation;


}





while (operation = 2)

C++ calculator program?
This is the easiest mistake to make. while (operation = 1) is actually RESETTING operation to 1. You need to do (operation == 1) instead.





Even experienced developers make this typo. To prevent it, get in the habit of putting the const first. While (1 == operation). The compiler will prevent you from ever messing up again.





The second problem is that operation is a char, but you are comparing it to an int. The compiler doesn't complain, because under the covers a char is really an int of the ASCII value of the char. If you fix the = to ==, you will fall through. You need to write while ('1' == operation) or define operation as an int instead of a char.





Once you fix that, you should be good to go. There is no problem with ambiguity with the operate functions. Your overloads are fine.
Reply:try operator overloading. . .it might make the program a bit shorter. . .
Reply:All your functions are called "operate", which is confusing, rename them to something more meaningful "MyMultiply", "MyDivide" etc.





Also, your while loop is only spinning if "operation" is "1". So it needs to loop if "operation" is less than four.





You'll will also need some extra logic inside the loop to determine the correct function to call.
Reply:Okay, going to try and tackle a couple of your issues, some which have already been mentioned:





1. Your while loops are using assignment "=" instead of comparison.





2. It seems that you are attempting to do operator overloading. You will call the different "operate" functions based on what types you feed in. Since you are stuck in your first while loop, you are always feeding in doubles 'x' and 'y' and therefore will always call your operator that deals with doubles.





3. The general structure of your program is a bit odd. It seems that what you generally want to do is loop infinitely(hopefully this is what you meant to do). If that is the case, you need a single while loop. Your current setup would make it so that you could at best, do some number of multiplies, then some number of divisions, then some addition, and then some subtraction, but you could never do addition, then multiplication, etc. Use a case/switch statement if you are familiar with it. If you aren't, you can use if statements. Then main thing is just to have one overall loop.
Reply:Why are you using While? While is a loop.





Change the while to a an If statement. Or better yet change to a switch statement (If you covered switch statements in your class).





switch (operations)





case '1'


{


cout %26lt;%26lt; "enter x:" %26lt;%26lt;endl;


cin %26gt;%26gt; x;


cout %26lt;%26lt; "enter y:" %26lt;%26lt;endl;


cin %26gt;%26gt; y;


cout %26lt;%26lt; operate (x,y) %26lt;%26lt; endl;


cout %26lt;%26lt; "Choose operation" %26lt;%26lt;endl;


cout %26lt;%26lt; "1: *, 2:/, 3:+, 4: - "%26lt;%26lt;endl;


cin %26gt;%26gt; operation;


break;


}


case '2'


...
Reply:I think there are a couple of reasons that the program will only mutiply, first being that all of the methods are named "operate", so when the program calls "operate", it is always going to use the first one encountered, which happens to be the one that multiplies. I would stongly suggest changing to each method name to a unique name.


Now, about the logic.. this program could be greatly simplified by creating methods for the inputting and outputting of the numbers, as well as an "operation" method that could perform any one of the four operations, depending on what the user chooses. You could include the operations in a swtich statement, like so :





switch (operation)


1


multiply the nums


break


2


divide the nums


break


...





Code may be a little of (I'm not a C++ developer) but the concept is good. Try that out. Good luck :)
Reply:include the int main() function, write your user define functions after the int main, and give them defferent names. your program shall work if you like i could give you a full code.


No comments:

Post a Comment