Advertisement

C++ Workshop - Project 1

Started by August 16, 2006 05:41 PM
193 comments, last by me_minus 15 years, 3 months ago
JWalsh; The latter.

Imagine playing craps with a random number generator that just did '(rand()%10) + 2'. You'd never get those sevens.

kingpinzs, read the end of page 3 in this thread.
eeh, burricks. see, my problem with the main() function is that i declare my menus as functions as well, but it says when i compile it, that they can't be accessed. and if i do it the way DevC++ tells me to, then i would have a hard time getting the functions right, because id have to place them before the main() function. i think im going to just use goto() commands and labels, thatll make things a lot easier, unless one of you guys gots an answer for me.

if you can give me pointers on how to declare a class and subset functions, id be great, i tried what i thought was the right way, but i guess it wasnt, oh well. heres what i tried:

class Menus : public MainMenu(){}


what did i do wrong here? C++ is so confusing some times.... aggg..


Quote:
Original post by kingpinzs
I have a few questions.

First what does AC, 1d and a number or 2d and a number all stand for?
Second question is it best to make a struct and a vector for the purchase equetment functions?


I may not be fluent in C++, but i am very fluent on how D+D works.

First off: AC stands for "Armor Class", which is the chance to hit a particular character. You roll 1d20 + Attack Bonuses, if your sum is equal or over, you hit. If you score a perfect 20, you roll again, if you hit, you cause a critical. In some cases, you have a weapon whose critical is something like this "19-20/x#", the "19-20" is the roll youd need to 'activate' a critical, then you would do a confirmation roll. the "/x#" is what to multiply the damage by.

1d# (or something similar): This is a die roll, a roll of 1d20 is a roll of one 20 sided die, a 2d10 is a roll of 2 ten sided dice, and so on.

As for the store, i have no clue. hope this helps homes.
Advertisement
Assuming MainMenu exists,

class Menus : public MainMenu //no parans here{}; //semi colon here


The question is, is that doing what you wanted? It's declaring a class Menus that derived from MainMenu. Your post is, overall, confusing.

What's wrong with sticking the functions before main()? Nothing, that's what. And don't, for the love of god, use gotos.
the problem with sticking the functions before main() is that there would be quite a few of them, and assuming from what i get when i place a function after main(), i would have to have them in the right order in order to compile correctly.

what i want basically is to define a class : "Menus", with sub-classes (or whatever), ie : "MainMenu", which is where i would hold all the menus.

excuse me if im confusing, im a complete n00b at C++ and it still befuddles me at times, though im working around that with my current monatery situation, and therefore not being able to purchase the books necessary to further my "education." im hopefully going to get a job at a local Barnes+Noble, so not only will i get a 40% or so discount, but ill also be getting my own money, so i can finally buy the books i need.

and whats so wrong with goto()'s? it would be a simple and effective solution to my problem, unless they have some inherent flaw that im not aware of, in which case, please, enlighten me.
Quote:
Original post by RavynousHunter
the problem with sticking the functions before main() is that there would be quite a few of them, and assuming from what i get when i place a function after main(), i would have to have them in the right order in order to compile correctly.

what i want basically is to define a class : "Menus", with sub-classes (or whatever), ie : "MainMenu", which is where i would hold all the menus.

excuse me if im confusing, im a complete n00b at C++ and it still befuddles me at times, though im working around that with my current monatery situation, and therefore not being able to purchase the books necessary to further my "education." im hopefully going to get a job at a local Barnes+Noble, so not only will i get a 40% or so discount, but ill also be getting my own money, so i can finally buy the books i need.

and whats so wrong with goto()'s? it would be a simple and effective solution to my problem, unless they have some inherent flaw that im not aware of, in which case, please, enlighten me.


You're local library should have at least one C++ book.

It sounds like there are a few things that you're not utilizing in your program. The first is function prototypes.

int main(){    dosomething();    return 0;}void dosomething(){    cout<<"hi";}


Won't compile because you are using a function that hasn't been defined yet. However.

void dosomething();int main(){    dosomething();    return 0;}void dosomething(){    cout<<"hi";}


Will compile because you are declaring the function with a prototype. The compiler sees that the fucntion exists and when it's called by main goes and gets the function information. This is how you can use functions in main that are defined after main.

However, one of the things that you are going to want to utilize in this project is multiple header and source files. You can put the class declarations in a separate header file, the class function code in a separate source file with the same name as the header, and then include the header file in your main function.

That way all the code is separate and easier to maintain. This is something that you definitely want to learn if you don't already. Once you start making any windowed game you are going to have wayyyy too much code to have it all in one file.

As an example here is how it would look:

In the main file:
#include <iostream>#include "Classes.h"using namespace std;int main(){    ClassHi Hello;    Hello.DoSomething();    return 0;}

in the header file, Classes.h:
class ClassHi{public:    void DoSomething();};

in the source file, Classes.cpp:
#include <iostream>#include "Classes.h"void ClassHi::DoSomething(){     std::cout<<"Hi";}


In this, your main code is the only thing in your main cpp file. Your class information is in a header file so people can see what is in your class. And the nuts and bolts of your class is in a separate source file for modifying or seeing how something works. When you have 5,000 lines of code it makes everything much easier. You can also use multiple source and header files in the same way. My last game had 8 different source and header files for stuff like windows code, graphics code, audio code, user input code, and game code.

[edit: I guess I was wrong, some people do use goto's] Goto's are generally shunned in C++. There is usually a better way around use of a goto. Loops take the place of goto's, and if you're doing it just so your program compiles, you aren't utilizing prototypes or proper design. The few exceptions where it might be ok to use one will not apply to you in this project. If they do, you're doing something the hard way.

If your library really doesn't have any c++ books, you can use google to find a free copy of the book the workshop is using (Sam's teach yourself c++ in 21 days) online, it's only the 2nd edition, but it is still very helpful.

[Edited by - ChurchSkiz on August 31, 2006 8:41:37 AM]
Quote:
the problem with sticking the functions before main() is that there would be quite a few of them,
So?

Quote:
and assuming from what i get when i place a function after main(), i would have to have them in the right order in order to compile correctly.
Forward declare them. Also, google for C++ header tutorials.


Quote:
what i want basically is to define a class : "Menus", with sub-classes (or whatever), ie : "MainMenu", which is where i would hold all the menus.
Then you have it backwards. MainMenu should derive from Menu.

Quote:
and whats so wrong with goto()'s? it would be a simple and effective solution to my problem, unless they have some inherent flaw that im not aware of, in which case, please, enlighten me.


http://www.c2.com/cgi/wiki?GotoConsideredHarmful
http://www.c2.com/cgi/wiki?GotoStillConsideredHarmful

Advertisement
I got some more questions on the design. Sorry if this was addressed earlier, I skimmed all the questions and replies.

I have some questions about the Create Character screen.

You said if you have a character already, the program will say that if you proceed the character will be deleted. So if you say yes, then the previous character will be deleted right then, right?

What if someone makes a name without rolling for stats? Should I implement a random roll for stats as soon as a name was created?

What if someone rolls for stats without entering a character name? Should I have a default name?



P.S. Thank you samanime for your help earlier. I decided to stick with doing states since I figured it will be useful for my future projects and it just seems to use all of the C++ I've learned.

I have a question.
The input in all the menus is a character. How do we handle that.
I tried searching and i found in on of the workshops a method using strings.
Is there maybe a simpler method?
The C++ stream system is, unfortunatly, rather klunky and not really designed for 'immediate' input. Meaning that you'll have to press the key for the selection you want, then press enter. If you just want to detect the selection key as soon as it's pressed, you'll find it much easier to use the C-api function 'getch'.
The C++ stream system does have the advantage that it's terribly simple for complex types. For example, "cin >> some_int;". To mimick that with the C-api you'd have to accept a string of numbers, then convert it to an integer. It's not at all plesant.
How can I generate random names? I am not sure how to randomize string variables. I am not sure how to create the list of the names.

This topic is closed to new replies.

Advertisement