9 Replies - 1290 Views - Last Post: 23 August 2014 - 08:23 AM Rate Topic: -----

#1 KrazENooB   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 06-August 13

Simple question from a noob.

Posted 19 August 2014 - 04:41 AM

So, just to state, I am NOT a student, I am simply someone trying to better my range of skills and teach myself to write c++ code, mostly to learn game development. I am in no hurry to start working on the next big thing, for now just content with hobbyist coding and trying to learn how/why things work.

So, here is my question.

I am messing about with the idea of a tick-based text based combat adventure thing, in console. Basically you have two stats, and HP, and you may use different actions to combat mobs, and you will act until you die, and then a score will output. I am just learning, so I tried to think of something simple.

So, I broke it down.

Main initiate game via "menu" options.
then, upon "NEW" the code will then run the "Game Loo" which will be made up of set of functions that loop.

But before I started all that, I wanted to touch on the idea of initializing all the games data in one function, and I wanted to see if my function was running, so I gave it a return value and then put it into my main, and it's giving a weird output from what I expected.

So, what are ways to sort of test a function that returns nothing, but initializes data for later use.

// HELLO MIKE FOXTROT!

/* 
	MULTI LINE COMMENTS	CHECK IT OUT!
	MIKE FOXTROT!
*/

#include<iostream>
using namespace std;

int INIT_VARIABLES(){
	// Player Stats
	int ipAtk, ipDef = 0;
	// Player HP
	int ipHP, ipMHP = 0;
	// Mob Stats
	int ieAtk, ieDef = 0;
	// Mob HP
	int ieHP, ieMHP = 0;
	return 0;
}

int main(){
	cout << "HELLO, MIKE FOXTROT!" << endl;
	getchar();
	cout << "C++, MIKE FOXTROT!" << endl;
	getchar();
	cout << INIT_VARIABLES << endl;
	getchar();
	return 0;
}


Output =

HELLO, MIKE FOXTROT!

C++, MIKE FOXTROT!

(Random Number/Letter combo?!)

This post has been edited by Skydiver: 22 August 2014 - 09:34 PM
Reason for edit:: Replaced profanity with something more PG.


Is This A Good Question/Topic? 0
  • +

Replies To: Simple question from a noob.

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Simple question from a noob.

Posted 19 August 2014 - 05:39 AM

On line 28, you are printing out of the address of your function. You are not calling it.

Remember to keep things PG Rated. It's in the forum Terms of Service.

And additionally, your function does return something. Line 11 says that the function will return an int, and on line 20 you delivered by returning 0.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Simple question from a noob.

Posted 19 August 2014 - 07:47 AM

One thing to note: all of those variables you initialize in that function cease to exist once you return from that function.
Was This Post Helpful? 0
  • +
  • -

#4 pythonuser007   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 25-January 14

Re: Simple question from a noob.

Posted 22 August 2014 - 05:28 AM

If you need to use the variables in function INIT_VARIABLES then you would need to either initialise the variables in the function as static int and return them in an array or initialise them as pointers in the main function or you could put them into a structure and declare a pointer to the struct to pass it around to the functions you need. As it is now you lose the variables after the function returns as they go out of scope.

Also my personal opinion is , if you want to make kicking games, learn and use C not C++ as it is faster and not as messy as in feature loaded. Too many features in C++ contribute to the shooting yourself in the foot syndrome. Too many libraries and too much magic for me, id rather know what my code was doing.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Simple question from a noob.

Posted 22 August 2014 - 06:27 AM

View Postpythonuser007, on 22 August 2014 - 08:28 AM, said:

Also my personal opinion is , if you want to make kicking games, learn and use C not C++ as it is faster and not as messy as in feature loaded. Too many features in C++ contribute to the shooting yourself in the foot syndrome. Too many libraries and too much magic for me, id rather know what my code was doing.

Pfft. And I'd say that since programmers are human, heavy use of pointers (in C) leads to memory leaks and security issues. The C++ libraries are likely to be safer and faster than any given programmer's attempt at reinventing the wheel.

Plus, with better OOP capabilities you can better organize your code and develop faster.

... and the speed differences are trivial or nonexistent in most cases.
Was This Post Helpful? 1
  • +
  • -

#6 KrazENooB   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 33
  • Joined: 06-August 13

Re: Simple question from a noob.

Posted 22 August 2014 - 02:53 PM

First, just want to say thank you to everyone interested in helping me learn a bit more about this, and I had completely forgotten that those variables would end up local to that function as it stands. Silly me...

I have not gotten a chance to do more with this, I just wanted to rush back and apologize as I posted this one no sleep, and after a 12 day at work while watching Samuel jack son clips on you-tube.

So, sorry about that.

I will mess about a bit, if anyone has any good literature for me, any at all. I am always looking for new tutorials, or guides, or new ways to learn. OOP and Classes are giving me allot of trouble, not allot of the explanations are making it all click with how I should be structuring this. As someone else had said, I am NOT trying to re-invent the wheel.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Simple question from a noob.

Posted 22 August 2014 - 09:45 PM

View Postpythonuser007, on 22 August 2014 - 08:28 AM, said:

If you need to use the variables in function INIT_VARIABLES then you would need to ... initialise them as pointers in the main function ...


NO! DO NOT set them up as pointers to integers like you did in your post on the other thread's lines 49-54. (Inserted here as a spoiler for reference.)
Spoiler


What you can do is pass in pointers to the integers. This is because it's a complete waste of resources (CPU time, memory, and most of all developer time) to have to allocate space for integers, and then pass pointers to them, when you could just get pointers to the integers.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Simple question from a noob.

Posted 22 August 2014 - 10:12 PM

Fixed that for you:

View PostCTphpnwb, on 22 August 2014 - 09:27 AM, said:

... and the speed differences are trivial or nonexistent in most modern cases.


With the first generation C++ compilers, and the computers at that era, there was just enough overhead in C++ that the overhead added up overtime. I remember getting hired on to work on a v3 of a project. v1 was in pure C. v2 was written from scratch in C++. The decision was made to write v3 from scratch in pure C once more because v2, although much easier to debug and understand, was also much slower than v1. When v3 was done, it had regained much of the speed and was comparable to v1.

At that time, the C++ code for v2 would go through the equivalent of a CFront pre-compiler to generate C code which would then be compiled into the executable. C++ compiler technology has jumped forward many years since the 90's. A few years later, one of my friends took time to recompile the original v2 code using a modern C++ compiler and ran it side by side against v3. The v2 code ended up being faster. Of course, he didn't recompile the v3 code with a modern C compiler to make it fully an apples to apples comparison.

Personally, with modern computers and compilers, C++ is the way to go if you can write clear legible code. Nowadays, CPU, RAM, and HD is cheap, but developer time is not. Anything that accelerates the developers ability to understand, write, and debug code is worth it. That means writing code that avoids the more obscure aspects of the C++ language. (e.g. If I have to write a comment to indicate that something subtle is happening, I've failed in writing easy to understand code.)

View PostKrazENooB, on 22 August 2014 - 05:53 PM, said:

I will mess about a bit, if anyone has any good literature for me, any at all. I am always looking for new tutorials, or guides, or new ways to learn. OOP and Classes are giving me allot of trouble, not allot of the explanations are making it all click with how I should be structuring this. As someone else had said, I am NOT trying to re-invent the wheel.

Did you look at some the resources listed in the sticky posts at the top of this forum?
Was This Post Helpful? 2
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Simple question from a noob.

Posted 23 August 2014 - 05:56 AM

View PostSkydiver, on 23 August 2014 - 01:12 AM, said:

Fixed that for you:

View PostCTphpnwb, on 22 August 2014 - 09:27 AM, said:

... and the speed differences are trivial or nonexistent in most modern cases.

In my defense, I always assume that we're dealing with modern systems. I remember taking FORTRAN in 1980 when I first heard about the Y2K bugs. I asked my TA why they hadn't fixed the issue by then and he said that if people were still using the current code in 20 years then it was their own fault. I agree with him, but I'd put the limit for old code at about 10 years. Technology advances too quickly to hold onto ancient parts.

Side note: Given my preference for newer technology, it still fascinates and angers me that Unix time stamps aren't modern enough to hold up past 2038. You'd think they'd have switched to 64 bit time stamps long ago!
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: Simple question from a noob.

Posted 23 August 2014 - 08:23 AM

I agree!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1