Thread: small problem

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    small problem

    In the following code I get the error ' freespace: function must return a value' . Could you tell me what's wrong?. Thanks





    Code:
    #include <stdio.h>
    #include <string.h>
    int freespace(char name[][20],int pos_pntr);
    int add_contact(char name[][20],char company[][20],int phone_number[],int pos_pntr);
    void main(void)
    {
     char name[10][20];
     char company[10][20];
     int phone_number[10];
     int pos_pntr=0;
     pos_pntr=freespace(name,pos_pntr);
     pos_pntr=add_contact(name,company,phone_number,pos_pntr);
     
    }
    int freespace(char name[][20],int pos_pntr)
    {
     int count=0,i;
     for(i=0;i<10;i++)
      { 
       strcpy(name[i], NULL);
      }
     
      
     for(i=0;i<10;i++)
     {
      if(name[i]== NULL)
       {
        count++;
       }
       if(count==0)
       {
        i=20;
        return pos_pntr;
       }
       else
       {
        printf("No Free sapce left");
        return;
       }
      }
     pos_pntr++;
     }
    
      
     
    
        
    int add_contact(char name[][20],char company[][20],int phone_number[],int pos_pntr)
    {
     puts("Enter the name fo the contact:-");
     gets(name[pos_pntr]);
     puts("Enter the company name:-");
     gets(company[pos_pntr]);
     puts("Enter the phone number:-");
     scanf("%d",&phone_number[pos_pntr]);
     pos_pntr++;
    
     return pos_pntr;
    
     
    }

    &#91;code]&#91;/code]tagged by Salem

  2. #2
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    your function freespace is an int and MUST return something.

    Code:
    else
    {
    printf("No Free sapce left");
    return 0;
    }
    Try that.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >Could you tell me what's wrong?.

    What wrong is, is this: 'freespace: function must return a value', it means that function freespace() should return a value, but it doesn't.

    Code:
    int freespace(char name[][20],int pos_pntr)
    {
      int count=0,i;
      for(i=0;i<10;i++)
      { 
        strcpy(name[i], NULL);
      }
    
      for(i=0;i<10;i++)
      {
        if(name[i]== NULL)
        {
          count++;
        }
        if(count==0)
        {
          i=20;
          return pos_pntr;
        }
        else
        {
          printf("No Free sapce left");
    
          Here an error value should be returned
          return;
        }
      }
      pos_pntr++;
      Here a value should be returned
    }
    If I read your code well, then your loop will always end immediately, because of the returns in the if-else construction.

  4. #4
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    main() should also return an int as void main() is WRONG!
    Code:
    int main()
    {
       return 0;
    }
    is the ONLY correct way to use main()!!

  5. #5
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    weird... I looked at that when I first posted and it was int main... he must have changed it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 10:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 04:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 03:23 AM