Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi Purpose Calculator
#1
This is my first C++ Project Ever, and Calculators have been pretty popular lately.

The volume code was based on something I found on a MSU site (Michigan SU), and then everything else was my custom code, except the formula for C > F and F > C.

Since I use Linux I can only provide the source, below:

http://pastebin.com/Jz5gnLgm
AvA is my HF Account name ^^
Reply
#2
Updated Link,

Does anyone know how to make it so I can multiply by decimals?
Reply
#3
You cannot multiply by decimals right now because you have declared a, b and c as integers. An integer is a whole number. If you notice, you only ever use up to four variables through your entire code. There's little reason to bother declaring separate variables for cases. Just declare four floats within the while loop (so they'll be flushed with each iteration) and use them as needed. If you know you're never going to add or subtract decimals then you could declare integers for those cases specifically. But realistically that's not something a calculator can assume.

I noticed you have a lot of redundancy in your coding. Specifically recalling the cout object numerous times for one output session. That's unnecessary. You also aren't returning a value at the end of the main function; you should be returning 0 for success.

http://diseasesf.pastebin.com/HNMastWg
Ho, ho, ho! Well, if it isn't fat stinking billy goat Billy Boy in poison!
How art thou, thou globby bottle of cheap, stinking chip oil?
Come and get one in the yarbles, if ya have any yarbles, you eunuch jelly thou!
Reply
#4
Thanks Disease, never knew that.

I had taken return 0; out because of the while (choice < 100) { loop, but I guess your placement lets it work.

Also, is there a way to have like double and float? Is that a class or something? Dunno them yet.
Reply
#5
(10-24-2010, 09:14 PM)Reality Wrote: Thanks Disease, never knew that.

I had taken return 0; out because of the while (choice < 100) { loop, but I guess your placement lets it work.

Also, is there a way to have like double and float? Is that a class or something? Dunno them yet.

Why would you need both? If you need the precision of a double then just use that over float. The performance difference between floats and doubles is virtually nonexistent these days. Modern CPUs all have floating point units (FPUs) built in. Most FPUs don't even distinguish between float and double.

Barring performance the only difference between the two is memory usage. A float uses up 4 bytes while a double uses 8. Given that on the low end PCs have 1-2GBs of RAM, the memory usage isn't a problem for modern programmers. Especially considering your application only has a maximum of four.

Also, functions must always return the data type they are declared as. The one excepting being a void function, because that's what void represents: nothing. The main function is declared as an integer, thus it must always return an integer. You having a loop inside the function doesn't change that.
Ho, ho, ho! Well, if it isn't fat stinking billy goat Billy Boy in poison!
How art thou, thou globby bottle of cheap, stinking chip oil?
Come and get one in the yarbles, if ya have any yarbles, you eunuch jelly thou!
Reply
#6
Is it possible in c++ to allow the person to type the sign they choose>?

Like in pythonI think you can do

print "Choose blah blah"
if input = ""
etc?

sorry for typing my lapop broke and im on a netbook
Reply
#7
(10-25-2010, 12:37 PM)Reality Wrote: Is it possible in c++ to allow the person to type the sign they choose>?

Like in pythonI think you can do

print "Choose blah blah"
if input = ""
etc?

sorry for typing my lapop broke and im on a netbook

Yes, absolutely. The easiest thing would be to store the input as a char (a 1 byte data type that can store a single character such as 'a'). You could also bring in the C++ string library and store the input as a string. The string library overloads the equal to operator (==) to allow for comparing two strings.

Char
Code:
#include <iostream>

int main ()
{
  std::cout << "Enter a character: ";
  char input;
  std::cin >> input;
  
  if (input == 'a')
  {
    std::cout << "You entered the letter A." << std::endl;
  }
  
  return 0;
}

String
Code:
#include <iostream>
#include <string>

int main ()
{
  std::cout << "Enter a character: ";
  std::string input;
  std::cin >> input;
  
  if (input == "something")
  {
    std::cout << "You said something." << std::endl;
  }
  
  return 0;
}
Ho, ho, ho! Well, if it isn't fat stinking billy goat Billy Boy in poison!
How art thou, thou globby bottle of cheap, stinking chip oil?
Come and get one in the yarbles, if ya have any yarbles, you eunuch jelly thou!
Reply
#8
I see.

I don't know about strings yet, so I'll look into them.

Is it possible for the person to just type in the equation they want completed?

ie 2*2/2
Reply
#9
(10-25-2010, 05:04 PM)Reality Wrote: I see.

I don't know about strings yet, so I'll look into them.

Is it possible for the person to just type in the equation they want completed?

ie 2*2/2

Of course it's possible but it's not feasible given what you know about C++. You'd need to parse the input string and look for the necessary information. There are a variety of ways you could do it, but you'll need to understand the standard library more.
Ho, ho, ho! Well, if it isn't fat stinking billy goat Billy Boy in poison!
How art thou, thou globby bottle of cheap, stinking chip oil?
Come and get one in the yarbles, if ya have any yarbles, you eunuch jelly thou!
Reply
#10
Alright just checking that there is a way.

I'm having computer troubles currently and I lost the USB with all my C++ data,Damn.

I'll have to copy the source out <.>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  my own calculator WHITE PANDA 8 1,859 07-14-2010, 03:19 PM
Last Post: Kondry
  Calculator v1 Brainless Control 21 4,441 01-18-2010, 10:34 PM
Last Post: Equinøx
  Calculator in C Liberty 4 1,124 12-10-2009, 11:45 PM
Last Post: Sagittarius

Forum Jump:


Users browsing this thread: 1 Guest(s)