Thread: dont know why prog crashes

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    22

    dont know why prog crashes

    THis program is an inventory program, the first time u run it it creats a file to store the inventory, but after you exit the program and run it again as soon as you enter a code for anything it crashes, can anyone fix it or tell me how to fix it???

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > #define clrscr() system("cls")
    Strike 1.

    > void main()
    Strike 2.

    > can anyone fix it or tell me how to fix it???
    A. I'm sure quite a few could.
    B. Code it without errors.

    Possibly, you should scan the file yourself, and mess around with functions by themselves. Independtly try to figure out exactly where you're going wrong. Culture functions out of your large program, and get them into smaller programs by themselves. Use the "singling out" method. Then, once you think you have pin-pointed the possible error, try and fix it yourself. If you need help with the discovered problematic area, post the code for troubleshooting.
    Last edited by Shadow; 05-22-2002 at 07:05 PM.
    The world is waiting. I must leave you now.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    It has no ERRORS!!!!!!!
    when i run it without the file created already it works perfect!!!!! it just crashes when the file is there!!!!

    and i loked at the debugger and it showed that it was crashing whenever you entered the code field.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > it just crashes when the file is there!!!!
    That's odd. It works just fine for me.
    Though, it doesn't write to the file, and you should have an option to stop entering products, and return to the menu.

    [EDIT]
    Now that i've reread the posts, I noticed something.

    > B. Code it without errors.
    > > It has no ERRORS!!!!!!!

    > when i run it without the file created already it works perfect!!!!! it just crashes when the file is there!!!!
    That seems like an "error" to me.
    [/EDIT]
    Last edited by Shadow; 05-22-2002 at 09:12 PM.
    The world is waiting. I must leave you now.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, it's a shot in the dark, but:

    Code:
    
    In_File = fopen("Inventory.dat", "r+b"); //opening in binary mode for read/write
    
      if (In_File == NULL)
      {
    	printf("\n\nFile does not exist: Creating file");
    	In_File = fopen("Inventory.dat", "w+b"); //creation in binary mode

    ..."r+b" should be "r+", and "w+b" should be "w+"....

    I honestly don't have the time to debug it for you, but I can tell you that you're not far from completion - just keep at it.

    Good luck.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    i tried r+ and w+, and it still crashes but thanx for trying to help, maybe it is just my comp and it might work on another, but thanx for all the help guys

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I see you are using: fflush(stdin). This is wrong (see thread)
    Code:
    head=work=(struct linven *)malloc(sizeof(struct linven));
    I'm not sure if this works on every compiler. Personally I prever:
    Code:
    head=(struct linven *)malloc(sizeof(struct linven));
    work = head;
    Code:
    fread(work,sizeof(struct finven),1,In_File);
    Always check return values:
    Code:
    if(fread(work,sizeof(struct finven),1,In_File) != 1)
       /* do something */
    Code:
    work->next=(struct linven *)malloc(sizeof(struct linven));
    Again check your return values:
    Code:
    if((work->next=(struct linven *)malloc(sizeof(struct linven))) == NULL)
      /* do something */
    B.t.w. I'ts working perfect on my machine. Nice piece of work!

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    thanx!!!!
    but i got to get it to work on mine cause i its a project for school!!!!!

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by mattyans
    thanx!!!!
    but i got to get it to work on mine cause i its a project for school!!!!!
    If it's still broken, post your latest attempt (only the suspect bit at first).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User speedster's Avatar
    Join Date
    Feb 2002
    Posts
    7
    Here is a program I think that will help.
    A example of course hope it helps you.
    Last edited by speedster; 05-23-2002 at 03:32 PM.

  11. #11
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Speedster, don't use void main.
    If it's not your program, don't post void main programs.

    Why?
    Simply,
    If you're too lazy to say int main .... return 0;, then what else are you to lazy to do?
    The world is waiting. I must leave you now.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure if this works on every compiler. Personally I prever:
    Yes, assignments such as this are valid and will work on all compilers.

    a = b = c;

    This is valid. While it may not be clear to everyone what happens, it is perfectly valid. In this case, c is assigned to b, which is in turn assigned to a.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    22
    here is the latest attempt

    still same problem

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >void main()
    You're still persisting with this? Make it int main(void).

    >while(!feof(In_File))
    This won't work properly in all cases. You need to check the return from fread().

    >In_File = fopen("Inventory.dat", "wb");
    You should check this opened successfully before using it.

    >fflush(stdin);
    Good grief.....

    >(struct linven *)malloc(sizeof(struct linven));
    cast the return from malloc isn't needed, don't do it.

    >scanf("%f", &work->price);
    Always check the return code from this function to ensure you read correct data.

    >In_File = fopen("Inventory.dat", "r+");
    I believe that depending on your compiler, you'll probably want "rb" here. You certainly don't want r+, as this is read/write mode, and you're only reading.

    >head=work=(struct linven *)malloc(sizeof(struct linven));
    >fread(work,sizeof(struct finven),1,In_File);
    This is also wrong. You are malloc'ing memory sizeof linven, then within fread you are loading the number of bytes used by struct finven. Make your mind up which one it should be! You also have the same problem on the write, and in the add_record() function. I think you have your two structures muddled up.

    If you're having trouble with it crashing, put a few printf() statements in convenient places to help pin point the line of code that is killing it. But I suggest you fix the above lot first.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > But I suggest you fix the above lot first.
    Yes, review this thread a great deal.
    Refer to man pages, FAQS, and google trying to solve / fix all of the above suggestions.

    Maybe it's just me ( ? ), but, you seem to rush frantically to fix your code. It seems as if your taking the suggestions, and frantically putting them to work without taking your time to slowly understand things. I mean, poor ol' Hammer sounds like a broken record.

    If this isn't the case, then woops, I made a faulty assumption.

    Quote from hammer:
    > If it's still broken, post your latest attempt (only the suspect bit at first).
    You post the entire code.

    > but i got to get it to work on mine cause i its a project for school!!!!!
    We have all displayed the generousity to help with a topc that is mainly shunned.

    Now, as I review the thread once more
    > maybe it is just my comp and it might work on another, but thanx for all the help guys
    ...not once do I hear you mention the intended OS, and compiler you are using.

    [edit]
    To be clear, this information would be vital to us atleast getting the thing up and running.
    [/edit]

    Judging by your #define clrscr() = system("cls"); call, or there-abouts, I would say your compiling on windows.

    Yes?
    What compiler?

    It shouldn't be "your computer"...that's an irrelevant issue. What matters is your OS and compiler, which have not yet been confirmed.

    BTW, the proper way to use comments in C is:
    /* Comments */
    Not:
    // Comments
    The 2nd one is C++.
    Last edited by Shadow; 05-23-2002 at 06:22 PM.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prog crashes upon read of system file
    By Queatrix in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2007, 10:28 PM
  2. Program crashes and memory leaks
    By ulillillia in forum Tech Board
    Replies: 1
    Last Post: 05-15-2007, 10:54 PM
  3. DEV-C++ made program crashes when run from outside the ide
    By rainmanddw in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 10:27 PM
  4. Exe crashes with Stack Dump
    By Shaman in forum C++ Programming
    Replies: 4
    Last Post: 10-19-2001, 02:24 PM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 05:33 PM