Quote (AbDuCt @ 14 Feb 2014 22:31)
If you use your operators correctly you can eliminate all your redundant error checking.
Code
int main() {
cout << "Input a type and an amount (K, M, F, C): ";
cin >> unit >> amount;
if (unit == 'K' && amount >= 0) {
cout << "Distance " << amount << " kilometers is "
<< amount * MILES_PER_KM << " miles.";
}
else if (unit == 'M' && amount >= 0) {
cout << "Distance " << amount << " miles is "
<< amount * KM_PER_MILE << " kilometers.";
}
else if (unit == 'F' && amount <= MAX_F_TEMP && amount >= MIN_F_TEMP) {
cout << "Temperature " << amount << " F is "
<< ( amount - FREEZING_F ) * CELSIUS_PER_FAHR << " C.";
}
else if ( unit == 'C' ) {
cout << "Temperature " << amount << " C is "
<< amount * FAHR_PER_CELSIUS + FREEZING_F << " F.";
}
else {
cout << "UNABLE TO PROCESS: " << unit
<< " is not a recognized measurement." << endl;
cout << "Please enter either K, M, F, or C and a value above 0" << endl;
}
return 0;
}
It limits your specific error messages for each if branch but it reduces the amount of code. Only do this if your project does not state you need proper error messages.
You can also remove your <string> include as you are not using any string manipulation functions.
Also double check it compiles and works properly after any changes.
edit:: Also your Celsius to Fahrenheit if branch does not have any error checking. Not sure if this was by design.
edit2:: I am also not a fan of the expanded style of C I much rather condense everything as shown in the edited code. Although again it is up to how you are being taught style wise. Be sure you wont lose marks for using your own coding style.
OMG. Hahahaha

I was just thinking to myself as I read his code. "Hmm. No comments, no && operators, no knowledge of float, int, double, long, etc. Oh man..."
Quote (bensfriend1 @ 14 Feb 2014 22:39)
#include <iostream>
#include <iomanip>
#include <string>
@abduct what do the first two mean besides just the string? can i remove those as well?
And the C to F code doesnt need error checking as far as i can tell on the project outline
I believe I need the redundant error and more code because the project really wants the proper error messages.
Oh man. You should be in a basic course bud. Not sure who taught you but they didn't give you any fundamentals it seems.
Read this:
STARTING OUT WITH C++ From Control Structures through Objects SEVENTH EDITIONhttp://www.amazon.com/s/?ie=UTF8&keywords=starting+out&tag=googhydr-20&index=stripbooks&hvadid=19931788157&hvpos=1t2&hvexid=&hvnetw=s&hvrand=13867330391977755615&hvpone=&hvptwo=&hvqmt=b&hvdev=c&ref=pd%5Fsl%5F92zzw19zud%5FbIt goes through all the basics very well. To answer your original questiom: int is short for integer, float for floating precision point, double for double floating precision point, char is character. IIRC it is 8 bits or 1 byte of data. I think it is the smallest unit in C++ but I could be wrong.**
Code
Integer literal 20
String literal "Today we sold "
String literal "bushels of apples.\n"
Integer literal 0
Also:
Code
[B]Data Type Size Range[/B]
short 2 bytes -32,768 to +32,767
unsigned short 2 bytes 0 to +65,535
int 4 bytes -2,147,483,648 to +2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
long 4 bytes -2,147,483,648 to +2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295
- Integers are at least as big as short integers.
- Long integers are at least as big as integers.
- Unsigned short integers are the same size as short integers.
- Unsigned integers are the same size as integers.
- Unsigned long integers are the same size as long integers.
In stead of const you can use #define for keep a variable from being modified.
Code
#define PI 3.14159
#define DIAMETER 10.0
The #include <iostream> is a preprocessor directive. It goes before the compiler and inputs all the data from the directive. It's much like calling a library in Java. It instead of writing the crap load of code it takes to do a cout//cin, etc. (By the way cout stands for console back when people typed on terminals to access consoles.) Same with <stings>, etc.; they are called header files. Hence they go at the head of the file.
iostream, string, etc. is also the name of the file being called forth into the source code.
Well I am tired and that is enough of that today.
Edit:: Left a sentence half written.**
This post was edited by NinjaSushi2 on Feb 14 2014 11:08pm