#Test for a project of mine

23 messages · Page 1 of 1 (latest)

elder marsh
#

Nothing special here, just testing some code of mine

#
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    
    const double PERCENT = 100.0;

    double priceOld = 0.0;
    double priceNew = 0.0;
    double diff = 0.0;
    double pctChange = 0.0;
    string trend = "";

    
    cout << "Enter the price of the game in the first year: ";
    cin >> priceOld;
    
    cout << "Enter the price of the game in the second year: ";
    cin >> priceNew;
    
    diff = priceNew - priceOld;

    
    if (priceOld != 0)
        pctChange = (diff / priceOld) * PERCENT;
    else
        pctChange = 0; 
    
    if (diff > 0)
    {
        trend = "increased";
    }
    else if (diff < 0)
    {
        trend = "decreased";
    }
    else
    {
        trend = "stayed the same";
    }
    
    cout << endl;
    cout << "-----------------------------------------------" << endl;
    cout << "     Video Game Price Change Analyzer" << endl;
    cout << "-----------------------------------------------" << endl;
    cout << fixed << setprecision(2);

    if (trend == "stayed the same")
    {
        cout << "The price stayed the same." << endl;
    }
    else
    {
        cout << "The price " << trend << " by $" << abs(diff) << endl;
        cout << "That is a " << pctChange << "% " << trend << " since the first year." << endl;
    }

    cout << "-----------------------------------------------" << endl;

    return 0;
}
dense treeBOT
#
Critical error:

You must provide a valid language or compiler!

;compile c++
```
int main() {}
```

elder marsh
#

;compile c++

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
    
    const double PERCENT = 100.0;

    double priceOld = 0.0;
    double priceNew = 0.0;
    double diff = 0.0;
    double pctChange = 0.0;
    string trend = "";

    
    cout << "Enter the price of the game in the first year: ";
    cin >> priceOld;
    
    cout << "Enter the price of the game in the second year: ";
    cin >> priceNew;
    
    diff = priceNew - priceOld;

    
    if (priceOld != 0)
        pctChange = (diff / priceOld) * PERCENT;
    else
        pctChange = 0; 
    
    if (diff > 0)
    {
        trend = "increased";
    }
    else if (diff < 0)
    {
        trend = "decreased";
    }
    else
    {
        trend = "stayed the same";
    }
    
    cout << endl;
    cout << "-----------------------------------------------" << endl;
    cout << "     Video Game Price Change Analyzer" << endl;
    cout << "-----------------------------------------------" << endl;
    cout << fixed << setprecision(2);

    if (trend == "stayed the same")
    {
        cout << "The price stayed the same." << endl;
    }
    else
    {
        cout << "The price " << trend << " by $" << abs(diff) << endl;
        cout << "That is a " << pctChange << "% " << trend << " since the first year." << endl;
    }

    cout << "-----------------------------------------------" << endl;

    return 0;
}
dense treeBOT
#
Program Output
Enter the price of the game in the first year: Enter the price of the game in the second year: 
-----------------------------------------------
     Video Game Price Change Analyzer
-----------------------------------------------
The price stayed the
elder marsh
#

;compile c++

#include <iostream>
using namespace std;
int main()
{
    int num1 = 0,
    num2 = 0,
    sum = 0,
    min = 0,
    max = 0;
    double avg = 0.0;

    cout << "Please enter an integer: ";
    cin >> num1;

    cout << "Please enter another integer: ";
    cin >> num2;

    sum = num1 + num2;

    avg = (num1 + num2) / 2.0;

    min = num1 < num2 ? num1 : num2;

    max = num1 < num2 ? num2 : num1;

    cout << endl << "The sum of the two numbers is: " << sum << endl;
    cout << "The average of the two numbers is: " << avg << endl;
    cout << "The minimum of the two numbers is: " << min << endl;
    cout << "The maximum of the two numbers is: " << max << endl;
    
    return 0;
}
dense treeBOT
#
Program Output
Please enter an integer: Please enter another integer: 
The sum of the two numbers is: 0
The average of the two numbers is: 0
The minimum of the two numbers is: 0
The maximum of the two numbers is: 0
elder marsh
#

;compile c++

#include <iostream>
using namespace std;
int main()
{
    int num1 = 0,
    num2 = 0,
    sum = 0,
    min = 0,
    max = 0;
    double avg = 0.0;

    cout << "Please enter the price of one video game: ";
    cin >> num1;

    cout << "Please enter the price of another video game: ";
    cin >> num2;

    sum = num1 + num2;

    avg = (num1 + num2) / 2.0;

    min = num1 < num2 ? num1 : num2;

    max = num1 < num2 ? num2 : num1;

    cout << endl << "The sum of the two video games and their prices is: " << sum << endl;
    cout << "The average of the prices is: " << avg << endl;
    cout << "The minimum of the prices is: " << min << endl;
    cout << "The maximum of the prices is: " << max << endl;
    
    return 0;
}
dense treeBOT
#
Program Output
Please enter the price of one video game: Please enter the price of another video game: 
The sum of the two video games and their prices is: 0
The average of the prices is: 0
The minimum of the prices is: 0
The maximum of the prices is: 0
lavish sierraBOT
#
Why Is `using namespace std` Considered Bad Practice?

using namespace std will import all the symbols from std into the enclosing namespace. This can easily lead to name collisions, as the standard library is filled with common names: get, count, map, array, etc.

A key concern with using namespace std; is not what is imported now but rather what may suddenly be imported in the future.

While using namespace std; is alright for tiny projects, it is important to move away from it as soon as possible. Consider less intrusive alternatives:

// OK: *only* import std::vector
using std::vector;
// OK: namespace alias
namespace chr = std::chrono;
chr::duration x;
grand obsidian
#

maybe also try making functions for the not so trivial operations?

#

for example:

int min(int x, int y) {}
int max(int x, int y) {}
elder marsh
grand obsidian
elder marsh
sinful slate
elder marsh
grand obsidian
#

related constants usually

#

so say I have some set of constant Roles like admin, writer, editor etc.

#

I could do this:

namespace roles {
  const std::string admin = "admin";
  const std::string writer = "writer";
  const std::string editor = "editor";
}```

but 
```cpp
enum class Roles {
  Admin, Writer, Editor
};

is actually better because it describes a set of related constants