Check this out!

This is an example that I found in a C++ book I found laying around:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
    float original = 0.0;
    float rate = 0.0;

    float discount = 0.0;

    float sale = 0.0;

    cout << "Original price: ";
    cin >> original;
    cout << "Discount rate: ";
    cin >> rate;

    discount = original * discRate;

    sale = discount - original;

    cout << "Sales price: " << sale << endl;
}


I know right?

Here is what I think of it:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main() {
    float original, rate;
    cout << "Original price: "; cin >> original;
    cout << "Discount rate: ";  cin >> rate;
    cout << "Sales price: " << (original * rate) - original << "\n";
    return 0;
}


And I'm a newbie still. Well, maybe average, but still...

Isn't that just painful to look at?
Last edited on
Um... No...

This, however, is:
cout << "Original price: "; cin >> original;
Here's how I think it should be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
  double originalPrice,discountRate;
  cout << "Original price: ";
  cin >> originalPrice;
  cout << "Discount rate: ";
  cin >> discountRate;

  double discount = originalPrice * discountRate;
  double salePrice = original-discount;

  cout << "Sales price: " << salePrice << endl;
}


Code should be self-documenting whenever possible, i.e. it should be obvious what it does without needing additional commentary.
Topic archived. No new replies allowed.