how can i copy 5 fields into 1variable

This is a discussion on how can i copy 5 fields into 1variable within the C Programming forums, part of the General Programming Boards category; i have 5 variables. after user input all data in those variables[characters]. then i have to put all data from ...

  1. #1
    Unregistered
    Guest

    Question how can i copy 5 fields into 1variable

    i have 5 variables. after user input all data in those variables[characters]. then i have to put all data from 5 variables into 1 variable which goes to 1 array and i have to repeat untill the user has finished entering dat.
    manuindex[i]=code,name,address,contact
    how can i do this thing
    this is my try
    Code:
    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <math.h>
    struct Manufacturer 
        {
                          char manuindex[100,62], code[6],nam[16],              address[21],contact[11];
                        int i;
                      //bool pass;
          };
    main(void)
    {
            struct Manufacturer m;
            puts("Enter code");  //
    
              gets(m.code);	
    	
            //fflush(stdin);
    
              for(m.i=0;m.i<100;m.i++);
           {	
                   while (strlen(m.code)>0)
                       {
     	
                          puts("Enter name:");
                          gets(m.name);
                          puts("Enter address");			
                          gets(m.address);
                          puts("Enter Contact");
                          gets(m.contact);
                          strcpy(m.manuindex[m.i],m.code);
                          strcat(m.manuindex,m.name);
                          strcat(m.manuindex,m.address);
                          strcat(m.manuindex,m.contact);
                          puts("Enter code:");
                          gets(m.code);
                         }
              }
          printf(m.manuindex[1]); // just checking data
    }
    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: how can i copy 5 fields into 1variable

    >char manuindex[100,62],
    This bit is a syntax error. If you want a 2d array, do so like this:
    char manuindex[100][62],

    >nam[16],
    Should this say: name[16] ?

    >//fflush(stdin);
    Leave that one commented out, it's undefined so not guaranteed to work on all systems.

    >gets()
    is very bad, it does no bounds checking (search board for more information). Try using fgets() instead.

    >strcpy() and strcat()
    This doesn't do bounds checking, try using strncpy() and strncat() to keep your prog safe.

    Lastly, add return(0); to the end of the main() function.

    The program won't compile with those syntax errors. If you're new to C, try compiling it anyway so that you get used to the error messages that the compiler comes back with. That way you'll be able to fix the simple errors yourself.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest

    Unhappy

    thanks for ur help
    i fixed some problems and this program below it seems to be working ok but i have one problem. when it goes through the function "showD" to display the value of the variable,the value doesn't go with it
    it can show the value in the main but not outside
    how can i solve this problem?
    thanks
    Code:
    include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <math.h>
    
    struct Manufacturer 
    	{
    char manuindex[100][62], code[6],name[16], address[21],contact[11];
    int i;
    //bool pass;
    };
    void showD(char test[]);
    int main()
    {
    	char tt[62];
         struct Manufacturer m;
         	m.i=0;
        puts("Enter code");  //
    
          gets(m.code);	
    	
          //fflush(stdin);
    
         
               while (strlen(m.code)>0)
                 {
     	
                  puts("Enter name:");
                 gets(m.name);
                 puts("Enter address");			
                 gets(m.address);
                 puts("Enter Contact");
                 gets(m.contact);
                    strcpy(tt,m.code);
                   strcat(tt,m.name);
                    strcat(tt,m.address);
                    strcat(tt,m.contact);
                    strcpy(m.manuindex[m.i],tt);
                puts("Enter code:");
                gets(m.code);
                m.i=m.i+1;
                  }
                  showD(m.manuindex);
    //puts(m.manuindex[0]);
    //printf("%s",m.manuindex[0]); // just checking data
    return(0);
    }
    
    
    /////*************Function***************////
    void showD(char test[])
     {
    	 
    	// struct Manufacturer mn;
    //puts(test[0]);
    printf("%s",test[0]);
    
    }

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    When you print a string you need to include a pointer to the string and not the first charector.

    Here is what you need to do.

    Code:
    void showD(char test[])
    {
    	 
    	// struct Manufacturer mn;
    //puts(test[0]);
    printf("%s",test); 
    }
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    Unregistered
    Guest
    How can I just show the first element of the array showD[0] not all of it?
    Sean - just putting the array name like "printf("%s",test);" doesn't seem to work either
    Any suggestions?
    Thanks

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How can I just show the first element....
    You can pass the address of the first string only to the function showD() like this:
    Code:
    showD(m.manuindex[0]);
    Then the printf in showD() should look like this:
    Code:
    printf("%s",test);
    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. Lists: adding fields (not nodes) & more
    By Mariano L Gappa in forum C++ Programming
    Replies: 15
    Last Post: 11-09-2005, 07:26 PM
  2. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-22-2005, 12:55 AM
  3. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 03:41 AM
  4. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM

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