Sign in to follow this  

caculator project help please

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

heres my calculators source #include <iostream> using namespace std; int main() { int length; int width; cout << "please enter length: "; cin >> length; cout << "please enter the width: "; cout << "the area is "; cout << length * width/2; return 0; this is for a traingle i know but could i change this code to allow input of the function eg. divide (/) ps. how do i get this code into those white boxes? thanks [Edited by - gamerking on August 31, 2005 5:46:38 AM]

Share this post


Link to post
Share on other sites
For a triangle it should be:

cout << ( (width/2) * length );

If you wanted to yes you could write an equation parser. This would be alot of hard work though and i'm pretty sure there is a library out there somewhere that will do this for you, google.

ace

Share this post


Link to post
Share on other sites
The input can be a string and then parse it (i think i saw a math library somewhere in sourceforge if you want).
As for the white boxes, you can color the background of the input text to white.
(i think it can be implemented with SetConsoleTextAttribute)

Share this post


Link to post
Share on other sites
If you only want to be able to specify what operator to use, like / or *, just read one character and check what it is. Here's an example:

char c;

cin >> c;

switch(c)
{
case '\':
cout << "Dividing" << endl;
break;
case '*':
cout << "Multiplying" << endl;
break;
case 'this is supposed to be a plus-sign, but it doesn't work.':
cout << "Adding" << endl;
break;
case '-':
cout << "Subtracting" << endl;
break;
}



This will enable you to let the user input for example 2 numbers and an operator. But if you wan't to make your calculator more advanced you'll propably have to use an equation parser as ace said. But I guess that you only want to make a simple calculator, so that won't be needed.

And just to avoid some confusion, ignore what pex22 said about white boxes. It seems like he misunderstood you. What you want is [source]code goes here[/source]. You can find it in the faq. What pex22 talked about is coloring your programs background.

Anyway, I hope this'll help you.

Share this post


Link to post
Share on other sites
Get hold of "The C++ Programming Language", by Stroustrup, go to Chap 6, Sec 6.1 "A Desk Calculator".

Step by step howtos on (1) the parser, (2) the input function, (3) error handling (4) the driver and (5) command-line arguments (optional). You might be able to get info on the 'net by searching 'Stroustrup desk calculator'.

Good Luck!

--random_thinker

Share this post


Link to post
Share on other sites

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this