Program crashing

This is a discussion on Program crashing within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> int keuze1; int keuze2; int keuze3; char pad[200]; char naam[50]; char padnieuw[200]; char naamnieuw[50]; FILE ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    Program crashing

    Code:
       
    #include <stdio.h>
    #include <string.h>
    
    int keuze1;
    int keuze2;
    int keuze3;
    char pad[200];
    char naam[50];
    char padnieuw[200];
    char naamnieuw[50];
    FILE *fp;
    int regelteller;
    
    struct b
    {
       char naamvader[20];
       char naammoeder[20];      
    };   
    
    struct a
    {
       char voornaam[5];
       struct b;
       int leeftijd;
       float jaarsalaris;
    };
    
    
    int werkinbestand (void)
    {
       a a;
       b b;
    
       printf("\n\n\n\t[1] Add record to existing file.");
       printf("\n\t[2] Change recording in existing file.");
       printf("\n\t[3] Delete a record from an existing file.");
       printf("\n\t[4] Show existing recorsd for a file.");
       printf("\n\t[5] Create a new file");
       printf("\n\t[6] Close current file and open a new one");
       printf("\n\t[7] End program");
    
       printf("\n\n\tGive your choice: ");
       scanf("%d",& keuze2);
    
        switch(keuze2)
          {
             case 1:
                fp = fopen(pad, "a+");
                if (fp == NULL) 
                   printf("\n\tKan bestand niet openen.");
                else
                   {
                   printf("First:\n");
                   scanf("%s", a.voornaam);
                   printf("\nAnother string\n");
                   scanf("%s", b.naamvader);
                   printf("\nstring:\n");
                   scanf("%s", b.naammoeder);
                   printf("\nGive age:\n");
                   scanf("%d", a.leeftijd);
                   printf("\nfloat:\n");
                   scanf("%f", a.jaarsalaris);
                   fprintf(fp, "\n%s 1| %s 2| %s 3| %d 4| %f", a.voornaam, b.naamvader, b.naammoeder, a.leeftijd, a.jaarsalaris);               
                   fclose(fp);
                   }
                break;
                
             case 2:
                printf("case 2");
                break;
                
             case 3:
                printf("case 3");
                break;
                
             case 4:
                printf("case 4");
                break;
                
             case 5:
                printf("case 5");
                break;
                
             case 6:
                printf("case 6");
                break;
                
             case 7:
                printf("case 7");
                break;
         }
    }
    
    int werkinnieuwbestand (void)
    {
       
       printf("\n\n\n\t[1] Voeg een record toe.");
       printf("\n\t[2] Creeer een nieuw bestand.");
       printf("\n\t[3] Sluit het huidige bestand en selecteer ander (bestaand) bestand.");
       printf("\n\t[4] Einde programma");
          
       printf("\n\n\tGeef het nummer van uw keuze: ");
       scanf("%d",& keuze3);
    
        switch(keuze3)
          {
             case 1:
                printf("case 1");
                break;
                
             case 2:
                printf("case 2");
                break;
                
             case 3:
                printf("case 3");
                break;   
                                     
             case 4:
                printf("case 4");
                break;
         }
    }
    
    int main (void)
    {
       printf("\n\n\n\t[1] Do you want to work in an existing file?");
       printf("\n\t[2] Do you want to record a new file?");
    
       printf("\n\n\tNumber of choice: ");
       scanf("%d",& keuze1);
    
          switch(keuze1)
             {
                case 1:
                   printf("\n\tGive path of file:\n\n\t");
                   scanf("%s",& pad);
                   printf("\n\tName of file:\n\n\t");
                   scanf("%s",& naam);
                   strncat(pad, naam, 250);
                   werkinbestand();
                   break;
    
                case 2:
                   printf("\n\tGeef het pad van het bestand in:\n\n\t");
                   scanf("%s",& pad);
                   printf("\n\tGeef de naam van het bestand in:\n\n\t");
                   scanf("%s",& naam);
                   strncat(pad, naam, 250);
                   break;
             }
       getchar();
       getchar();
    }
    What i am trying to do is to create a simple database
    I want to collect records of 3 strings, one integer and a float (the assigntment says I have to use a struct in a struct so it looks kinda odd right now)

    The program crashes after trying to add a record to an existing file

    (Option 1 in the menu, path and filename filled in, option 1 in the second menu)

    could someone tell me what i am doing wrong?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Well, you're not posting the code that crashes. The code you posted doesn't even compile:
    Code:
    $ gcc -Wall database.c
    database.c:23: warning: declaration does not declare anything
    database.c: In function ‘werkinbestand’:
    database.c:31: error: ‘a’ undeclared (first use in this function)
    database.c:31: error: (Each undeclared identifier is reported only once
    database.c:31: error: for each function it appears in.)
    database.c:31: error: expected ‘;’ before ‘a’
    database.c:32: error: ‘b’ undeclared (first use in this function)
    database.c:32: error: expected ‘;’ before ‘b’
    database.c: In function ‘main’:
    database.c:137: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[200]’
    database.c:139: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[50]’
    database.c:146: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[200]’
    database.c:148: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[50]’
    Line 23: 'struct b' is the type, you need a variable name, like 'struct b foo'.
    Lines 31 and 32: 'a' by itself is not a valid type, you need 'struct a'. Then, you need to name the variable something other than 'a', like 'foo'. Same for b.
    Lines 137, 139, 146 and 148: Drop the &. The name of the array serves as a pointer to the first element.

    Also, you correctly declared main to return an int, but you don't return one. Put return 0; at the end of main.

    Lastly, read this: http://www.c2.com/cgi/wiki?GlobalVariablesAreBad. Move those global variables into main and pass them to your functions as needed.

    Fix all that, and come back with code that compiles, and we'll help you debug your run-time issues.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Despite all appearances of a C program, it actually needs C++ to compile it.
    a and b are declared as structs, but to actually USE them in a C program, one has to say

    struct a a;
    struct b b;

    But the warnings (actually, errors) need to be fixed all the same.
    Code:
    $ g++ -Wall  bar.c
    bar.c: In function ‘int werkinbestand()’:
    bar.c:60: warning: format ‘%d’ expects type ‘int*’, but argument 2 has type ‘int’
    bar.c:62: warning: format ‘%f’ expects type ‘float*’, but argument 2 has type ‘double’
    bar.c:92: warning: no return statement in function returning non-void
    bar.c: In function ‘int werkinnieuwbestand()’:
    bar.c:123: warning: no return statement in function returning non-void
    bar.c: In function ‘int main()’:
    bar.c:137: warning: format ‘%s’ expects type ‘char*’, but argument 2 has type ‘char (*)[200]’
    bar.c:139: warning: format ‘%s’ expects type ‘char*’, but argument 2 has type ‘char (*)[50]’
    bar.c:146: warning: format ‘%s’ expects type ‘char*’, but argument 2 has type ‘char (*)[200]’
    bar.c:148: warning: format ‘%s’ expects type ‘char*’, but argument 2 has type ‘char (*)[50]’
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int keuze1;
    int keuze2;
    int keuze3;
    char pad[200];
    char naam[50];
    char padnieuw[200];
    char naamnieuw[50];
    FILE *fp;
    int regelteller;
    
    typedef struct ouderss
        {    
        char naamvader[20];
        char naammoeder[20]; 
        } ouder;
    
    typedef struct
        {
            char voornaam[5];
            struct ouderss ouders;
            int leeftijd;
            float jaarsalaris;
        } info;
    
    
    int werkinbestand (void)
    {
       info record;
       ouder inin;
    
       printf("\n\n\n\t[1] Add record to existing file.");
       printf("\n\t[2] Change recording in existing file.");
       printf("\n\t[3] Delete a record from an existing file.");
       printf("\n\t[4] Show existing recorsd for a file.");
       printf("\n\t[5] Create a new file");
       printf("\n\t[6] Close current file and open a new one");
       printf("\n\t[7] End program");
    
       printf("\n\n\tGive your choice: ");
       scanf("%d",& keuze2);
    
        switch(keuze2)
          {
             case 1:
                fp = fopen(pad, "a+");
                if (fp == NULL) 
                   printf("\n\tKan bestand niet openen.");
                else
                   {
                   printf("First:\n");
                   scanf("%s", record.voornaam);
                   printf("\nAnother string\n");
                   scanf("%s", inin.naamvader);
                   printf("\nstring:\n");
                   scanf("%s", inin.naammoeder);
                   printf("\nGive age:\n");
                   scanf("%d", record.leeftijd);
                   printf("\nfloat:\n");
                   scanf("%f",record.jaarsalaris);
                   fprintf(fp, "\n%s 1| %s 2| %s 3| %d 4| %f", record.voornaam, inin.naamvader, inin.naammoeder, record.leeftijd, record.jaarsalaris);               
                   fclose(fp);
                   }
                break;
                
             case 2:
                printf("case 2");
                break;
                
             case 3:
                printf("case 3");
                break;
                
             case 4:
                printf("case 4");
                break;
                
             case 5:
                printf("case 5");
                break;
                
             case 6:
                printf("case 6");
                break;
                
             case 7:
                printf("case 7");
                break;
         }
    }
    
    int werkinnieuwbestand (void)
    {
       
       printf("\n\n\n\t[1] Voeg een record toe.");
       printf("\n\t[2] Creeer een nieuw bestand.");
       printf("\n\t[3] Sluit het huidige bestand en selecteer ander (bestaand) bestand.");
       printf("\n\t[4] Einde programma");
          
       printf("\n\n\tGeef het nummer van uw keuze: ");
       scanf("%d",& keuze3);
    
        switch(keuze3)
          {
             case 1:
                printf("case 1");
                break;
                
             case 2:
                printf("case 2");
                break;
                
             case 3:
                printf("case 3");
                break;   
                                     
             case 4:
                printf("case 4");
                break;
         }
    }
    
    int main (void)
    {
       printf("\n\n\n\t[1] Do you want to work in an existing file?");
       printf("\n\t[2] Do you want to create a new file?");
    
       printf("\n\n\tNumber of choice: ");
       scanf("%d",& keuze1);
    
          switch(keuze1)
             {
                case 1:
                   printf("\n\tGive path of file:\n\n\t");
                   scanf("%s", pad);
                   printf("\n\tName of file:\n\n\t");
                   scanf("%s", naam);
                   strncat(pad, naam, 250);
                   werkinbestand();
                   break;
    
                case 2:
                   printf("\n\tGeef het pad van het bestand in:\n\n\t");
                   scanf("%s", pad);
                   printf("\n\tGeef de naam van het bestand in:\n\n\t");
                   scanf("%s", naam);
                   strncat(pad, naam, 250);
                   break;
             }
       getchar();
       getchar();
       return 0;
    }
    this code compiles perfectly but when running, it crashes before I can enter the float.

    also, I dont really understand what you guys mean
    im farely new to coding so if you guys would take a few more seconds describing, it would be a great help

    im using the dev c++

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Your code still doesn't compile perfectly. You need to turn the warnings all the way up on your compiler. Actually, since Dev-C++ is no longer supported or maintained, I would recommend switching compilers. Pelles C is great if you only write C. If you want C++ too, you can look into Code::Blocks (an IDE) which comes with MinGW (GCC port to Windows) by default. Whichever compiler you end up using, learn to enable all warnings, and always make sure you correct each one. Here's what I get when I compile.
    Code:
    $ gcc -Wall -o db db.c
    db.c: In function ‘werkinbestand’:
    db.c:60: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    db.c:62: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double’
    db.c: In function ‘werkinnieuwbestand’:
    db.c:123: warning: control reaches end of non-void function
    db.c: In function ‘werkinbestand’:
    db.c:92: warning: control reaches end of non-void function
    Lines 60 and 62: You need the address of those variables. Put an & in front of them.
    Line 123 and 92: those functions say they return an int. You need to return one, otherwise you will return garbage off the stack and possibly crash your program.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,427
    Quote Originally Posted by krinl View Post
    also, I dont really understand what you guys mean
    im farely new to coding so if you guys would take a few more seconds describing, it would be a great help
    You need to tell us exactly what it is you don't understand and what you need more explanation of.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. crashing program, help
    By xniinja in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 11:42 AM
  2. Program crashing when I use IO
    By legit in forum C++ Programming
    Replies: 9
    Last Post: 05-31-2009, 07:54 AM
  3. Program crashing, Need help!
    By Obsidian_179 in forum C Programming
    Replies: 3
    Last Post: 06-19-2008, 05:26 PM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. help plz crashing program
    By mill1000 in forum C++ Programming
    Replies: 3
    Last Post: 08-23-2002, 09:15 AM

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