Thread: please help me with c code!!!

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Unhappy please help me with c code!!!

    Hey, im new to this forum , and just new to c language. Im trying to do simple program for like 6 hours!!! and its just not able too work, please help me if you can:

    Code:
    struct date {
    	int day;
    	int month;
    	int year;
    }now;
    struct det{
    	char name[32];
    	struct date b;
    	int salary;
    };
    struct det worker [5]={{"shosho",12,12,1937,16500},{"mosho",11,11,1950,2500},{"dosho",10,10,1922,5467},{"fosho",9,9,1967,3222},{"sosho",8,8,2008,12342345}};
    
    void main ()
    {
    	char c[32];
    	int i,hef=0,d=0;
    	now.day=10;
    	now.month=5;
    	now.year=2011;
    	puts ("PUT NAME OF THE WORKER");
    	gets (c);
    	for (i=0;i<5;i++)
    	{
    		hef=((now.year*10000+now.month*100+now.day)-(worker[i].b.year*10000+worker[i].b.month*100+worker[i].b.day)/10000);
    
    		if (worker[i].name==c)
    		{
    			if (hef>= 67)
    		puts("YOU GOT IT");
    			else
    				puts("come later");
    		}
    		else
    		{
    			d++;
    			if (d==5)
    				puts("no name like this");
    		}
    	}
    }

    the problem is that when i write an existing name (like mosho) it should chek his age and if its more than 67 print something, if its less print another thing, but the program cannot rekognize existing name :S

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You can't compare strings with the == you need to use strcmp(). Check the manual page for it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also void main() should be int main(void) and should return 0 on successful execution.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First you've got to get more than 1 character for the worker's name...
    Second C can't do this --> if (worker[i].name==c) It doesn't know how.

    Hints: Look up fgets() and strcmp().

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Oh wow... a three way collision with only two people...

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    2
    Oh tnx alot , it finally worked, after i puted if (strcmp(worker[i].name,c)==0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread