Jump to content

  • Log In with Google      Sign In   
  • Create Account


HaVoK87

Member Since 14 Dec 2011
Offline Last Active Sep 12 2013 08:57 PM

Posts I've Made

In Topic: Basic C++ Help Maybe?

12 September 2013 - 11:35 AM

So after taking some advice I cleaned up the code a bit. Tell me what you think. Also is there like a IRC or Mumble server which has a bunch of game programmers that we can ask advice or hangout with? That would be amazing if someone could share me some info on that.

// Game Stats
// Demonstrates declaring and initializing variables

#include <iostream>
using namespace std;

int main()
{
	// Establishing variables and values
	int score = 72;
	double distance = 1200.76;
	char playAgain = 'y';
	bool shieldsUp = true;
	short lives = 3;
	short aliensKilled = 10;
	double engineTemp = 6572.89;
	int fuel;

	// Printing information to screen
	cout << "\nScore: " << score << endl;
	cout << "Distance: " << distance << endl;
	cout << "Play Again?: " << playAgain << endl;
	cout << "Shields Up?: " << shieldsUp << endl;
	cout << "Lives: " << lives << endl;
	cout << "Aliens Killed: "<< aliensKilled << endl;
	cout << "Engine Temp: " << engineTemp << endl;
	
	// Printing information and receiving information
	cout << "\nHow much fuel? ";
	cin >> fuel;
	cout << "Fuel: " << fuel << endl;

	// Defining different types into user type
	typedef unsigned short int ushort;
	ushort bonus = 10;
	cout << "\nBonus: " << bonus << endl;

	// Ending program
	return 0;
}

In Topic: Basic C++ Help Maybe?

11 September 2013 - 04:58 PM

Just some tips:

 

Please teach your self to move "using namespace <namespace_name>;" into a function call or wrapped in another namespace, preferably the first option. This is because you have just polluted the global namespace with all the names of std, which is bad and can in cases lead to compiler errors that look really strange.

 

Slightly better way to write the initial declaration and initialisation of the variables is like this

    int score = 0;
    double distance = 1200.76;
    char playAgain = 'y';
    bool shieldsUp = true;

    short lives = 3; //Don't define multiple variables on the same line this can create issues for you when declaring references and pointers on one line
    short aliensKilled = 10;

The reason for this is that you will not forget to initialise a variable, C++ doesn't automatically do this for you, so untill you actually assign a value to a variable it has an undefined value(usually whatever the memory is at the time the variable is declared and given a memory area to use).

 

Thank you for the information. I'm actually reading the book Beginning C++ Through Game Programming Third Edition and I think it's going to show me just that. It takes you through the basics first and then shows you how to write the same program in a different format which I like so I'm not confused when I see it one way or the other like yours. You guys are awesome I didn't know this community was so active and helpful!


In Topic: Basic C++ Help Maybe?

11 September 2013 - 04:31 PM

 

Double quotes are used to define strings, single quotes are used to define a character. You want:

playAgain = 'y';

 

Sir as basic as that is you are awesome! Compiled perfectly fine thank you very much!


PARTNERS