Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Creating Arrays of Structures

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Scathe
I post too much
Reputation: 0

Joined: 02 Aug 2006
Posts: 3631
Location: Smoking a blunt

PostPosted: Wed Apr 01, 2009 1:53 pm    Post subject: [C++] Creating Arrays of Structures Reply with quote

I'm a little confused as to how to pass arrays as parameters in functions.

Here's what i've got so far(This code compiles fine, i haven't set up arrays yet seeing as i can never get it to compile correctly.)

Code:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

struct student{
       string name;
       int age;
       short ID;
       };

string set_name();
int set_age();
short login();

int myInt;
int choice;
string myStr;
short myShort;


int main(){
   
    student stud;
    student* stu;
    stu=&stud;
   
   
    for(int x=0; x<25; x++){
            cout<<"\nWould you like to create a new account, or login to an old one?"<<endl;
            cout<<"\nCreate = 1, Login = 2"<<endl;
            cin>>choice;
           
            if(choice==1)
            {
                         stu->name = set_name();
                         stu->age = set_age();
                         cout<<"\nPlease enter your desired ID# ";
                         cin>>stu->ID;
                         cout<<"\nYour user ID is: "<<stu->ID;
                         x++;
                         
            }
            if(choice==2)
            {
                         login();
                         if(login()==stu->ID){
                         cout<<"\nCongrats you have logged in";
                         cout<<"\nYour info is as follows: "<<endl;
                         cout<<"\nName: "<<stu->name<<endl;
                         cout<<"Age : "<<stu->age<<endl;
                         cout<<"ID  : "<<stu->ID<<endl;
                         }
     else
     cout<<"\nIncorrect login ID";
            }
   }
           
    system("PAUSE");
    return 0;
}
int set_age(){
     cout<<"\nPlease enter your age ";
     cin>>myInt;
     return(myInt);
}
short login(){
     cout<<"\nPlease enter your ID#  ";
     cin>>myShort;
     return(myShort);
}
string set_name(){
     cout<<"\nPlease enter your name ";
     cin>>myStr;
     return(myStr);     
}


Any help as to how i would make an array of the structures, and be able to store multiple sets of data would be greatly appreciated.

_________________

<Moose> they will call me beard penis
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Wed Apr 01, 2009 2:41 pm    Post subject: Reply with quote

I'm not reading your code.


Anyway,

When arrays are defined, they are basically pointers, and the number inside the brackets is the the allocation it will receive. (sizeof(type)*number)

If you want to pass an array, all you have to put in the parameters of the prototype is a reference to the array, and since arrays are already referenced (aka pointers) you can just do something like this:

Code:

void SetBuffer(int *buff, int iToSet)
{
     for(int i = 0; i < 4; i++)
      buff[i] = iToSet;
}

int _tmain(int argc, _TCHAR* argv[])
{
   int buffer[4];
   SetBuffer(buffer,6);
   printf("[0]: %i\n[1]: %i\n[2]: %i\n[3]: %i",buffer[0],buffer[1],buffer[2],buffer[3]);
   Sleep(10000);
   return 0;
}


Something like that would work.



**EDIT:

Didn't see the other question..

You can make arrays a number of ways...
Code:

student stu[15];

Code:

student *stu;

stu = (student*)malloc(15 * sizeof(student));

free(stu); //remember to free when done, or you will get memory leaks
Back to top
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Wed Apr 01, 2009 3:22 pm    Post subject: Reply with quote

Code:
student *fap = new student[15]; // 15 Students
Back to top
View user's profile Send private message MSN Messenger
Scathe
I post too much
Reputation: 0

Joined: 02 Aug 2006
Posts: 3631
Location: Smoking a blunt

PostPosted: Wed Apr 01, 2009 8:15 pm    Post subject: Reply with quote

Thanks guys Smile
_________________

<Moose> they will call me beard penis
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
igoticecream
Grandmaster Cheater Supreme
Reputation: 0

Joined: 23 Apr 2006
Posts: 1807
Location: 0x00400000

PostPosted: Wed Apr 01, 2009 10:40 pm    Post subject: Reply with quote

when i read array of structures, do you mean lists on C?

if then, i have made this for school as a homework long time ago

Code:

#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <conio.h>

typedef struct nodo {
   int numero;
   char letra;
   struct nodo *proximo;
}NODO;

NODO *primero = NULL;

int empty(nodo *primero){
   if (primero == NULL)
      return TRUE;
   else
      return FALSE;
}

nodo *insert(nodo *primero,int num,char let){
    nodo *nuevo, *aux;
    nuevo=(nodo*)malloc(sizeof(nodo));
    nuevo->numero=num;
    nuevo->letra=let;
   if(primero == NULL){
      nuevo->proximo=primero;
      primero=nuevo;
   }
   else{
      aux=primero;
      while(aux->proximo != NULL)
         aux=aux->proximo;
      aux->proximo=nuevo;
      nuevo->proximo=NULL;
   }
   return (primero);
}

...


and in the main when it ask for number and letter

Code:

printf("Numero a agregar a lista: ");
scanf("%d",&numeroinput);
printf("Letra a agregar a lista: ");
scanf("%s",&letrainput);
primero=insert(primero, numeroinput, letrainput);


this will create something liek this (where A represent a struct and where -> represent this points to)

A->A->A->A->A->NULL
[/quote]
Back to top
View user's profile Send private message
Scathe
I post too much
Reputation: 0

Joined: 02 Aug 2006
Posts: 3631
Location: Smoking a blunt

PostPosted: Sun Apr 05, 2009 7:23 pm    Post subject: Reply with quote

Thanks igoticecream that helped as well Very Happy
_________________

<Moose> they will call me beard penis
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Sun Apr 05, 2009 7:45 pm    Post subject: Reply with quote

xwallflowerx wrote:
Thanks igoticecream that helped as well Very Happy


If you are using C++, I would recommend you use the STL Containers. They are pretty efficient speed wise, just mind the bloat that can occur if you don't use them properly (as with everything template'd).
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites