Jump to content

  • Log In with Google      Sign In   
  • Create Account

Basic C++ Help Maybe?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 HaVoK87   Members   -  Reputation: 103

Like
0Likes
Like

Posted 11 September 2013 - 04:07 PM

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.



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8192

Like
6Likes
Like

Posted 11 September 2013 - 04:10 PM

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

playAgain = 'y';


#3 HaVoK87   Members   -  Reputation: 103

Like
0Likes
Like

Posted 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!



#4 NightCreature83   Crossbones+   -  Reputation: 2822

Like
3Likes
Like

Posted 11 September 2013 - 04:50 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).


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#5 HaVoK87   Members   -  Reputation: 103

Like
0Likes
Like

Posted 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!



#6 Code_Grammer   Members   -  Reputation: 225

Like
0Likes
Like

Posted 12 September 2013 - 10:02 AM

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.



#7 HaVoK87   Members   -  Reputation: 103

Like
0Likes
Like

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


#8 molehill mountaineer   Members   -  Reputation: 595

Like
0Likes
Like

Posted 16 September 2013 - 11:08 AM

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.


Edited by molehill mountaineer, 16 September 2013 - 11:10 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS