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 


Need help with this simple ANSI C ISO 99 Code

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Aviar³
Grandmaster Cheater
Reputation: 50

Joined: 03 Jan 2008
Posts: 655
Location: Canada

PostPosted: Mon Sep 07, 2009 7:41 pm    Post subject: Need help with this simple ANSI C ISO 99 Code Reply with quote

Always messes up when asking for input to store to codigoBarra:

Code:
#include <stdio.h>


void defdata(struct datos * datpoint, int amount);
void mosdat(struct datos * datpoint, int amount);

struct datos{
   int edad;
   float sueldo;
   int codigoBarra;
   char sexo[9];
   float cotas;
   float particula;
};

int main(void) {
   struct datos * datpoint;
   int amount = 1;
   printf("Cuantas estructuras de datos deceas crear: ");
   scanf("%d", &amount);
   if(amount && amount <= 10 && amount >= 1){
      datpoint = (struct datos *)malloc(amount * (sizeof(struct datos)));
      if (datpoint){
         defdata(datpoint, amount);
         printf("Int edad toma %d bytes.\nFloat suelda toma %d bytes.\nVector de chars sexo toma %d bytes.\n Int codigo de barra toma %d bytes.\nFloat cotas toma %d bytes.\nFloat particulas toma %d bytes. ", sizeof(int), sizeof(float), sizeof(int), sizeof(char), sizeof(int), sizeof(float), sizeof(float));
      }
      else
         printf("No hay sufficiente memoria para almacenar la estructura en memoria \n");
   }
   else
      printf("La capacidad maxima de estructuras permitidas es diez. \n");
   
   return 0;
}

void defdata(struct datos * datpoint, int amount)
{
   int i = 0;
   
   for(i ; i<=amount; i++){
      printf("Ingrese la edad: ");
      scanf("\n%d", datpoint->edad);
      printf("Ingrese sueldo: ");
      scanf("\n%f", datpoint->sueldo);
      printf("Ingrese el codigo de barra: ");
      scanf("\n%d", datpoint->codigoBarra);
      printf("Ingrese sexo (Masculino o Feminino): ");
      scanf("%[^mf]", datpoint->sexo);
        printf("Ingrese cota de edificio: ");
      scanf("\n%f", datpoint->cotas);
       printf("Ingrese de particulas: ");
      scanf("\n%f", datpoint->particula);
        datpoint++;
      
   }
}

_________________
This is the inception of deception, checking the depth of your perception.
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Tue Sep 08, 2009 5:03 am    Post subject: Reply with quote

will datpoint be stored inproperly because its a local variable in 2 functions?
_________________
Back to top
View user's profile Send private message
psychoHacker
Expert Cheater
Reputation: 0

Joined: 31 Dec 2008
Posts: 125
Location: Behind You

PostPosted: Tue Sep 08, 2009 7:02 am    Post subject: Reply with quote

Would be easier to read it those txt would be in english. Only thing I can say in spanish is: chupa me la polla que la tengo muy grande. (Im not even sure is that right)
_________________
This is what happends to your site if you disrespect me:
http://www.zjhuaxing.net/
Back to top
View user's profile Send private message
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Tue Sep 08, 2009 8:20 am    Post subject: Reply with quote

psychoHacker wrote:
Would be easier to read it those txt would be in english.
Yeah, it's always fun to read code in foreign language.. It should not make any difference, but actually it does and it's harder to read.
Back to top
View user's profile Send private message
killersamurai
Expert Cheater
Reputation: 0

Joined: 10 Sep 2007
Posts: 197
Location: Colorado

PostPosted: Tue Sep 08, 2009 9:15 am    Post subject: Reply with quote

Code:

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


void defdata(struct datos * datpoint, int amount);

struct data // datos
{
   int age; // edad
   float salary; // sueldo: can mean salary,pay,wage or Income. I'm choosing salary.
   int barCode; // codigo de barra
   char sex[9]; // sexo
   float elevation; // cota: it's usually level, but elevation sounds better
   float particle; // particula
};

int main(void) {
   struct data * datpoint;
   int amount = 1;
   printf("How many data structures do you wish to create: ");
   scanf("%d", &amount);
   if(amount && amount <= 10 && amount >= 1){
      datpoint = (struct data *)malloc(amount * (sizeof(struct data)));
      if (datpoint){
         defdata(datpoint, amount);
         printf("Int age takes %d bytes.\nFloat salary takes %d bytes.\nchar[] sex takes %d bytes.\n Int barCode takes %d bytes.\nFloat elevation takes %d bytes.\nFloat particles toma %d bytes. ", sizeof(int), sizeof(float), sizeof(int), sizeof(char), sizeof(int), sizeof(float), sizeof(float));
      }
      else
         printf("There is not enough memory in order to store the structure in memory \n");
   }
   else
      printf("The maximum capacity of structures allowed is 10.\n");

   free(datpoint);
   
   return 0;
}

void defdata(struct data * datpoint, int amount)
{
   int i = 0;
   
   for(i ; i < amount; i++){
      printf("Enter the age: "); // Ingrese la edad
      scanf("\n%d", &datpoint->age);
      printf("Enter salary: "); // Ingrese sueldo
      scanf("\n%f", &datpoint->salary);
      printf("Enter the bar code: "); // Ingrese el codigo de barra
      scanf("\n%d", &datpoint->barCode);
      printf("Enter (male or female): "); // Ingrese sexo (Masculino o Feminino)
      scanf("%s", datpoint->sex);
      printf("Enter building elevation: "); // Ingrese cota de edificio:
      scanf("\n%f", &datpoint->elevation);
      printf("Enter particle: "); // Ingrese de particulas
      scanf("\n%f", &datpoint->particle);
      datpoint++;
   }
}


I translated it for those who want to see. The problem was that the original poster treated the members of datos as if there were all pointers. Even though datpoint is a pointer, you have to still treat the members as if datpoint wasn't a pointer. The original poster also forgot to free up some memory. In the above code, I added some fixes to get rid of the errors.
Back to top
View user's profile Send private message
Aviar³
Grandmaster Cheater
Reputation: 50

Joined: 03 Jan 2008
Posts: 655
Location: Canada

PostPosted: Tue Sep 08, 2009 1:48 pm    Post subject: Reply with quote

Thanks, I forgot about freeing memory. I was wondering, why does the C regular expression cause the abnormal execution?
_________________
This is the inception of deception, checking the depth of your perception.
Back to top
View user's profile Send private message
killersamurai
Expert Cheater
Reputation: 0

Joined: 10 Sep 2007
Posts: 197
Location: Colorado

PostPosted: Tue Sep 08, 2009 10:42 pm    Post subject: Reply with quote

Code:

scanf("%[^mf]", datpoint->sexo);


It will stop only when a m or f is pressed. The m or f will not be put into the memory location. This will cause the program to keep on wanting input. This is very bad since the heap will get corrupted if there isn't enough memory allocated to it.
Back to top
View user's profile Send private message
DeletedUser88899
Expert Cheater
Reputation: 5

Joined: 05 Oct 2007
Posts: 196

PostPosted: Mon Nov 09, 2009 3:38 pm    Post subject: Reply with quote

Code:
printf("Ingrese el codigo de barra: ");
scanf("\n%d", datpoint->codigoBarra);


This fails because scanf expects a pointer. The previous scanf's were all strings. Strings are passed by address, integers are passed by value.

Use:

Code:
printf("Ingrese el codigo de barra: ");
scanf("\n%d", &datpoint->codigoBarra);
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