Passing the value to the function Problem

This is a discussion on Passing the value to the function Problem within the C Programming forums, part of the General Programming Boards category; In "Code" function, How can i get the count value from the main program? Cheers Code: #include<stdio.h> #include<string.h> #include<conio.h> #include<stdlib.h> ...

  1. #1
    Unregistered
    Guest

    Unhappy Passing the value to the function Problem

    In "Code" function, How can i get the count value from the main program?
    Cheers

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    #include<stdlib.h>
    void sortarray(struct manufacturer manu[],int length);
    void code(struct manufacturer manu[],int length, struct count c);
    
    //************prototype******************
    struct manufacturer
    {
    	char code[5];
    	char name[16];
    	char address[21];
    	char contact[11];
    	int position;
    };
    struct count
    {
    	int i;
    };
    //*********m a i n *****************
    int main(void)
    {
    	struct manufacturer man[10];
    	struct count ct;
    	int i;
    	
    	for(ct.i=0;ct.i<10;ct.i++)
    	{
    	printf("position main: %d\n",ct.i);
    	code(man,6,ct);
    	
    		
    
    	}
    return(0);
    
    }
    //**************function****************
    void code(struct manufacturer manu[],int length, struct count c)
    {
    
    	
    		
    	printf("Position outside: %d\n",manu[c.i].position);
    	puts("Enter Code ");
    	gets(manu[c.i]. code);
    	
    	}

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm not sure I understand what you mean, but I'll have a go at helping you anyway.

    It looks like you are trying to print the value of manu[c.i].position); within the code() function. When you do so, it gives you garbage. This is because you haven't put anything into the "position" variable yet.

    Try this in the main function:
    Code:
    	for(ct.i=0;ct.i<10;ct.i++)
    	{
    		printf("position main: %d\n",ct.i);
    		man[ct.i].position = ct.i; /* new bit */
    		code(man,6,ct);
    	}
    In fact, the bit I've added could just as easily go in the code() function.
    Hope this helps you!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    You know I gave up trying to understand what you are trying to do there, so instead, I give you something to play with and hopefully you can figure out for yourself what you're trying to do.

    But first things first! Don't make your strings so damn small!

    "char x[4]" type stuff is just a disaster waiting to happen.

    Second, understand that the number one rule in programming is "hand - to - hand" passing of variables. Your program is not psychic. There must be a physical connection between components...

    Third, remember that you have to fill in anything that will be printed and/or processed. Empty variables == crash - OK?

    Anyway, good luck....


    The following progam does NOT answer your question!! But it will run and you can modify it, as necessary. You will have to be more clear on your question to get more detailed help...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <conio.c>  //...needed for clrscr()
    
    
    
    //void sortarray(struct manufacturer manu[],int length);
    
    
    
    
    
    int matches(char *s, char *t)
    {
     if( strstr(s, t) )
      return 1;
     else
      return 0;
    }
    
    void find(struct manufacturer manu[],int length );
    
    //************prototype******************
    
    
    
    
    struct manufacturer
    {
    	char code[20];
    	char name[20];
    	char address[50];
    	char contact[20];
    	int  position;
    };
    
    
    
    
    
    
    
    //*********m a i n *****************
    int main(void)
    {
    
    
    	
        char temp[20];
    	int i;
        int MAX;
    	
        printf("How many employees to enter today?:\n");
        fgets(temp, 20, stdin);
    
        MAX = atoi( temp );
    
        if( MAX == 0)
          return 0;
    
    
    	struct manufacturer man[ MAX ];
    
    
        for(i=0; i< MAX; i++)
    	{
    	 printf("please enter a name:\n");
         fgets(man[i].name, 20, stdin);
    
         printf("please enter his position:\n");
         fgets(temp, 20 ,stdin);
    
         man[i].position = atoi( temp );  //...convert string to integer...
    	
         printf("please enter the job code:\n");
         fgets(man[i].code, 20, stdin);
    
    	 printf("please enter the worker's address:\n");
         fgets(man[i].address, 50, stdin);
    
    	 printf("please enter a contact number:\n");
         fgets(man[i].contact, 20, stdin);
    
         clrscr();
    	}
    
    
            find(man, MAX );
     
            return(0);
    
    }
    //**************function****************
    void find(struct manufacturer manu[],int length)
    {
    
        #define FALSE 0
    	#define TRUE  1
    	
    	char choice[100];	
    	int i;
    	int again = TRUE;
        int found;
    
    
     do{
    
     printf("please enter a code to search for, 'X' to exit:\n");
     fgets(choice, 100, stdin);
    
    
       if( matches( choice, "x" ) || matches(choice, "X") )
        again = FALSE;
    
       found = FALSE;  //...reset variable...
    
    	for(i=0; i<length && again == TRUE ; i++)
    	{
             if( matches(manu[i].code, choice) )
              {
               found = TRUE;
    
               printf("Matches for search : %s\n", choice);
    
               printf("name: %s\n", manu[i].name);
               printf("position: %d\n\n", manu[i].position);
    	       printf("address: %s\n", manu[i].address);
    	       printf("contact: %s\n", manu[i].contact);
               getch();
               clrscr();	 	
             }
    	}
    
        if( found == FALSE && again == TRUE)
         {
           printf("\nSorry, no matches for that code\n");
           getch();
         }
    
            clrscr();
    
    	}while(again);
    
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Unregistered
    Guest
    Thanks
    i know i should use position instead of struct count
    but the problem is in main i declare the structure as an array
    "struct manufacturer man[10];"

    i want to use "position as a counter to show the number of records currently in the file. how can i use like this
    "gets(manu[manu[?].position]. code);" in the function
    it should be like this "gets(manu[i].code);
    how can i solve this problem?



    Code:
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    #include<stdlib.h>
    void sortarray(struct manufacturer manu[],int length);
    void code(struct manufacturer manu[],int length);
    
    //************prototype******************
    struct manufacturer
    {
    	char code[5];
    	char name[16];
    	char address[21];
    	char contact[11];
    	int position;
    };
    
    //*********m a i n *****************
    int main(void)
    {
    	struct manufacturer man[10];//*****?
    
    	int i;
    	
    	for(i=0;i<10;i++)
    	{
    	printf("position main: %d\n",i);
    	code(man,6);//*****?
    	
    		
    
    	}
    return(0);
    
    }
    //**************function****************
    void code(struct manufacturer manu[],int length, struct count c)
    {
    
    	
    		
    	printf("Position outside: %d\n",manu.position);
    	puts("Enter Code ");
    	gets(manu[manu[?].position]. code);//****???
    	
    	}

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    "gets(manu[manu[?].position]. code);" in the function
    it should be like this "gets(manu[i].code);
    Again, (sorry), but I don't quite understand what you mean?

    I have amended your latest code slightly. Have a look and see if it does what you want.
    Code:
    /*$T JUNK2.C GC 1.137 05/06/02 11:31:44 */
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
    #include <stdlib.h>
    
    struct manufacturer
    {
    	char	code[5];
    	char	name[16];
    	char	address[21];
    	char	contact[11];
    	int		position;
    };
    
    /*
     -------------------------------------------------
        prototype
     -------------------------------------------------
     */
    void	sortarray(struct manufacturer manu[], int length);
    void	code(struct manufacturer manu[], int length, int);
    
    /*
     =================================================
        m a i n
     =================================================
     */
    int main(void)
    {
    	/*~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    	struct manufacturer man[10];	/* ? */
    	int					i;
    	/*~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    	for (i = 0; i < 10; i++)
    	{
    		printf("position main: %d\n", i);
    		code(man, 6, i);			/* ? */
    	}
    
    	return(0);
    }
    
    /*
     =================================================
        function
     =================================================
     */
    void code(struct manufacturer manu[], int length, int i)
    {
    	manu[i].position = i;
    	printf("Position outside: %d\n", manu[i].position);
    	puts("Enter Code ");
    	gets(manu[i].code);	/* ??? */
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21