Thread: BorlandC proj

  1. #46
    Registered User
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    28
    Yes, that is a peeudo code!
    ok so just to make sure, this is exactly what I have to have:

    for (int i = 0; i < m ; i++ )
    free(2Darray[i]);
    free(2Darray);

    right?!?

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that looks right.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #48
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In Borland C you may need to keep the cast, though. In my early version, I do have to keep it, but your version is much later, so maybe different.

  4. #49
    Registered User
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    28
    Guys I've got a weird problem in my prog!!! its so crazy cuz I can't get to know whats wrong!
    I was trying to debug a part of my prg, so stepping over the prg, the way it usually is! everytime there's a printf for getting a string from console, followed by a gets(...) or fgets(...), the way it was all the time is that while debugging, when it reaches to fgets or gets, it'll open up the console window asking the user to enter the string and then once gotten, it'll go back to debugging and stepping over to the next parts!
    but now, when I'm debugging this part, it reaches the gets, (or fgets, I tried both) but doesn't come up with console to ask the user for the string!!!
    like for example I wanna have A23= 6 + 7 read, and put into the array called statement! (I can't use scanf cuz it'll terminate once reaching whitespace and I can't remove spaces either cuz my prg needs spaces for the calc part to work) so I have the following:
    Code:
    char statement[10]= {0};
    printf("plz enter ur expr:\n ");
    gets(statement);
    it goes thru all three lines and the rest but never tries to get the expr once reaching gets! it jus keeps goin blindly!

  5. #50
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you PLEASE use the fgets(...) instead of gets().

    So, you probably have used "scanf()" somewhere before the fgets() - it means that you have a dangling newline in the input buffer, and the string is "empty" in gets().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #51
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Have you used scanf() AT ALL at any point earlier in the code?

    Also, see the FAQ on why using gets() is bad.
    An expression of only 9 characters is woefully inadequate.
    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.

  7. #52
    Registered User
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    28
    well I tried it with fgets too; yeah I know gets is the worst choice! I'll try to avoid it!
    yes, I do have an scanf before that! cuz its a switch case part, so I have an scanf for reading the case # first!
    ok, so what should I do to solve the problem?! anything to put right after the scanf to remove the \n from the buffer!?

  8. #53
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    while((c = getchar()) != '\n');
    Last edited by Adak; 01-18-2008 at 01:15 PM.

  9. #54
    Registered User
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    28
    um... and where do I need to put that? how come u've put a semicolon at the end? is it a typo?!
    could u plz extend ur code snippet a little bit so that I can see how this works exactly!?

  10. #55
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by mary18 View Post
    um... and where do I need to put that? how come u've put a semicolon at the end? is it a typo?!
    could u plz extend ur code snippet a little bit so that I can see how this works exactly!?
    It's just a one line while() line of code. Every statement in C must end with a semi-colon.
    It just keeps looping back to get another char from the input buffer, until it reaches the newline char left over from scanf().

    It should go just after a scanf() call, to remove the '\n' (newline), from the input buffer. If the input buffer has no newline to get, then you'll need to press Enter before the program will continue. Since there would be no display, it would look like your program had frozen up.

    You could also just use: c = getch(); as long as you're sure there is just one newline in the buffer.

  11. #56
    Registered User
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    28
    ok, while I was waiting for ur reply I tried another way of solving it too and it worked! I just put gets("\n") right after the scanf so to bring the newline out of buffer!
    Adak I'm having yet another crazy bug! remember my Q about freeing a 2D mem allocation?! it doesn't accept it! like there's no compilation or linking error caught, but when I'm debuging, when it gets to the end, the freeing part, it gives an error msg(the error is not part of borlnadC its comin out of my own OS): "CPU has encountered an illegal instruction! close prg to terminate!"
    u know btwn my options for user the last one's option #8 to exit the program! and since before exiting its gonna go thru the freeing part, once 8 is entered as the option to exit, the prog freezes!

  12. #57
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The pseudo code at the top of this page looks right, but you always have to check if the i and m variables are OK.

    I'll take a look at it, but you'll need to post up the memory declaration part, and the memory freeing part of the code.

    It's always a good check to print up the contents of your malloc'd array, just before you free it, if you're having trouble with the free part. Just to make sure that your array is what you think it is, at that point.

  13. #58
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    "CPU has encountered an illegal operation" is almost certainly because you are doing one of the following:
    1. Freeing memory that you didn't allocate.
    2. Overwriting the end of your allocation.

    First check is to see if you have the same pointer from the malloc as you do when you free. So step through the malloc code and check out [write the values down] what the pointers are [for small values], and then step through the free-code and check that the values are the exact same ones.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #59
    Registered User
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    28
    right on target Mats! "1. Freeing memory that you didn't allocate" was the case for me!
    I had put the freeing part out of my switch case with no check on the fact that if there's any mem allocated, it needs to be freed, otherwise nothing to free! so then I was trying to exit using case#8 so it'll get out of the switch but still go thru the freeing part while the case of creating a table and allocating mem for it hadn't been chosen at all!! knowing that, I fixed it and it works now!
    thanx alot guys!
    Also I was trying to hav the case of showing the content of the table whenever the user wants! and I had set a loop in which, I'd printed a dash for any cell thats empty and otherwise the actual contents!
    it looks not-too-bad when the size of the table is small! but when it gets bigger the lines get mixed up since the console window can't fit the whole thing on the page and so throws the ones at the end of the rows into the next line! what can I do about it?!

  15. #60
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your cells should be sized so you can fit only X number of them on your screen at a time, along with a small one or two character wide, border on each side. Each cell should be the same size in a column (the widest) probably.

    Personally, I see no need to put a --- in each blank cell. A blank cell should stay blank, imo.

    Of course, you can't show all the cells of a large spreadsheet on a single page. You have to put a page turner (perhaps centered near the bottom of the page), and when you press an arrow key <- it goes to the left one page, and -> it goes one page to the right. Naturally, if it's already on page one, the left arrow on the page changer, should be greyed out or removed.

    Lots of details for a good display.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fullscreen .exe window in Borlandc
    By mihaio07 in forum C Programming
    Replies: 3
    Last Post: 01-29-2008, 12:57 PM
  2. export vc++ proj to dev c++
    By jay_uccs in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2005, 07:43 AM
  3. Need Help on creating a Proj on Dev C++
    By djxtremor in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2002, 08:24 PM
  4. Simple problem for you guys - school proj
    By ryancsutton in forum C++ Programming
    Replies: 7
    Last Post: 10-01-2002, 03:06 PM