Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please I tried so hard to solve this error for more than a week, but I'm stll stuck on it, I've tried to use strcpy also but i got the same conversion error, please does anyone know how to solve this problem? I have an idea of what the error is, I have tried googling for help (to understand it so I can, fix it and , not make it again) I can't seem to figure out how to fix it. :$ I have tried different combinations of * and & to reference and dereference the char,So far, nothing is working! I'm getting VERY frustrated.

I'd appreciate any help in understanding what I need to do. I'm tired of this error! Thanks!!!!

What I have tried:

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

struct employee{
    char street[50],country[50],city[50];
    int building_nb, zip;
} e[100];

int comparator(const void* p, const void* q)
    {
        return strcmp(((struct employee*)p)->country,((struct employee*)q)->country);
    }


int main()
{
    int a,n,space=0;
    struct employee e[100];
    printf("Enter total of employees:");
    scanf("%d",&n);
    for(a=0;a<n;a++)
        {
            printf("\n Enter informations of the employee number %d:\n",a+1);
            printf("Enter street: ");
            scanf("%s", e[a].street);
            
            printf("Enter building number: ");
            scanf("%d", &e[a].building_nb);
            
            printf("Enter ZIP code: ");
            scanf("%d", &e[a].zip);
            
            printf("Enter city: ");
            scanf("%s", e[a].city);
    
            printf("Enter country: ");
            scanf("%s", e[a].country);
        }
    printf("Displaying Information:\n");
    qsort(e, n, sizeof(struct employee), comparator);
    for(a=0;a<n;a++)
        {
            printf("\n Employee no.%d info:\n",a+1);
            printf("\t Street:%s\n ",e[a].street);
            printf("\t Building number: %d\n",e[a].building_nb);
            printf("\t ZIP Code: %d\n",e[a].zip);
            printf("\t City:%s\n ",e[a].city);
            printf("\t Country:%s\n\n",e[a].country);
        }
    
    for (a = 0;a<n;a++)
        {
            if ((e[a].city == " ")||(e[a].city == "," && e[a+1].city == " ")||(e[a].city == "."))
                space++;
        }
    int count = 0;
    int c = 0;

    int i = 0;
    int j = 0;
    int k = 0;
    int spaceCnt = 0;

    char str1[64];

    char p[20][50];
    char ptr1[20][50];

    char* ptr;

    for (; i < strlen(e[100].city); i++) {
        if ((e[i].city == " ") || (e[i].city == "," && e[i+1].city == " ") || (e[i].city == ".")) {
            spaceCnt = spaceCnt + 1;
        }
    }

    for (i = 0; j < strlen(e[100].city); j++) {
        if ((e[j].city == " ") || (e[j].city == ",") || (e[j].city == ".")) {
            p[i][k] = '\0';
            i++;
            k = 0;
        }
        else
            p[i][k++] = e[j].city; //Error line
          //  strcpy(p[i][k++], e[j].city); // I tried to fix it by using strcpy,but i got the same error 
    }

    k = 0;
    i = 0;
    for (; i <= spaceCnt; i++) {
        for (j = 0; j <= spaceCnt; j++) {
            if (i == j) {
                strcpy(ptr1[k], p[i]);
                k = k + 1;
                count = count + 1;
                break;
            }
            else {
                if (strcmp(ptr1[j], p[i]) != 0)
                    continue;
                else
                    break;
            }
        }
    }

    for (i = 0; i < count; i++) {
        for (j = 0; j <= spaceCnt; j++) {
            if (strcmp(ptr1[i], p[j]) == 0)
                c = c + 1;
        }
        printf("Frequency of '%s' is: %d\n", ptr1[i], c);
        c = 0;
    return 0;
}
}
Posted
Updated 29-May-22 8:28am
v2

First off, indent your code properly: You have mixed styles in there:
struct employee{
    char street[50],country[50],city[50];
    int building_nb, zip;
} e[100];

int comparator(const void* p, const void* q)
    {
        return strcmp(((struct employee*)p)->country,((struct employee*)q)->country);
    }

for (a = 0;a<n;a++)
    {
        if ((e[a].city == " ")||(e[a].city == "," && e[a+1].city == " ")||(e[a].city == "."))
            space++;
    }

for (; i < strlen(e[100].city); i++) {
    if ((e[i].city == " ") || (e[i].city == "," && e[i+1].city == " ") || (e[i].city == ".")) {
        spaceCnt = spaceCnt + 1;
    }
}

which makes it hard to work out what the heck is going on, particularly when it all ends up with this:
    return 0;
}
}
Either that return is in the wrong place, or you have something rather mixed up in your code.

And it make sit look like you found unrelated bits of code on the internet and threw them together in the hope it would all work...

Pick one style, and stick to it. It makes your code more readable, and laso reliable because you can see what is going on properly...

Now look at the error line:
p[i][k++] = e[j].city; //Error line

Look up what the various bits are declared as:
char p[20][50];

struct employee e[100];

struct employee{
    char street[50],country[50],city[50];
    int building_nb, zip;
}
So city is an array of char values - a char* - and p[x][y] is a char
You are trying to put a pointer value into a char location and it is rightly telling you that's a bad idea.

Now, your code is confusing and difficult to read, so I have no idea what p is there for, or what you are actually trying to do. Using one character names for everything makes it easy for you to type, but it makes it really hard to work out what they are supposed to be used for or what the heck you are actually trying to do!

But I'd probably want to either copy the city string into the p[x] string, copy the city pointer into the p[x] pointer, or copy each character of the city string into each character of the p[x][y] locations.
 
Share this answer
 
Comments
CPallini 29-May-22 13:48pm    
5.
Clean up the code and try to understand it. You must learn about struct and using the debugger. Read some chapters about it in the Learn C++ tutoria.

I dont believe that you are stuck about that code for a week, because writing that code means to understand it. So I guess you havent wrote that but copied and changed some of it.

C++
strlen(e[100].city)
looks very strange. Using the string length of 100th city is nonsense (WTF???).
You better redesign it as class and write comments what you are doing.
Start with better names:
C++
// struct employee e[100];
struct employee employees[100];
 
Share this answer
 
Comments
CPallini 29-May-22 13:49pm    
5.
The following line doesn't even compile, for good reason.
C
p[i][k++] = e[j].city; //Error line

The comment states that a string is actually to be copied.
C
// strcpy(p[i][k++], e[j].city); // I tried to fix it by using strcpy,but i got the same error 
char p[20][50];

The employee variable is declared twice, once locally and once globally - that is nonsense and also dangerous.
It would probably be a good idea to improve the data type declaration first:
C
typedef struct {
	char street[50], country[50], city[50];
	int building_nb, zip;
} employee;

Then the declaration of e in main():
C
employee e[100];

If you now want to copy the up to 100 cities with a length of up to 50 characters into the array p should p also be large enough:
C
#define NUME sizeof(e)/sizeof(e[0])
#define NUMC sizeof(e[0].city)
    
// char p[20][50];
char p[NUME][NUMC];

If the compiler allows it, you can use n here instead of NUME.

A single space is encoded with ' ' and not with " ".
Two loops are needed to compare all characters in all cities.
Example:
C
for (i = 0; i < n; i++) {
  for (j = 0; j < strlen(e[i].city)+1; j++) {
    if ((e[i].city[j] == ' ') || (e[i].city[j] == ',') || (e[i].city[j] == '.'))
       p[i][j] = (char)0;
    else
       p[i][j] = e[i].city[j];
    }
}

There's a lot more, but that should be enough to solve the original problem for now.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900