Thread: Program keeps crashing

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    29

    Program keeps crashing

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    typedef struct
    {
            char studentname[50];
            int studentid;
    }       database;
    
    int main(int argc, char *argv[])
    {
        database school;
        FILE *file_ptr;
        char text[50];
        int temp;
      
        while(1)
        {
        printf("\nEnter Student Name: ");
        scanf("%s", &school.studentname);
        if(school.studentname == 0)
                          {
                              break;
                          }
                               
        printf("\nEnter Student ID: ");
        scanf("%s", &school.studentid);
        
        printf("\nHere is your input Data: %s, %s\n", school.studentname , school.studentid);
        
        printf("\nAdd to school.csv ?(y/n) : ");
        getchar();
        scanf("%c", &temp);
                    
                    if(temp == 'y' || temp == 'Y')
                            {
                                   file_ptr = fopen("school.csv" , "a");
                                                printf("\nFile school.csv opened\n");
                                                fprintf(file_ptr, "%s, %s\n", school.studentname , school.studentid);
                                                fclose(file_ptr);
                                                printf("File Saved.\n");
                                            }
                                   
                          
                    else
                              {
                              printf("\nFile not Saved.");   
                                                }
    
                                                }
      
      
      
      
      system("PAUSE");	
      return 0;
    }
    Gets Name
    Gets ID
    Output Name, ID
    Prompt to save
    if y or Y, save to school.csv
    loop until 0

    but its crashing when I input ID (id are typically D0124331)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > scanf("%s", &school.studentid);
    Why are you reading a string into an int?
    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.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    How should I change it then

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Reading how scanf works, and picking a suitable data type for your student ID.
    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.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Charak View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    typedef struct
    {
            char studentname[50];
            int studentid;
    }       database;
    
    int main(int argc, char *argv[])
    {
        database school;
        FILE *file_ptr;
        char text[50];
        int temp;
      
        while(1)
        {
        printf("\nEnter Student Name: ");
        scanf("%s", &school.studentname);
        if(school.studentname == 0)
                          {
                              break;
                          }
                               
        printf("\nEnter Student ID: ");
        scanf("%s", &school.studentid);
        
        printf("\nHere is your input Data: %s, %s\n", school.studentname , school.studentid);
        
        printf("\nAdd to school.csv ?(y/n) : ");
        getchar();
        scanf("%c", &temp);
                    
                    if(temp == 'y' || temp == 'Y')
                            {
                                   file_ptr = fopen("school.csv" , "a");
                                                printf("\nFile school.csv opened\n");
                                                fprintf(file_ptr, "%s, %s\n", school.studentname , school.studentid);
                                                fclose(file_ptr);
                                                printf("File Saved.\n");
                                            }
                                   
                          
                    else
                              {
                              printf("\nFile not Saved.");   
                                                }
    
                                                }
      
      
      
      
      system("PAUSE");	
      return 0;
    }
    Gets Name
    Gets ID
    Output Name, ID
    Prompt to save
    if y or Y, save to school.csv
    loop until 0

    but its crashing when I input ID (id are typically D0124331)

    Well there's a few issues here...
    1) you can't test ... if (string == 0) ... and expect a useable result.
    2) studentid is an integer not a string.
    3) "here is your input data" is unnecessary... people know what they entered.
    4) You are not verifying that your file actually opened before writing to it.
    5) Your fprintf() formatting statement has errors.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    29
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct
    {
            char studentName[50];
            int studentId;
    }       database;
    
    int main(int argc, char *argv[])
    {
        database school;
        FILE *file_ptr;
        char text[50];
        int data,temp;
        
        while(1)
        {
                printf("\nEnter Student Name: ");
                gets(school.studentName);
    
                printf("\nEnter Student ID: ");
                scanf("%d", &school.studentId);
                if(school.studentId == 0)          
                {
                 break;
                }
                               
                               printf("\nAdd to school.csv? (y/n): ");
                               getchar();
                               scanf("%c", &temp);
                               
                                           if(temp == 'y' || temp == 'Y')
                                           {
                                                   
                                            file_ptr = fopen("school.csv" , "a");
                                                printf("\nFile school.csv opened\n");
                                                fprintf(file_ptr, "%s, %d\n", school.studentName , school.studentId);
                                                fclose(file_ptr);
                                                printf("File Saved.\n");
                                            }
                            
                          
                                            else(temp == 'n' || temp == 'N');
                                            {
                                            printf("\nFile not Saved.");   
                                            }
                          }
    
      system("PAUSE");	
      return 0;
    }
    Fixed but how do i fix my fprint?

    How do I fix this line so I can use a Space (ie. Bobby Bob)
    Code:
                printf("\nEnter Student Name: ");
                scanf("%s", &school.studentName);
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    typedef struct
    {
            char studentName[50];
            int studentId;
    }       database;
    
    int main(int argc, char *argv[])
    {
        database school;
        FILE *file_ptr;
        char text[50];
        int begin,temp;
        
        while(1)
        {
        printf("\nInput Data? (Yes = 1) : ");
        scanf("%d", &begin);
        if(begin != 1)          
        {
        break;
        }
                printf("\nEnter Student Name: ");
                fgets(school.studentName, 50, stdin);
                
                printf("\nEnter Student ID: ");
                scanf("%d", &school.studentId);
                      
                               printf("\nAdd to school.csv? (y/n): ");
                               getchar();
                               scanf("%c", &temp);
                               
                                           if(temp == 'y' || temp == 'Y')
                                           {
                                                   
                                            file_ptr = fopen("school.csv" , "a");
                                            if( file_ptr != NULL)
                                            {
                                                printf("File school.csv opened\n");
                                            }
                                                //printf("\nFile school.csv opened\n");
                                                fprintf(file_ptr, "%[^ ]s, %d\n", school.studentName , school.studentId);
                                                fclose(file_ptr);
                                                printf("File Saved.\n");
                                            }
                            
                          
                                            else(temp == 'n' || temp == 'N');
                                            {
                                            printf("\nFile not Saved.\n");   
                                            }
                          }
    
      system("PAUSE");	
      return 1;
    }
    Problems:

    1. Skipping Student Name Input
    2. Fprintf not saving to file
    Last edited by Charak; 03-23-2011 at 07:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing b/c of malloc, can someone please help?
    By matthayzon89 in forum C Programming
    Replies: 2
    Last Post: 11-14-2010, 02:53 PM
  2. Replies: 3
    Last Post: 09-23-2010, 04:15 AM
  3. Program crashing when replacing newline character
    By mdekom12 in forum C Programming
    Replies: 2
    Last Post: 05-01-2010, 08:49 PM
  4. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  5. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM