473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Incompatible types in assignment

22
Expand|Select|Wrap|Line Numbers
  1. /*
  2.      File Name:  lab7.c
  3.      Author:  Nick Cantey
  4.      Description:  This application reads a binary file of employee information
  5.                    sorts by last name and prints the sorted information to  
  6.                    to the screen in a table.
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. typedef struct
  14. {
  15.      char lname[25];
  16.      char fname[25];
  17.      int age;
  18.      double salary;
  19. } employee_t;
  20.  
  21. void sort(employee_t*, int);
  22.  
  23. int main ()
  24. {
  25.  
  26. FILE* in;
  27. employee_t employees[10];
  28. int i, n, j, k;
  29. double temp;
  30.  
  31.  
  32. in = fopen("lab7.in", "rb");
  33. i=0;  
  34. while( fread(&employees[i], sizeof(employee_t), 1, in ) == 1)
  35. {
  36. i++;
  37. }
  38. fclose(in);
  39. n=i;
  40. printf("%d\n", n);
  41. printf("%s %s\n", employees[0].lname, employees[0].fname);
  42. printf("%s\n", employees[1].lname);
  43.  
  44.  
  45.  
  46.  
  47. sort(employees, n);
  48. printf("%s %s\n", employees[0].lname, employees[0].fname);
  49.  
  50. }
  51.  
  52. /* Function Name:  sort
  53.    Parameters:  &employees(address to array) and int(number of elements in array
  54.    Description:  This function sorts employees in ascending order by last name
  55. */
  56.  
  57. void sort(employee_t* e, int n)
  58. {
  59. int temp, min, current, j;
  60.  
  61. for(current=0; current<n-1; current++)
  62.    {
  63.    min=current;
  64.       for(j=current; j<n; j++)
  65.       {
  66.       if(strcmp(e[j].lname, e[min].lname) < 0) 
  67.         temp=e[min];
  68.         e[min]=e[current];
  69.         e[current]=temp;
  70.       }
  71.    } 
  72. }
  73.  
Here is my question/problem. In my "sort function" I am having issues with the "temp=e[min];" line and "e[current]=temp" line. The error I get when compiling this program is "Incompatible types in assignment" for those two lines. If anyone could help me with this, that would be great. Thank you in advance.
Apr 28 '07 #1
5 8996
JosAH
11,448 Expert 8TB
Here is my question/problem. In my "sort function" I am having issues with the "temp=e[min];" line and "e[current]=temp" line. The error I get when compiling this program is "Incompatible types in assignment" for those two lines. If anyone could help me with this, that would be great. Thank you in advance.
'e' is a pointer to an employee_t so e[min] has type employee_t; temp has int
type, hence the error diagnostic. Better make temp an employee_t too.

kind regards,

Jos
Apr 28 '07 #2
canteyn
22
thanks that helped, however I am now receiving weird characters on from the "printf()" statements I am using to check to make sure everything is getting sorted correctly. If you wouldn't mind compiling the code and seeing if you might be able to catch the mistakes, that would be fantastic.

Expand|Select|Wrap|Line Numbers
  1. /*
  2.      File Name:  lab7.c
  3.      Author:  Nick Cantey
  4.      Description:  This application reads a binary file of employee information
  5.                    sorts by last name and prints the sorted information to  
  6.                    to the screen in a table.
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. typedef struct
  14. {
  15.      char lname[25];
  16.      char fname[25];
  17.      int age;
  18.      double salary;
  19. } employee_t;
  20.  
  21. void sort(employee_t*, int);
  22.  
  23. int main ()
  24. {
  25.  
  26. FILE* in;
  27. employee_t employees[10];
  28. int i, n, j, k;
  29. double temp;
  30.  
  31.  
  32. in = fopen("lab7.in", "rb");
  33. i=0;  
  34. while( fread(&employees[i], sizeof(employee_t), 1, in ) == 1)
  35. {
  36. i++;
  37. }
  38. fclose(in);
  39. n=i;
  40. printf("%d\n", n);
  41. printf("%s %s\n", employees[0].lname, employees[0].fname);
  42. printf("%s\n", employees[1].lname);
  43.  
  44.  
  45.  
  46.  
  47. sort(employees, n);
  48. printf("%s %s\n", employees[0].lname, employees[0].fname);
  49.  
  50. }
  51.  
  52. /* Function Name:  sort
  53.    Parameters:  &employees(address to array) and int(number of elements in array
  54.    Description:  This function sorts employees in ascending order by last name
  55. */
  56.  
  57. void sort(employee_t* e, int n)
  58. {
  59. int min, current, j;
  60. employee_t temp;
  61.  
  62. for(current=0; current<n-1; current++)
  63.    {
  64.    min=current;
  65.       for(j=current; j<n; j++)
  66.       {
  67.       if(strcmp(e[j].lname, e[min].lname) < 0) 
  68.         e[min]=temp;
  69.         e[min]=e[current];
  70.         e[current]=temp;
  71.       }
  72.    }
  73. printf("%s %s \n", e[0].lname, e[1].lname);
  74. }
  75.  
  76.  
Here is what the out put looks like:
2
Smith John
Cantey
ÿ4 Cantey
ÿ4


those y's and 4's should not be there at all, which leads me to believe that what would be the lname string has now been replaced with something, please take a look at my sort and see if it is working correctly.
Apr 28 '07 #3
JosAH
11,448 Expert 8TB
If you *don't* sort those records does the output show correctly then? If so,
it was the sorting that corrupted the data, if not you incorrectly read the
data from the file or the file already contained corrupted data.

kind regards,

Jos
Apr 28 '07 #4
canteyn
22
If you *don't* sort those records does the output show correctly then? If so,
it was the sorting that corrupted the data, if not you incorrectly read the
data from the file or the file already contained corrupted data.

kind regards,

Jos
Yes before I sort the records the data is shown correctly, so it is in my sorting that the data gets corrupted, but I don't have the slightest idea how to correct that. Any suggestions?
Apr 28 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Shouldn't this code in your sort be looked at carefully?
Expand|Select|Wrap|Line Numbers
  1. e[min]=temp;
  2. e[min]=e[current];
  3.  
Apr 29 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Brian Stubblefield | last post by:
Dear clc members, I am new to C and am posting several messages concerning a large C program that I am debugging. I am encountering a "incompatible types in assignment" warning for the...
2
by: Dennis Schulz | last post by:
Hi all, the following programm is supposed to check for login and password with inbuilt linux functions. in line 57 and 64 there are erorrs: incompatible types in asignment. whats wrong with...
10
by: gk245 | last post by:
I have something like this: #include <stdio.h> main () { struct line { char write; char read;
8
by: fei.liu | last post by:
I have the following source code. It seems wierd to me why gca's value cannot be reassigned. It's afterall a pointer and has a pointer value. I am aware that the standard says it's not allowed. But...
13
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
1
by: pyo2004 | last post by:
Here's the definition of work_struct struct work_struct { unsigned long pending; struct list_head entry; void (*func)(void *); void *data; void *wq_data; struct timer_list timer; };
6
by: billiabon | last post by:
Hello,everybody! I need your help! My problem is this assignment char pnm; void enqueue(int pd,char pnm,int pnum,int ptm,struct queue *z) {struct node *p; p=(struct node...
4
by: pandaemonium | last post by:
I have two quick problems with the code I am writing: First, I define a struct of char arrays (strings) and then try accessing the same. I get an "incompatible types in assignment" error: struct...
2
by: kardon33 | last post by:
I am getting a "error: incompatible types in assignment" error and cant figure out why? I am trying to set lastRow to row at the end of the code snippet. int row; int lastRow; size_t...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.