Posts: 753
Threads: 70
Joined: Jul 2010
Reputation:
14
10-22-2010, 10:05 PM
(This post was last modified: 10-24-2010, 07:22 PM by Reality.)
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 ^^
Posts: 753
Threads: 70
Joined: Jul 2010
Reputation:
14
Updated Link,
Does anyone know how to make it so I can multiply by decimals?
Posts: 118
Threads: 3
Joined: Oct 2010
Reputation:
4
10-24-2010, 08:21 PM
(This post was last modified: 10-27-2010, 07:26 PM by Disease.)
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!
Posts: 753
Threads: 70
Joined: Jul 2010
Reputation:
14
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.
Posts: 118
Threads: 3
Joined: Oct 2010
Reputation:
4
10-25-2010, 04:13 AM
(This post was last modified: 10-25-2010, 05:30 AM by Disease.)
(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!
Posts: 753
Threads: 70
Joined: Jul 2010
Reputation:
14
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
Posts: 118
Threads: 3
Joined: Oct 2010
Reputation:
4
(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!
Posts: 753
Threads: 70
Joined: Jul 2010
Reputation:
14
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
Posts: 118
Threads: 3
Joined: Oct 2010
Reputation:
4
(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!
Posts: 753
Threads: 70
Joined: Jul 2010
Reputation:
14
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 <.>
|