Declaring Length

Declaring Length

Post by Its just me on Sat Dec 03, 2011 11:43 pm
([msg=63175]see Declaring Length[/msg])

A few months ago I decided to learn the programming language C. Throughout those months I recurring problem has been coming up in my code, the clients have to declare 'how many of something' or 'how long something will be' and in my mind I see this as a problem since the user does not always know.

Example of my problem:
Code: Select all
#include <stdio.h>
#include <stdlib.h>

/*
   Some unfamiliar syntax in this code might be the
   Get* functions. These are from the cs50 library.
   The functions take the user's input and converts
   it to the data type indicated.
*/
#include <cs50.h>

typedef struct
{
    int number;
    char *name;
}
player;

int
main(void)
{
    // This is where the problem lies, the program is asking the users how many players are on his/her team
    printf("Number of Players: \n");
    int size = GetInt();

    player team[size];

    printf("%d\n", sizeof(player));

    for (int i = 0; i < size; i++)
    {
   printf("Player's Number: ");
   team[i].number = GetInt();

   printf("Player's Name: ");
   team[i].name = GetString();

   printf("\n");
    }

    FILE *data = fopen("database", "w");
    if (data != NULL)
    {
   for (int j = 0; j < size; j++)
   {
       fprintf(data, "%d\n", team[j].number);
       fprintf(data, "%s\n", team[j].name);
   }
   fclose(data);
    }

    for (int i = 0; i < size; i++)
    {
   free(team[i].name);
    }
}
Its just me
New User
New User
 
Posts: 12
Joined: Tue Nov 30, 2010 5:54 pm
Blog: View Blog (0)


Re: Declaring Length

Post by LoGiCaL__ on Sun Dec 04, 2011 4:27 am
([msg=63178]see Re: Declaring Length[/msg])

the clients have to declare 'how many of something' or 'how long something will be' and in my mind I see this as a problem since the user does not always know.


If they don't know/won't know, why ask them?

I could be missing the point of it completely, but check into dynamic memory allocation.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: Declaring Length

Post by ghost107 on Sun Dec 04, 2011 7:23 am
([msg=63179]see Re: Declaring Length[/msg])

When you want to allocate dynamic size its better to use pointers:
So in your example:
Code: Select all
  // This is where the problem lies, the program is asking the users how many players are on his/her team
    printf("Number of Players: \n");
    int size = GetInt();

    player *team =(player *)malloc(size *sizeof(player));

But the code is for static data (the name pointer in the struct will make it to not work), like:
Code: Select all
typedef struct
{
    int number;
    char name[30];
}
player;


Another method is to use linked lists:
Code: Select all
typedef struct node{
      int index;
      player data;
      node *next;
};
//first node
static  node *first;

//Creating a node
node *addNode(player data, node * it){
     node  *newNode = (node*)malloc(sizeof(node));
     newNode->index = 0;
     newNode->data = data;
     newNode->next = NULL;

     //check if list is empty
     if (first == NULL){
        first = newNode; 
     }
     else{//add a node
        it->next = newNode;
        newNode->index = it->index + 1;       
     }
     return newNode;   
}

//in main will look
int main(){
     player data;
     node *iterator = first;
     printf("Number of Players: \n");
     int size = GetInt();

    //creating the team
    for(int i = 0; i < size; i++){
          printf("Player's Number: ");
     data.number = GetInt();
          printf("Player's Name: ");
     data.name = GetString();
         iterator = addNode(data, iterator);
         if(i == 0){
              first = iterator;
         }
    }
   
    FILE *data2 = fopen("database", "w");
    iterator = first;
    if (data2 != NULL)
    {
     for(int j = 0; j < size; j++){
        if(iterator != NULL){
            fprintf(data2, "%d\n", iterator->data.number);
            fprintf(data2, "%s\n", iterator->data.name);
        }
       iterator = iterator->next;
   }
   fclose(data2);
     return 0;
}


Its not a perfect code since i didn't compiled it(just wrote it directly in this thread), I've tried to put it as much in C with out writing C++ sintax.
C++ allready has linked lists allready defined in STL library.

For more info you can use google for linked list:
ghost107
Poster
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: Declaring Length

Post by Its just me on Sun Dec 04, 2011 10:50 am
([msg=63180]see Re: Declaring Length[/msg])

Thanks guys just what I was looking for
Solved
Its just me
New User
New User
 
Posts: 12
Joined: Tue Nov 30, 2010 5:54 pm
Blog: View Blog (0)


Re: Declaring Length

Post by LoGiCaL__ on Sun Dec 04, 2011 3:23 pm
([msg=63183]see Re: Declaring Length[/msg])

Its just me wrote:Thanks guys just what I was looking for<br>Solved
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests