Computer Science 1 - Project 1

Hey y’all. Im not really too active here anymore but I’m starting university for computer science and i figured id post my progress with programming and all that :slight_smile: I will be posting these once every week or so.

Anywhoo, this program calculates the distance an object falls given the time in seconds by the user.



// CS 1511
// Project 1

#include <iostream>
using namespace std;

int main()
{
  // Variable Declarations
  int time, distance;

  // Inputs
  cout << "This program will calculate the distance of an object in freefall. Please enter the time of the drop in seconds:";
  cin >> time;

  // Calculations
  distance = time * time * 32 / 2;

  // Output & End of program
  cout << distance << " units
";
  cout << "The program is now over. Thank you.
";

	return 0;
}



don’t use

using namespace std;

instead make habit of using

std::cout
and so on for everything in standard library.

edit: forgot the say, don’t use namspace std; globally.

Thanks i eat food. However, my compiler gave me error messages… I believe its because I’m logged into a Unix server run by my uni that is not running any of the latest c++ updates

that should affect this unless they’re running some deprecated stuff

Instead of just telling him what not to use, why not explain why he shouldn’t use it?

@OP:

This should be how your code should look with his suggestion.


#include <iostream>

using std::cout;
using std::cin;

int main()
{
  // Variable Declarations
  int time, distance;

  // Inputs
  cout << "This program will calculate the distance of an object in freefall. Please enter the time of the drop in seconds:";
  cin >> time;

  // Calculations
  distance = time * time * 32 / 2;

  // Output & End of program
  cout << distance << " units
";
  cout << "The program is now over. Thank you.
";

	return 0;
}

If you’ve already discussed scope, then you should understand that you can also call these locally which would be a better alternative to get used to before you start larger scaled programs in order to avoid polluting your project’s namespace.


#include <iostream>

int main()
{
  using std::cout;
  using std::cin;

  // Variable Declarations
  int time, distance;

  // Inputs
  cout << "This program will calculate the distance of an object in freefall. Please enter the time of the drop in seconds:";
  cin >> time;

  // Calculations
  distance = time * time * 32 / 2;

  // Output & End of program
  cout << distance << " units
";
  cout << "The program is now over. Thank you.
";

	return 0;
}

Good luck with your class! :smiley:

A lot of classes require the use of namespace std regardless if it is the best way to do it or not.