hey people!
I am trying to make a calculator in C++.
I am stuck on how to square a number.
Would anybody know how to do this?
This is a discussion on Squaring numbers in c++ within the C++ Programming forums, part of the General Programming Boards category; hey people! I am trying to make a calculator in C++. I am stuck on how to square a number. ...
hey people!
I am trying to make a calculator in C++.
I am stuck on how to square a number.
Would anybody know how to do this?
You er... multiply the number by itself.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
That depends on how you want to do it, but one way is to use the std::sqrt function from <cmath>. That said, it would be wiser to start by implementing say, addition, and only implement other functionality after you have a working prototype.Originally Posted by Sanka792
Last edited by laserlight; 01-27-2009 at 11:31 AM. Reason: oafter -> after
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Google is your friend
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
You could read say, cppreference.com's entry on sqrt, and/or you could search the Web as iMalc suggested.Originally Posted by Sanka792
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Um, it takes a value (16) and returns the square root of the value (4.0). There's nothing more to it. Do you have an idea how to call any function?
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
This was not compiled and just getting the answer doesn't do you any good.Code:#include <iostream> #include <cmath> int main(){ int n = 16; // number to square root std::cout << std::sqrt(n) << std::endl; }
Well from where i'm sitting, it sounds like a big function to me. I havn't made a calc persae, but i'll probably make one now just for the fun of it. The way I would do it would be trial and error. Think about the behaviour of numbers. An odd number multiplied by an odd number is obviously going to be an odd number. Start from there, create a method of finding out if the input number was even or odd, then take it from there.
Thnks guys so much for the help.
I now know what to do.
I already have a prototype that can add, subtract, multiply and divide.
But i was thinking of adding in formulas like the -b formula.
But i goit another question.
Is it possible to create an interface for c++ so that it looks like a program?
I mean a console that a user can use that covers up command prompt?
As in a graphical user interface that say, resembles a hand held calculator? Yes, but since you appear relatively new to C++... not yet.Originally Posted by Sanka792
At the moment I would suggest a "classical" problem: allow the user to enter an arithmetic expression, from the console window, in reverse Polish notation (search the Web if you must). When you are satisfied with what you have done, allow the user to enter an arithmetic expression in the "normal" infix notation. You could always reuse this later on when you do write a GUI calculator.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Just a note: if you wanted to raise a number to an arbitrary power, you can use the pow() function, also in <cmath>.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.