-2

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;   
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Please edit your code into the question as text and explain what isn't working in it. – Retired Ninja Feb 17 '22 at 15:45
  • You may want to read this: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel Feb 17 '22 at 15:46
  • 1
    `sal[3]` is a *single element* (a single character) in the `sal` array. One that is out of bounds. What resource are you using to learn C? What does it say about [`strcpy`](https://en.cppreference.com/w/c/string/byte/strcpy) and how to us it? – Some programmer dude Feb 17 '22 at 15:46
  • You should get a compiler warning for passing an integer value while `%s` expects a `char*`. Unfortunately I cannot copy the related from your picture into that comment. That line will print garbage for the salary or crash. Also your calls to `strcpy` should create some warning like "making pointer from integer value". Read those warnings carefully and address them. Hint: Both warnings are related to using `sal[3]` – Gerhardh Feb 17 '22 at 15:49
  • 1
    Even if you fix the errors mentioned above, `char sal[3];` is not able to hold a string with 3 digits because you also need some room for the terminating 0 byte. – Gerhardh Feb 17 '22 at 15:51
  • 1
    `gets(empdata[100]);` There is no `gets` function in the C language, there is no element at index 100 in the `empdata` array, there is no element in `empdata` that is a valid pointer, and there is no need to have an array of 100 character pointers in this program So that's at least four grave errors in one line of code. – n. m. could be an AI Feb 17 '22 at 16:20
  • Perhaps you may want to start small. Figure out how to declare a string variable. Then figure out how to read a string from the standard input. – n. m. could be an AI Feb 17 '22 at 16:24
  • 1
    See [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/q/1694036/15168)— which covers alternatives too. You have a number for the rate (though at the moment you have it stored as a string). You can use numbers to index into arrays — so create an array of strings with values for the different rate numbers, then convert the rate to a number and index into the array (remembering to handle rate value that are out of control — given that rate 0 isn't valid, you might use the index 0 entry for invalid rates). Arrays can easily be extended to cover more rates. – Jonathan Leffler Feb 17 '22 at 16:49

2 Answers2

0

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.

  • 1
    you mean `strcpy(&sal[0],&sal1[0]); ` – pm100 Feb 18 '22 at 00:36
  • you should also mention that `char f[] = "300"` is better as the compiler will work out the length – pm100 Feb 18 '22 at 00:36
  • In the first comment you are right, i edited it. The second i'm not sure because yes it is better in a certain way but at the same time i prefer using the other way in this case because it remembers you that the string can only be n chars long so that you dont have problems with the destination string being too short in the future. I'm not saying you are wrong but they are both correct form. – Simone Serafini Feb 18 '22 at 15:25
0

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;   
   }
Lily AB
  • 392
  • 2
  • 6