Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Using namespace std

This is a common issue. Don't do it. Don't do it.

main signature

C++ specifies that main() must be declared returning an int. void main() is not valid C++.

Variable declarations

In compare_budgets() you declare all your variables at the beginning of the function. It is better to declare them as late as possible. Also, the values which are initialized and then only read should be marked const. This will guarantee that the variable cannot be accidentally modified, and makes your intent clearer. Thus, variables declared and set like this:

double result_housing;
// ...
result_housing = monthly.set_housing - amount.asked_housing;

Can be written on one line:

const double result_housing = monthly.set_housing - amount.asked_housing;

Limit output locations

In general it's a good idea to limit the number of places you write to an output stream. budget_alert() contains a lot of repetition, and directly outputs to cout. This makes it difficult to use a different stream, if you ever want to change the output location. I would rewrite the function to return a string indicating your alert message:

std::string budget_alert(const double alert)
{
 std::string budget_position;
 if (alert == 0) {
 budget_position = "RIGHT AT";
 } else if (alert > 0) {
 budget_position = "UNDER";
 } else {
 budget_position = "OVER";
 }
 return budget_position + " BUDGET";
}

Then you can adjust the call site to output the correct whitespace and parentheses. Also note that I use else if for mutually exclusive conditions. Again, this makes your intention clearer.

Meaningless comments

Some of your comments contain redundant information which already exists in the code.

void amounts (Asked_Budget&); // Input passed by reference

The comment here is completely unnecessary and just clutters your code. The function signature already indicates the parameter is taken by reference -- there's no need to say so in a comment.

Using namespace std

This is a common issue. Don't do it.

main signature

C++ specifies that main() must be declared returning an int. void main() is not valid C++.

Variable declarations

In compare_budgets() you declare all your variables at the beginning of the function. It is better to declare them as late as possible. Also, the values which are initialized and then only read should be marked const. This will guarantee that the variable cannot be accidentally modified, and makes your intent clearer. Thus, variables declared and set like this:

double result_housing;
// ...
result_housing = monthly.set_housing - amount.asked_housing;

Can be written on one line:

const double result_housing = monthly.set_housing - amount.asked_housing;

Limit output locations

In general it's a good idea to limit the number of places you write to an output stream. budget_alert() contains a lot of repetition, and directly outputs to cout. This makes it difficult to use a different stream, if you ever want to change the output location. I would rewrite the function to return a string indicating your alert message:

std::string budget_alert(const double alert)
{
 std::string budget_position;
 if (alert == 0) {
 budget_position = "RIGHT AT";
 } else if (alert > 0) {
 budget_position = "UNDER";
 } else {
 budget_position = "OVER";
 }
 return budget_position + " BUDGET";
}

Then you can adjust the call site to output the correct whitespace and parentheses. Also note that I use else if for mutually exclusive conditions. Again, this makes your intention clearer.

Meaningless comments

Some of your comments contain redundant information which already exists in the code.

void amounts (Asked_Budget&); // Input passed by reference

The comment here is completely unnecessary and just clutters your code. The function signature already indicates the parameter is taken by reference -- there's no need to say so in a comment.

Using namespace std

This is a common issue. Don't do it.

main signature

C++ specifies that main() must be declared returning an int. void main() is not valid C++.

Variable declarations

In compare_budgets() you declare all your variables at the beginning of the function. It is better to declare them as late as possible. Also, the values which are initialized and then only read should be marked const. This will guarantee that the variable cannot be accidentally modified, and makes your intent clearer. Thus, variables declared and set like this:

double result_housing;
// ...
result_housing = monthly.set_housing - amount.asked_housing;

Can be written on one line:

const double result_housing = monthly.set_housing - amount.asked_housing;

Limit output locations

In general it's a good idea to limit the number of places you write to an output stream. budget_alert() contains a lot of repetition, and directly outputs to cout. This makes it difficult to use a different stream, if you ever want to change the output location. I would rewrite the function to return a string indicating your alert message:

std::string budget_alert(const double alert)
{
 std::string budget_position;
 if (alert == 0) {
 budget_position = "RIGHT AT";
 } else if (alert > 0) {
 budget_position = "UNDER";
 } else {
 budget_position = "OVER";
 }
 return budget_position + " BUDGET";
}

Then you can adjust the call site to output the correct whitespace and parentheses. Also note that I use else if for mutually exclusive conditions. Again, this makes your intention clearer.

Meaningless comments

Some of your comments contain redundant information which already exists in the code.

void amounts (Asked_Budget&); // Input passed by reference

The comment here is completely unnecessary and just clutters your code. The function signature already indicates the parameter is taken by reference -- there's no need to say so in a comment.

Adding explanation for else usage.
Source Link
Aurelius
  • 1.4k
  • 8
  • 21

Using namespace std

This is a common issue. Don't do it.

main signature

C++ specifies that main() must be declared returning an int. void main() is not valid C++.

Variable declarations

In compare_budgets() you declare all your variables at the beginning of the function. It is better to declare them as late as possible. Also, the values which are initialized and then only read should be marked const. This will guarantee that the variable cannot be accidentally modified, and makes your intent clearer. Thus, variables declared and set like this:

double result_housing;
// ...
result_housing = monthly.set_housing - amount.asked_housing;

Can be written on one line:

const double result_housing = monthly.set_housing - amount.asked_housing;

Limit output locations

In general it's a good idea to limit the number of places you write to an output stream. budget_alert() contains a lot of repetition, and directly outputs to cout. This makes it difficult to use a different stream, if you ever want to change the output location. I would rewrite the function to return a string indicating your alert message:

std::string budget_alert(const double alert)
{
 std::string budget_position;
 if (alert == 0) {
 budget_position = "RIGHT AT";
 } else if (alert > 0) {
 budget_position = "UNDER";
 } else {
 budget_position = "OVER";
 }
 return budget_position + " BUDGET";
}

Then you can adjust the call site to output the correct whitespace and parentheses. Also note that I use else if for mutually exclusive conditions. Again, this makes your intention clearer.

Meaningless comments

Some of your comments contain redundant information which already exists in the code.

void amounts (Asked_Budget&); // Input passed by reference

The comment here is completely unnecessary and just clutters your code. The function signature already indicates the parameter is taken by reference -- there's no need to say so in a comment.

Using namespace std

This is a common issue. Don't do it.

main signature

C++ specifies that main() must be declared returning an int. void main() is not valid C++.

Variable declarations

In compare_budgets() you declare all your variables at the beginning of the function. It is better to declare them as late as possible. Also, the values which are initialized and then only read should be marked const. This will guarantee that the variable cannot be accidentally modified, and makes your intent clearer. Thus, variables declared and set like this:

double result_housing;
// ...
result_housing = monthly.set_housing - amount.asked_housing;

Can be written on one line:

const double result_housing = monthly.set_housing - amount.asked_housing;

Limit output locations

In general it's a good idea to limit the number of places you write to an output stream. budget_alert() contains a lot of repetition, and directly outputs to cout. This makes it difficult to use a different stream, if you ever want to change the output location. I would rewrite the function to return a string indicating your alert message:

std::string budget_alert(const double alert)
{
 std::string budget_position;
 if (alert == 0) {
 budget_position = "RIGHT AT";
 } else if (alert > 0) {
 budget_position = "UNDER";
 } else {
 budget_position = "OVER";
 }
 return budget_position + " BUDGET";
}

Then you can adjust the call site to output the correct whitespace and parentheses.

Meaningless comments

Some of your comments contain redundant information which already exists in the code.

void amounts (Asked_Budget&); // Input passed by reference

The comment here is completely unnecessary and just clutters your code. The function signature already indicates the parameter is taken by reference -- there's no need to say so in a comment.

Using namespace std

This is a common issue. Don't do it.

main signature

C++ specifies that main() must be declared returning an int. void main() is not valid C++.

Variable declarations

In compare_budgets() you declare all your variables at the beginning of the function. It is better to declare them as late as possible. Also, the values which are initialized and then only read should be marked const. This will guarantee that the variable cannot be accidentally modified, and makes your intent clearer. Thus, variables declared and set like this:

double result_housing;
// ...
result_housing = monthly.set_housing - amount.asked_housing;

Can be written on one line:

const double result_housing = monthly.set_housing - amount.asked_housing;

Limit output locations

In general it's a good idea to limit the number of places you write to an output stream. budget_alert() contains a lot of repetition, and directly outputs to cout. This makes it difficult to use a different stream, if you ever want to change the output location. I would rewrite the function to return a string indicating your alert message:

std::string budget_alert(const double alert)
{
 std::string budget_position;
 if (alert == 0) {
 budget_position = "RIGHT AT";
 } else if (alert > 0) {
 budget_position = "UNDER";
 } else {
 budget_position = "OVER";
 }
 return budget_position + " BUDGET";
}

Then you can adjust the call site to output the correct whitespace and parentheses. Also note that I use else if for mutually exclusive conditions. Again, this makes your intention clearer.

Meaningless comments

Some of your comments contain redundant information which already exists in the code.

void amounts (Asked_Budget&); // Input passed by reference

The comment here is completely unnecessary and just clutters your code. The function signature already indicates the parameter is taken by reference -- there's no need to say so in a comment.

Source Link
Aurelius
  • 1.4k
  • 8
  • 21

Using namespace std

This is a common issue. Don't do it.

main signature

C++ specifies that main() must be declared returning an int. void main() is not valid C++.

Variable declarations

In compare_budgets() you declare all your variables at the beginning of the function. It is better to declare them as late as possible. Also, the values which are initialized and then only read should be marked const. This will guarantee that the variable cannot be accidentally modified, and makes your intent clearer. Thus, variables declared and set like this:

double result_housing;
// ...
result_housing = monthly.set_housing - amount.asked_housing;

Can be written on one line:

const double result_housing = monthly.set_housing - amount.asked_housing;

Limit output locations

In general it's a good idea to limit the number of places you write to an output stream. budget_alert() contains a lot of repetition, and directly outputs to cout. This makes it difficult to use a different stream, if you ever want to change the output location. I would rewrite the function to return a string indicating your alert message:

std::string budget_alert(const double alert)
{
 std::string budget_position;
 if (alert == 0) {
 budget_position = "RIGHT AT";
 } else if (alert > 0) {
 budget_position = "UNDER";
 } else {
 budget_position = "OVER";
 }
 return budget_position + " BUDGET";
}

Then you can adjust the call site to output the correct whitespace and parentheses.

Meaningless comments

Some of your comments contain redundant information which already exists in the code.

void amounts (Asked_Budget&); // Input passed by reference

The comment here is completely unnecessary and just clutters your code. The function signature already indicates the parameter is taken by reference -- there's no need to say so in a comment.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /