Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ Help, Array error

Name: Anonymous 2007-06-07 12:06 ID:+O+EKF2S

Hey guys this compiles but when i build the selected number of knights only the first one gets a random number and the rest get -801208102 or something?
// knight values is a struct.
knightValues* CreateKnights2(int& numberOfKnights2)
{
    system("CLS");
    srand( (unsigned)time( NULL ) );

    numberOfKnights2 = 0;

    std::cout << "Enter how many Knights you would like to create: ";
    std::cin >> numberOfKnights2;

    knightValues* myCreatedKnights2 = new knightValues[numberOfKnights2];

    for (int i = 0; i < numberOfKnights2; i++){
        myCreatedKnights2[i].health = rand() % 100 + 100;
        myCreatedKnights2[i].armour = rand() % 200 + 150;

    return myCreatedKnights2; // this is being assigned to army in the switch
    }
}
------
knightValues* army;        // army is the pointer to the array of knights
int nKnights;            // this is the number of knights

------
switch (option)
      {
          case 1 : army = CreateKnights(nKnights); break;

Name: Anonymous 2007-06-07 12:08 ID:+O+EKF2S

Don't mind the number 2 that isn't ment to be there, i copied the wrong part as i'm building 2 of them. so when it says anything with a 2 after the name dw about it. And yes it compiles and works but only the first knight gets values the rest dont

Name: Anonymous 2007-06-07 12:27 ID:RgdhJioM

Number of knights? Homework GET! Also, we're not in your mind, we don't know anything about your knights problem you talk about as if it were a 2 years old /prog meme.

Name: Anonymous 2007-06-07 12:31 ID:11V5JN1+

>>2
Your return statement is inside the for loop asshat. So it only ever performs one iteration before returning.

Name: Anonymous 2007-06-07 12:50 ID:Pt5KfWjr

system("CLS") HAHAHAHAHAHAHA

Name: Anonymous 2007-06-07 13:21 ID:zYqviSS6

Use the stl faggot. They created it for a reason you know.

[code]// knight values is a struct.
vector<knightValues> CreateKnights2()
{
    system("CLS");
    srand( (unsigned)time( NULL ) );

    numberOfKnights2 = 0;

    std::cout << "Enter how many Knights you would like to create: ";
    std::cin >> numberOfKnights2;

    vector<knightValues> myCreatedKnights2(numberOfKnights2);

    for (vector<knightValues>::iterator it = myCreatedKnight2.begin(); it != myCreatedKnight2.end(); ++it){
        it->health = rand() % 100 + 100;
        it->armour = rand() % 200 + 150;
    }
    return myCreatedKnights2; // this is being assigned to army in the switch
}
------
vector<knightValues> army;        // army is the vector of knights
int nKnights;            // this is the number of knights

------
switch (option)
      {
          case 1:
              army = CreateKnights();
              nKnight = army.size();
              break;

Name: Anonymous 2007-06-07 13:45 ID:Pt5KfWjr

lol return by value

Name: Anonymous 2007-06-07 13:59 ID:MxycKpfs

>>1
var = 0x104c; // Set the variable var to 0x14c.

Name: Anonymous 2007-06-07 14:01 ID:Pt5KfWjr

lowerCamelCase for struct names and UpperCamelCase for functions.. did your lecturer grow up on CodeWarrior targeting System 6 or something?

Name: Anonymous 2007-06-07 14:24 ID:11V5JN1+

>>6
Using STL won't prevent tards from putting return statements inside for loops.

Name: Anonymous 2007-06-07 19:50 ID:+O+EKF2S

Amazing how some of use go ape shit over something, it's a task i have from the course I'm doing do i have to do it their way...

Name: Anonymous 2007-06-07 19:54 ID:Heaven

fix your own fucking you worthless retard?

Name: Anonymous 2007-06-07 20:03 ID:Heaven

>>1
My other car is a cdr.

Name: Anonymous 2007-06-07 20:21 ID:+O+EKF2S

Thanks Heaven, just thought i might ask for some help instead you "fucking you worthless retard"

Name: Anonymous 2007-06-07 20:49 ID:+O+EKF2S

Ok, I only have one last error with this program now. Not to sure why this is happening. I'm not the best with functions but i came up with this.

My switch holds this:
case 5 : TwoArmysFight(army, nKnights, army2, nKnights2); break;

My battle.h holds this:
void TwoArmysFight(knightValues* myCreatedKnights, int numberOfKnights, knightValues* myCreatedKnights2, int numberOfKnights2)

And my battle.cpp holds this:
void TwoArmysFight(knightValues* myCreatedKnights, int numberOfKnights, knightValues* myCreatedKnights2, int numberOfKnights2)

From my knowledge (new to c++) is it possible a function can't take 4 items inserted into its parameters?

Name: Anonymous 2007-06-07 20:52 ID:Heaven

Oh for fucks sake, just write it in C.

Name: Anonymous 2007-06-07 20:56 ID:+O+EKF2S

It's the fact of i have a set task and how i have to do it. This small program must be written in c++. Yes i probably look like the biggest programming idiot for c++ but I've just started on the topic. So please don't mind how bad some of my skills are for it. I'm only asking for advice and help.

Name: Anonymous 2007-06-07 21:03 ID:Mo7Uj3KT


    for (int i = 0; i < numberOfKnights2; i++){
        myCreatedKnights2[i].health = rand() % 100 + 100;
        myCreatedKnights2[i].armour = rand() % 200 + 150;}//LEARN TO READ YOUR COMPILER'S FUCKING BUILD MESSAGES

BTW, learn what OOP is. And make this more OO.

Name: Anonymous 2007-06-07 21:06 ID:+O+EKF2S

May i ask why no one understands why i have to make this a certain way, i can't just go adding john doe. It needs to meet the requirements for the task.

Name: Anonymous 2007-06-07 21:16 ID:tuIsbGvQ

You wouldn't have had to pass in the sizes of the arrays if you'd used vectors, which would cut out two unnecessary parameters.

And >>16 is right, the way you're coding, you may as well write the damn thing in C.

Name: Anonymous 2007-06-07 21:16 ID:Mo7Uj3KT

Because the person who told you to do this that way is a clueless fucktard

Name: Anonymous 2007-06-07 21:17 ID:9crenf/e

>>19
May i ask why you're posting such horrible code and waste 2 minutes from an EXPERT PROGRAMMERs life?
All your answers are in stroustrup's book and man pages.

Lastly, why a twat like you would pick C++?
You're clearly stupid AND lazy.

I point and laugh @ people who expect to go to a programming course in college and actually learn something about programming.

1) You forgot to check the value of time()
2)
    numberOfKnights2 = 0;
    std::cin >> numberOfKnights2;

PROTIP: you didn't had to set it to 0.
3)
    knightValues* myCreatedKnights2 = new knightValues[numberOfKnights2];

VERRYY SAFE.
4)
for (int i = 0; i < numberOfKnights2; i++){

And what if i enter a negative number?

lol GAME PROGRAMMER WANNABEES.




>>16
i second that

Name: Anonymous 2007-06-07 21:26 ID:Mo7Uj3KT

   knightValues* myCreatedKnights2 = new knightValues[numberOfKnights2]; VERRYY SAFE.

it is, since new returns NULL if memory allocation failed

I also agree with >>16, but he has choosen C++ (because he's a 1337 gaem pr0gr4mmearz) and you can code this in C++ "correctly" in an "object oriented" way to achieve ENTERPRISE QUALITY

Name: Anonymous 2007-06-07 21:26 ID:+O+EKF2S

"Lastly, why a twat like you would pick C++?
You're clearly stupid AND lazy.

I point and laugh @ people who expect to go to a programming course in college and actually learn something about programming."

I guess after just coming out of year 12 from doing actionscript and some VB i got put into c++, this course is with atari and i've only just started it. I have knowledge of coding but not to this sort. As you can tell this is meant to be a basic program just to tick off some criteria. I'm not asking for use to be a hole to me, I'm just asking for some help with what I'm doing. So fuck sorry for wasting some of your pathetic lives.. jesus christ i hope some of use burn. But thanks to those who actually helped me with my simple errors.

Name: Anonymous 2007-06-07 21:30 ID:Mo7Uj3KT

I'm just asking for some help with what I'm doing.

help ? in MY /prog/ ?

Name: Anonymous 2007-06-07 21:40 ID:Heaven

So fuck sorry for wasting some of your pathetic lives..

Well if our lives are `pathetic' how pathetic are you for asking help from us?

you'll never be a real programmer. just quit.

Name: Anonymous 2007-06-07 21:53 ID:+O+EKF2S

Well considering i asked for help hoping i might get some i thought use would be half decent and help me out. I didn't expect to get abused by use at all. All i wanted was at least someone to tell me "yes your function in the battle.cpp is wrong and needs something like this ... " I wasn't expecting to get told to basically do something else..

Name: Anonymous 2007-06-07 21:56 ID:Heaven

>>27
WHAT A TWAT, WHAT AN ATTITUDE!
DO YOU REALIZE YOU'RE TALKING WITH EXPERT PROGRAMMERS HERE?
I DON'T THINK SO.

Name: Anonymous 2007-06-07 22:16 ID:Mo7Uj3KT

>>27
I actually DID help you. You're acting like a retard, will never become an EXPERT PROGRAMMER

Name: Anonymous 2007-06-07 23:34 ID:w/N8HPbk

>>1
Never use a signed integer for navigating an array.

Massive phail.

Name: Anonymous 2007-06-07 23:43 ID:Heaven

>>30
numberofknights2 is type of int too, as long as it's >=0 there's no problem.
There can be other problems with using an unsigned int to navigate an array too.

unsigned int i = UINT_MAX + 1; // what's the value of i? yep.

Name: Anonymous 2007-06-08 16:39 ID:1UY8RXQO

>>31
0

Name: Anonymous 2009-01-14 14:42

LISP

Name: Anonymous 2009-03-06 7:56


You mean that text   editor for toy   operating systems right   and by sdl   you mean the   redditfags and all   that other crap   make print etc   into real functions   and turn it   into a real   OS like Microsoft   with them now   claiming that their   arrival could almost   be curried to?

Don't change these.
Name: Email:
Entire Thread Thread List