C database problem

This is a discussion on C database problem within the C Programming forums, part of the General Programming Boards category; ok so I have a few problems with my code, first off the program i am trying to make uses ...

  1. #1
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230

    Question C database problem

    ok so I have a few problems with my code, first off the program i am trying to make uses a database, the user inputs a date and my program will turn it into a code that it will store in a txt file and the program will read it every night at mid night and if it has a code correspondes with the date it will show on the screen how many there are. but my problem right now is that i cant change the date to code.

    this is what i was trying to do but it crashes. this test does not include the whole date i was only testing with february, (at one point i think i try to change a char variable into an int variable, i dont no if this is legal or not so feel free to comment about it.) i have included an attachment if you wish to run it in your compiler.



    #include <conio.h>
    #include <stdio.h>
    int main(int arge, char *argv[])
    {
    FILE *pfile;
    int month;
    char sced[21];
    float date[32];
    char answer = 'y';
    char num1;
    char february;
    pfile = fopen("database2.txt", "w");

    if (pfile != NULL)
    {



    while (answer == 'y')
    {
    printf("write the month: ");
    if (month == february);
    {
    february = 01;
    }
    scanf("%s", &month);
    printf("what is happening:");
    scanf("%s", &sced);
    printf("loading");
    fprintf(pfile,"%s", &sced); /* crashes here*/
    fprintf(pfile,"%s\n", &month);
    printf("do you wish to put in any more dates [y/n] \n");
    answer = getch();
    }

    fclose(pfile);
    }

    else
    {
    printf("could not open the file.\n");

    }
    getch();
    }
    Attached Files Attached Files

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Code:
    scanf("%s", &month);
    month is not a string, so why are you using a %s here?
    Code:
    scanf("%s", &sced);
    sced IS a character array, and by definition the name of the array the address of the first element in the array, so you do not need to have a & in there
    Code:
    fprintf(pfile,"%s", &sced); /* crashes here*/
    You are printing the address as if it where a string...lose the & again
    Code:
    fprintf(pfile,"%s\n", &month);
    Again, month is not a string, so why the %s? And also again, why print the address rather than the value?

    Those are the things that jump out at me.

  3. #3
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    I took your advice and it doesn't crash any more , but when I open the program this is what is does :
    write the month: february <---(i write the month, press enter then it just skips to the end of the loop)
    what is happening:loadingdo you wish to put in any more dates [y/n] <-----(like this)

    i have included the file if you would like to see for yourself, and the code:




    Code:
    #include <conio.h>
    #include <stdio.h>
    int main(int arge, char *argv[])
    {
    FILE *pfile;
    int month;
    char sced[21];
    float date[32];
    char answer = 'y';
    char num1;
    char february; 
    pfile = fopen("database2.txt", "w");
    
    if (pfile != NULL)
    {
    
    
    
    while (answer == 'y')
    {
    printf("write the month: ");
    if (month == february);
    {
    february = 01;
    }
    scanf("%d", &month);
    printf("what is happening:");
    scanf("%d", sced);
    printf("loading");
    fprintf(pfile,"%s", sced); 
    fprintf(pfile,"%d\n", month);
    printf("do you wish to put in any more dates [y/n] \n");
    answer = getch();
    }
    
    fclose(pfile);
    }
    
    else
    {
    printf("could not open the file.\n");
    
    }
    getch();
    }
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Database setup for searching based on string distance
    By Mario F. in forum General Discussions
    Replies: 4
    Last Post: 02-24-2010, 06:15 AM
  2. Getting illegal case error
    By scmurphy64 in forum C Programming
    Replies: 2
    Last Post: 09-17-2009, 11:35 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  4. Making a Simple Database System
    By Speedy5 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2003, 10:17 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM

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