Basic C++ Help Maybe?

Started by
6 comments, last by molehill mountaineer 10 years, 7 months ago

Okay I have a weird issue going on here that I can't seem to figure out. I must let you all know that I'm running Xubuntu 13.04 using gedit and g++ through terminal. Yeah this is how I wanted to start learning C++ and I was told this would be the best way. Here is my code.


// Game Stats
// Demonstrates declaring and initializing variables

#include <iostream>
using namespace std;

int main()
{
	int score;
	double distance;
	char playAgain;
	bool shieldsUp;

	short lives, aliensKilled;

	score = 0;
	distance = 1200.76;
	playAgain = "y";
	shieldsUp = true;
	lives = 3;
	aliensKilled = 10;

	double engineTemp = 6572.89;

	cout << "\nscore: " << score << endl;
	cout << "distance: " << distance << endl;
	cout << "playAgain: " << playAgain << endl;
	//skipping shieldsUp since you don’t generally print Boolean values
	cout << "lives: " << lives << endl;
	cout << "aliensKilled: "<< aliensKilled << endl;
	cout << "engineTemp: " << engineTemp << endl;

	int fuel;
	cout << "\nHow much fuel? ";
	cin >> fuel;
	cout << "fuel: " << fuel << endl;

	typedef unsigned short int ushort;
	ushort bonus = 10;
	cout << "\nbonus: " << bonus << endl;

	return 0;
}

Now when I try to compile in terminal this is the error I'm getting.


************~/Desktop/dev/src$ g++ gamestats01.cpp -o gamestats01
gamestats01.cpp: In function ‘int main()’:
gamestats01.cpp:18:14: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
************~/Desktop/dev/src$ 

Can someone give me some insight on this? Also I have never programmed a day in my life. Is this okay for me to be using Linux and Terminal for starting? I hate Windows and would prefer not to touch it if I don't have to.

Advertisement

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

playAgain = 'y';

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!

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).

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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!

You also have the option of changing the datatype to string. Include the string header.

//Include the string header
#include <string>

Then change the datatype to string.

string playAgain;

If that was the only issue with the original code, then it should compile fine.

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;
}

you should avoid "using namespace std;" - this tells the compiler "look in the namespace std for all types" and can be a problem when you start using other namespaces.

For example: if you have a namespace "gameStuff" and there's a type called "cout" in there the compiler will be looking in the wrong namespace.

Because 'std' contains a 'cout' type the compiler won't know that something is wrong and the program will compile, then you get to look for the bug

(it uses std::cout when it should have been gameStuff::cout)

You can either:

- put using namespace std in the main function (that's what nightcreature was talking about). This will tell the compiler "using this namespace but only for this function")

- be more specific with your using statements, that is:


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

instead of the much more broad "using namespace std".

If you want to chat with us you can! After you've logged in there will be a link to the chat page. You'll find it at the top of the page, below the search box. You can also login with an IRC client but I don't recall the address at the moment.

This topic is closed to new replies.

Advertisement