Thread: Any Idea How to fix?

  1. #1
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32

    Any Idea How to fix?

    The remove function doesnt work if i call more than 3 functions which use it without closing my program, if i close my program after using each function then everything works fine. The remove/rename functions gives error mostly after calling a function that displays a lot of text to the screen, i think it has something to do with memory, dont think it is recursion related because none of my functions call themselves, after one has been used it goes directly back to the main menu.Anyways the standard remove and rename functions work fine only if i close and reopen the program every single time i use a function and return to the main menu.So yeah definately a memory problem. from my codes you'll see i store the return value of the remove function in an integer variable in order to display error or successful messages...
    its a large program so the codes wont be on here, instead heres a link to pastebin where they are:
    [C] DataBasetype - Pastebin.com

    you can test it too just execute each function from the list and by the time u reach the 4th or fifth function the remove/delete functions will give errors which they didnt do when the previous functions were used. And this doesnt just happen when i execute each function in the order they appear in my main menu just try the functions randomly and upon using the fourth or 5th then the error will occur
    Last edited by C-learner; 04-03-2014 at 10:20 AM.
    ~If significant coding comes to you easily then you are definitely doing something wrong~
    `

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    I don't know how many people would be keen on trying to debug ~1300 lines of code. Just in case you get no such offers, here are some tips for ensuring more helpful responses:

    Try to reduce your code to a simple yet complete example that is easy to read (i.e. not too long), that compiles, and that exhibits the problem behavior you are seeing. This has the added benefit of possibly helping you find the problem yourself.

    Be more clear on where the problem is. You say the "remove function doesn't work", but I don't see any functions clearly labeled as "remove". I might guess it would be in the "open_or_close_account()" function, but you should remove all ambiguity (be specific) when asking for help.

    Learn to use a debugger. This can help you find where the problem might be. Even if you still aren't able to fix it yourself, you can provide valuable clues to others so they know the vicinity of the problem to focus on, rather than trying to debug the entire thing from scratch.

    In short - do your best to minimize the work we have to do in order to help you find the problem.

  3. #3
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    Matticus what do you have any suggestion on a particular debugger good for windows users?
    ~If significant coding comes to you easily then you are definitely doing something wrong~
    `

  4. #4
    Registered User CrowRush's Avatar
    Join Date
    Apr 2014
    Location
    Halifax, Nova Scotia, Canada
    Posts
    5
    It this was my code 'n' potentially got lost without debugging my additions. Syntax will always slap back. I'd set "breakpoints" at the end of each function. Watch that pretty global *Structure*/locals. It will kick your error once you've hit your issue.
    Cheers

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Are you using an IDE? If so, many installations come bundled with a debugger. If not, what compiler are you using? You also received advice for other helpful tools here.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Matticus raises a lot of good points. Some other notes:

    • You are using goto. Goto is generally the wrong way to control the flow of your program. Read this link. There are appropriate uses of it, but your program does not qualify. Find better ways (if/else, switch/case, loops, functions) to control program flow.
    • As CrowRush suggested, avoid the use of global variables (read this link). Globals have their place, but not in your program.
    • Your program could use more helper functions. Much of your code is repeated and thus reusable. Especially the end of your function where you do all the error checking. It's 37 lines of identical code is several places -- better it's in a function that gets called at the end of each function. One place to find/fix mistakes (like your remove error).
    • Other things that can be put in functions: printing account info (lines 391-402, 747-758, 781-792 and all other places you see that block of printfs); updating a record in a file; retrieving a record in a file
    • The remove function being referred to is in the standard C library, in stdio.h (see C11 7.19.4.1).
    • Your program does not give good diagnostic output. You basically just say "error", you don't specify what type of error. Look into the perror or strerror functions for standard functions that fail. Consider descriptive messages like "Bad file format" instead of "THERE IS A FILE ERROR\n" in other cases.
    • Your program does use recursion, it just uses "indirect" or "mutual" recursion, meaning a function doesn't call itself directly, but it calls some other function(s) that in turn call it (without the original functions ever truly finishing).
    • Your program does not assign proper account numbers, for example I got: Account # : -1817671705. Not between 0 and RAND_MAX.
    • I get a "FILE ERROR OCCURED" (note, correct spelling has 2 R's: occurred) after a depositing money. The only thing I did before that was create an account. But this is separate from the remove() error you are seeing.
    • I can not actually trigger the remove() error you are seeing, even after running through 15-20 menu options.
    • Your input reading is not very robust. I made your program seg fault by accidentally putting in letters for the year of birth field. Consider reading all input as a whole line (using fgets), then using something like strtol or sscanf to parse that string for numeric data.
    • Your flow is not ideal. If I accidentally type a wrong input for savings plan type (e.g. 'F'), I get sent to the main menu instead of simply being told my choice was invalid and being asked again.
    • If the user's actual goal is to delete/rename a file, you should tell them that the operation was successful. If, however, the goal is to manipulate account info/balances, the user should not see messages like "FILE DELETED SUCCESSFULLY" and "FILE RENAMED SUCCESSFULLY". It confuses and/or frightens the user.
    • For me at least, in my Linux-equivalent version of your program, the sleep calls to display messages periodically don't work. I just get a long delay then a giant blob of text. You would need to fflush(stdout) after each printf to cause the text to go to the screen immediately. Maybe it works right in Windows though, I don't know.
    • The rest are nits/minor annoyances:
    • Also, some options like create account and check account have a "would you like to <create|check> another account?" prompt, while other menu options don't. An inconsistent UI is annoying and confusing to most users. Furthermore, giving them a yes-no prompt in those choices doesn't really offer convenience or time-saving, since the same amount of effort is required to type 'Y' (repeat) as it would be to press 'A' or 'F' from the main menu.
    • ALL CAPS IS LIKE SHOUTING!!!!
    • Clearing the screen all the time gives a "disruptive" feel to your program and makes it a bit awkward to use at times. Especially when I have to re-enter giant account numbers like -1817671705 all the time. I have to scroll way back to find them to copy-paste, plus I'm always having to "press a key to continue".


    Hmm...there's probably lots more, but that should give you plenty to chew on for now.

    EDIT: Ahh, I forgot:

    I have a very strong suspicion that you wrote most of this without testing. Plan first. Write small amounts of code -- 5-10 lines -- and compile/test often. Fix all compiler errors and warnings, and fix all logical errors and bugs. Test thoroughly, including bad input, "corner" or "edge" cases (create account with 0, 99, 100, 101, 500 dollars), etc -- don't just test input that is supposed to work, since users make typos and do stupid things, either on purpose or by accident. Don't move onto the next 5-10 lines of code until everything prior works perfectly.
    Last edited by anduril462; 04-03-2014 at 12:36 PM.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Looks like the code is no longer available at that link.

  8. #8
    Registered User C-learner's Avatar
    Join Date
    Feb 2014
    Posts
    32
    Thank you so much for all those improvement ideas im gonna spend a lot of time making those changes
    ~If significant coding comes to you easily then you are definitely doing something wrong~
    `

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I have no idea what's going on
    By theworldlovesu in forum C++ Programming
    Replies: 16
    Last Post: 07-18-2011, 08:02 PM
  2. Idea
    By User Name: in forum General Discussions
    Replies: 0
    Last Post: 04-10-2011, 02:31 PM
  3. Have an idea?
    By B0bDole in forum Projects and Job Recruitment
    Replies: 46
    Last Post: 01-13-2005, 03:25 PM
  4. Just an idea >?<
    By Cgawd in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-30-2002, 09:08 AM
  5. Good idea, bad idea.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-15-2002, 12:26 PM