1. Compiled the code using cygwin, hence added using namespace std;a using clause generally can be omitted when using the namespace prefix. also you could use std:: regardless whether a 'using namespace std;' was given or not.
2. Always initialize a variable while defining that.
Replace:
std::string in;
With:
std::string in = "";
3. Replace:the first output statements at the console always should begin with at least one line-feed because otherwise the output could begin anywhere in the middle of the console.
std::cout << "\nEnter the triangle base: ";
With:
std::cout << "Enter the triangle base: ";
Like the same written all expected cout statements.
4. Better to use:no, the (b == 0) is much better readable than (0 == b). you will get a warning from the compiler if you write
if (0 == b)
instead of
if (b == 0)
since by mistake programmer might write
if (b = 0)
for handling assignment inside conditional checking.
which will assign zero to b.
if (b = 0)
.5. Replace:std::getline(in) is a typing error. the correct Statement is
std::getline(in);
With:
#ifdef CYGWIN_NT
getline(cin, in);
#else
std::getline(in);
#endif
due to cygwin compilation.
std::getline(cin, in);
7. Replace:the first is better readable. see above.
if (in == "quit")
With:
if ( "quit" == in)
for handling assignment inside conditional checking.
9. Replace:why?
std::cout << "\nThe area is " << area << "units" << std::endl;
With:
std::cout << "Area : " << Area << " units\n";
to use Area variable.
10. Replace:
int Area = (b*h)/2;
With:
double Area = ( b * h) / 2.0;
that could be good if b and h are integers with an odd number. if the original code (Python) can handle integers and doubles without explicit types, i would suggest to use double for h, b, and Area.1. >> there is no requirement for cygwinyou didn't try it for "all platforms" but only for one, while the code i posted was pure c++ and actually would compile at each platform with a compiler since c++ standard (1998). you added preprocessor statements
Written the code for optimization at all platforms.
#ifdef CYGWIN_NT
#include <iostream>
using namespace std;
#elif NEXT_PLATFORM
#include <iostream.h>
#else
#include <iostream>
#endif
std::getline(cin, in);
2. >> std::string in = "";you are wrong. while this statement is true for all plain c types, it generally doesn't make sense if you want to create an empty class or struct object which has a valid default constructor. defining code which is redundant or not needed is not good programming but blows your code up and makes it badly readable. note, the compiler would generate a default constructor, a copy constructor and a default assignment operator for your class. if your class only has class members and no plain c types as members, you don't need to write code for all these. that makes your code lean and easy to maintain.
A good programmer need to initialize the variable where ever it is defined.
3. >> no, the (b == 0) is much better readable than (0 == b). you will get a warning from the compiler if you writeas told, you may have your code guide lines and i have mine. but you must not spoil another thread with those issues as they are off-topic. if you want to discuss these, you can ask your own question, and i am sure there are many experienced c++ programmers with an expertise of multiple decades which would participate.
Usually here goes related to development at all development projects
1. Initially written by old team members
Current team members need to handle old errors
Such kind of errors used to happen.
5. >> double Area = ( b * h) / 2.0;
handled the output to display when odd numbers are given input.
Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the ``narrower'' type is widened to that of the other, where plain integer is narrower than long integer is narrower than floating point is narrower than complex. Comparisons between numbers of mixed type use the same rule.2.6 The constructors int(), long(), float(), and complex() can be used to produce numbers of a specific type.that means that you are right by making Area a double. so point 5 would have been valuable if posted as an addition to my comment.
Open in new window