Problems with functions and structures. Help neded!

This is a discussion on Problems with functions and structures. Help neded! within the C Programming forums, part of the General Programming Boards category; sory if this was before but I realy can't find the answer... Code: #include <stdio.h>#include <stdlib.h> #include <string.h> #include <conio.h> ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    Unhappy Problems with functions and structures. Help neded!

    sory if this was before but I realy can't find the answer...

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    
    typedef struct
    { int day, month, year; } DATE;
    
    
    
    
    typedef struct
    {
    	DATA depdate;
    	char name;
    	int place;
    	char class;
    	int childrens;
    	} passager;
    
    
    
    
    void menu(void);
    void datentry (char *, int);
    
    
    
    
    int main()
    {
    
    
      clrscr();
    char name[40];
    char class[15];
    int n;
    passager *p=NULL;  //warning: 'p' is assigned a value that is never used
    
    
     menu();
     return 0;
    }
    
    
    
    
    
    
    void menu()
    {  passager *p; int n;
        int choice;
    
    
        do
        {
    	printf("\tMenu\n\n");
    	printf("1. data entry\n");
    	printf("2. Show passagers\n");
    	printf("3. Modify data\n");
    	printf("4. Search passager\n");
    	printf("5. Passagers sort\n");
    	printf("6. Add a passager\n");
    	printf("7. Insert a passager\n");
    	printf("8. Delete a passager\n");
    	printf("9. Save all data in a file\n");
    	printf("10. Read data from file\n");
    	printf("11. Stats\n");
    	printf("12. Exit\n");
    	scanf("%d",&choice);
    
    
    	switch(choice)
    	{
    	    case 1: datentry(name,place);   //here i have a problem with function parameters
    		break;
    	    case 2: showpass();
    		break;
    	// ......
    	    case 12: printf("Quiting...\n");
    	    getch();
    		exit(0);
    		break;
    	    default: printf("Wrong choice!\n");
    		break;
    	}
    
    
        } while (choice != 12);
    
    
    }
    void datentry(passager *p, int n) // here i'm sure that it's wrong but i don't 
    {  				know how it should be...
    	int i,k;
    puts("Enter the number of passagers(<100): \n");
    scanf("%d",&k);
    for(i=0; i<k; i++)
    {
    	printf("\t %d passager\n",i+1);
    	printf("Passager name: ");
    	fflush(stdin);
    	gets((*p).name);	//Eror: Undefinied symbol 'name' and Cannot convert 'int' to 'char' and Type mismatch '_s' in call to gets(char *)'
    	printf("Passager's place: ");
    	scanf("%d", &(*p).place);   //Eror: Undefinied symbol 'place'
    	printf("Passager's class(Business/Econom): ");
    	gets(class);                 //Eror: Expresion syntax
    	printf("Numaber o childrens: ");
    	scanf("%d" &(*p).childrens);   //Eror: Illegal use if pointer
    	printf("Departure date(DD MM YY): ");
    	scanf("%d %d %d",&(*p).depdate->day,&(*p).depdate->month, &(*p).depdate.year);  //and here some erors...
    }
    }
    
    i wroted erors in comments
    Last edited by paradokss; 12-13-2012 at 09:09 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    We aren't going to spend longer on this than you are.
    Tell us the purpose of the program.
    Tell us what is going wrong.
    Post error messages.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by DeadPlanet View Post
    We aren't going to spend longer on this than you are.
    Tell us the purpose of the program.
    Tell us what is going wrong.
    Post error messages.
    it's somethink like a data base of a air company... and i'm new to the programming and i readed some books but can't realy get it with this functions and pointers

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Hmm I'm sorry I didn't actually see that you'd put the error messages in as comments :P

  5. #5
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    It's certainly not a bad attempt!

    void datentry (char *, int);
    void datentry(passager *p, int n)

    Your function definition and declaration are different, they shouldn't be.

    datentry(name,place); <-- where have you defined the variable 'name'?

    fflush(stdin); don't use this
    FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    gets((*p).name); don't use this
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    There are a few more errors but they seem like a good starting point.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by DeadPlanet View Post
    It's certainly not a bad attempt!

    void datentry (char *, int);
    void datentry(passager *p, int n)

    Your function definition and declaration are different, they shouldn't be.

    datentry(name,place); <-- where have you defined the variable 'name'?
    name and place are defined in main...so i want function to use those strings...or best will be that function will store the name and others in struct "passager" and the passager in main declared vectors...

    should i declare name and place in main or in structure?

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by paradokss View Post
    name and place are defined in main...
    The code you have shown us only declares "name" in main(), but there is no "place".
    Declaring names inside a function like main() makes them local to that function, which means they aren't visible in other functions.

    Quote Originally Posted by paradokss View Post
    it's somethink like a data base of a air company... and i'm new to the programming and i readed some books but can't realy get it with this functions and pointers
    When did you start learning C?

    Reading books is not enough. You also have to understand the contents.
    I suggest practicing some simple examples using functions and structures before continuing with your program.

    For example, you could try to write a little program which stores different kind of values into a structure (ints, doubles, strings, ...) and pass this structure to a function which prints out the contents of each member.
    The next step would be to change the function so that you can pass a pointer to the structure.

    If you have problems with these tasks I'm afraid you have to go back studying your books or for example the tutorials on this site.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and structures in C
    By Kurse in forum C Programming
    Replies: 7
    Last Post: 08-10-2012, 02:17 AM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 12:24 AM
  3. help me with structures using functions
    By magnetpest2k7 in forum C Programming
    Replies: 2
    Last Post: 10-07-2009, 01:32 AM
  4. functions and structures
    By subflood in forum C Programming
    Replies: 2
    Last Post: 06-11-2005, 06:15 PM
  5. Structures and Functions
    By Extol in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2003, 07:28 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21