Home > Software engineering >  Copy the string value to another string
Copy the string value to another string

Time:02-20

Problem: A program that will ask an input of an employee data in this format:

ID, firstname_lastname, rate, address, position

Example:

001-111, Juan_DelaCruz, 2, Nasipit Agusan del Norte, Manager

Let Salary rate be equivalent to: 1=250, 2=300, 3=350, 4=375

From the input above, it will display like this:

Employee:001-111
First Name: Juan
Last Name: Dela Cruz
Salary rate: 300
Address: Nasipit Agusan del Norte
Position: Manager

I want the salary rate to automatically display its corresponding equivalent value based on the metric set above. I'm trying to figure out how to manipulate the strings so that whatever the result from string 'rate' will be passed on to string 'sal' with its value. Any inputs will be appreciated!

#include <stdio.h>
#include <string.h>

int main () {
    char *empdata[100];
    char *id, *fname, *lname, *rate, *add, *posi;
    
    printf("Employee data format: ID, firstname_lastname, rate, address, position");
    printf("\nEmployee data example: 001-111, Juan_DelaCruz, 2, Nasipit Agusan del Norte, Manager");
    printf("\nLet Salary rate be equivalent to: 1=250, 2=300, 3=350, 4=375");
    printf("\n\nEnter your data: ");
    gets(empdata[100]);
    
    id=strtok(empdata[100], ",");
    fname=strtok(NULL, "_");
    lname=strtok(NULL, ",");
    rate=strtok(NULL, ",");
    add=strtok(NULL, ",");
    posi=strtok(NULL, ",");
    
    char sal1[5]="250", sal2[5]="300", sal3[5]="350", sal4[5]="375";
    char sal[3];
    
    if (strncmp(rate,"1", 1) == 0)  {
        strcpy(sal[3],sal1[5]);
    } else if (strncmp(rate,"2", 1) == 0) {
        strcpy(sal[3],sal2[5]);
    } else if (strncmp(rate,"3", 1) == 0) {
        strcpy(sal[3],sal3[5]);
    } else if (strncmp(rate,"4", 1) == 0) {
        strcpy(sal[3],sal4[5]);
    }

    printf("\n\nHere's your employee data from the input above: ");
    printf("\n\nEmployee: %s", id);
    printf("\nFirst Name: %s", fname);
    printf("\nLast Name: %s", lname);
    printf("\nSalary Rate: %s", sal[3]);
    printf("\nAddress: %s", add);
    printf("\nPosition: %s", posi);
    
    return 0;   
}

CodePudding user response:

In the code there are 2 problems:

  1. The strings declaration is wrong.
    In C strings are made by the actual characters and \0 (\0 is the string terminator).
    In your code you declarated 4 strings, each one is a char[5] meaning it can contain 5 chars (4 characters \0), the string in which you want to copy the values is a char[3] meaning it can contain only 3 chars ( 2 characters \0).
    They arent compatible because in your scenario you are trying to copy a string of 3 characters \0 to a string that can contain only 2 characters \0.
    For example ,from your code, sal1[5]="250" in reality is "250\0" using 4/5 chars The correct declaration would be:
char sal1[5]="250", sal2[5]="300", sal3[5]="350", sal4[5]="375";
char sal[5];
  1. The strcpy parameters are wrong.
    In C strcpy() needs two parameters: char* destination and char* source, as you can see they both are char pointers.
    The pointers have to be pointers to the first element of the string because the functions copies the string starting from the pointer you gave as parameter to the terminator \0.
    In your code you wrote as parameter sal[3] and sal[5] wich are the last elements of the two strings. This leads to a lot of errors.
    The right use of strcpy() in your code is:
 strcpy(&sal[0],&sal1[0]);  
 //or  
 strcpy(sal,sal1);

Also, not raleted to the question, the printf at the end is wrong for the same motivation.
The correct version is:

printf("\nSalary Rate: %s", sal);

I hope my anser was useful. Sorry if i said something wrong, i'm kind of a new user and english is not my first language.

CodePudding user response:

Note the space after the comma in the sample input...

001-111, Juan_DelaCruz, 2, Nasipit Agusan del Norte, Manager

Post rate=strtok(NULL, ",");, rate contains two characters, space and '2', terminated by '\0'. Later, when performing a strncmp you have to accommodate for this.... An example would be...

(strncmp(rate," 2", 2) == 0);

I am sure you will figure more robust implementations, as time goes by...

Here is my version, with minimal changes to the current implementation.

Please see the comments.

int main () {

  //char *empdata[100]; /* declares "empdata" as array[100] of pointers to char.*/
   char empdata[100]; /* declares "empdata" as array[100] of characters */
   char *id, *fname, *lname, *rate, *add, *posi;

   printf("Employee data format: ID, firstname_lastname, rate, address,  position");
   printf("\nEmployee data example: 001-111, Juan_DelaCruz, 2, Nasipit Agusan del Norte, Manager");
   printf("\nLet Salary rate be equivalent to: 1=250, 2=300, 3=350, 4=375");
   printf("\n\nEnter your data: ");
   //gets(empdata[100]); /* the gets function was officially removed from C`s \
                            2011 international standard \
                            use fgets(char *s, int n, FILE * stream); \
                            fgets returns 's' or NULL if EOF or error occurs */
   fgets(empdata,100,stdin);

   //id=strtok(empdata[100], ","); /* strtok expects a character pointer */
                                   
   id=strtok(empdata, ","); /* "empdata" is the "address of/points to" the first \
                               element of the array*/
                         //Have made similar changes to the calls to strcpy and printf
   fname=strtok(NULL, "_");
   lname=strtok(NULL, ",");
   rate=strtok(NULL, ",");
   add=strtok(NULL, ",");
   posi=strtok(NULL, ",");

   char sal1[5]="250", sal2[5]="300", sal3[5]="350", sal4[5]="375";
  //char sal[3]; /* make sure to map the array sizes when copying \
                    will ensure that the '\0' terminator is copied as well */
   char sal[5];
   if (strncmp(rate," 1", 2) == 0)  {
       strcpy(sal,sal1);
   } else if (strncmp(rate," 2", 2) == 0) {
       strcpy(sal,sal2);
   } else if (strncmp(rate," 3", 2) == 0) {
       strcpy(sal,sal3);
   } else if (strncmp(rate," 4", 2) == 0) {
       strcpy(sal,sal4);
   }

   printf("\n\nHere's your employee data from the input above: ");
   printf("\n\nEmployee: %s", id);
   printf("\nFirst Name: %s", fname);
   printf("\nLast Name: %s", lname);
   printf("\nSalary Rate: %s", sal);
   printf("\nAddress: %s", add);
   printf("\nPosition: %s", posi);

   return 0;   
   }
  • Related