Thread: is it correct.. ??

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    20

    is it correct.. ??

    The below code take the entry from the user in a window and then displays the text back.

    The problem is it runs once fine, but if used a number of times it starts accepting much lesser text. One of my collegue said probably the buffer is getting filled, so delete the .obj file and recompile it. But with no success..

    pls somebody guide me..

    #include <stdio.h>
    #include <conio.h>

    main()
    {
    clrscr();
    window(10,10,70,20);
    textcolor(WHITE);
    char string[512];
    char *p;

    cprintf("Enter a string \r\n");
    p = cgets(string);

    cprintf("\r\n Your text is : %s", p);
    }

    - vivekjk

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    What OS/compiler do you use? (I hope it's not TurboC)

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    20
    yaa the OS is Win2K and compiler is Turbo C.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    TurboC has some bugs (nr 17, 18 and 19) in the cgets function which are solved now (I'm not sure if this also counts for the win2k version). Maybe you need to install a fix for it.
    Last edited by Monster; 11-01-2002 at 05:10 AM.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    20
    The program seems to be working fine on the Borland C++ Compiler 5.5.

    Thanx.

  6. #6
    Bios Raider biosninja's Avatar
    Join Date
    Jul 2002
    Location
    South Africa
    Posts
    765
    Works fine on my compiler : Borland C++ 5.02

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char string[]
    string is a reserved name in C.

    >>variable declarations
    Don't forget you should declare your variables at the top of the code block, not half way through it.

    >>main
    Should be declared as
    >>int main(void)
    ... with a return 0; at the end.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User angeljicu's Avatar
    Join Date
    Nov 2002
    Posts
    16

    Smile i think........

    In my opinion,you need to add a line
    #include <stdio.h>
    #include <conio.h>

    main()
    {
    char string[512];
    char *p;
    clrscr();
    window(10,10,70,20);
    textcolor(WHITE);

    cprintf("Enter a string \r\n");
    p = cgets(string);

    cprintf("\r\n Your text is : %s", p);
    free(p);free(string);
    /*free the mem of p,string;maybe your array too large*/
    }


    Last edited by angeljicu; 11-01-2002 at 08:20 PM.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Posts
    20
    I think there is still something wrong in here.. coz since string is a reserved word in c, i changed it to buffer and now this program after compiling in Borland C++ 5.5 its accepting more than 50 characters.

    aaye guys, pls help yaar.. in here i want to accept the entry from the user and store it in a file and append the entries.. thats the later part.


    #include <stdio.h>
    #include <conio.h>

    main()
    {
    char buffer[50];
    char *p;
    clrscr();
    window(10,10,70,20);
    textcolor(WHITE);

    cprintf("Enter a string \r\n");
    p = cgets(buffer);

    cprintf("\r\n Your text is : %s", p);
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: i think........

    Originally posted by angeljicu
    In my opinion,you need to add a line

    |-----snip-----|

    char string[512];
    char *p;

    p = cgets(string);

    cprintf("\r\n Your text is : %s", p);
    free(p);free(string);
    /*free the mem of p,string;maybe your array too large*/
    }
    Ok, lots and lots of stuff wrong here. For example:

    1) cgets is being used incorrectly. And I quote:

    To use, you must pre-fill the first character of the buffer. The first character is the size of the buffer. On return, the second character is the number of characters read. The third character is the first character read.
    It's simply amazing what you can learn if you take 10 seconds and with a search engine. I mean, take me for example. Having never used cgets, in no time at all I was able to find out exactly how to use it, and how to use it correctly. I guess I'm just freeking gifted.

    2) You do not need to free anything!

    'p' just ends up pointing to the third element of the array. You do not free arrays.

    3) The size of the array isn't too large as far as arrays go. The only possible problem I see here is that the first character is supposed to hold the size of the array. If you try and cram 512 in there, it won't fit.

    However, since neither of you are using cgets correctly anyway, this is hardly your problem.

    And now to fix your (everyone who has posted) problem:

    char buf[BUFSIZ] = { 127 }; /* Sure the buffer is bigger than 127, but what do I care? */
    char *p;
    ...
    p = cgets( buf );


    Vola!


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

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    20
    thanx a lot.. this takes exactly 127 characters and it works great..

    thanx once again.. i shall surely be back with more queries..

    here is the code thats working fine..
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    main()
    {
    char buffer[BUFSIZ] = {127};
    char *p;
    clrscr();
    window(10,10,70,20);
    textcolor(WHITE);
    
    cprintf("Enter a string \r\n");
    p = cgets(buffer);
    
    cprintf("\r\n Your text is : %s", p);
    }
    &#91;code]&#91;/code]tagged by Salem
    Shame it wasn't indented

  12. #12
    Registered User angeljicu's Avatar
    Join Date
    Nov 2002
    Posts
    16

    Question sorry......maybe i can't understand

    In our books,I even cannot find a line like you have corrected for me

    "char buf[BUFSIZ] = { 127 };
    /* Sure the buffer is bigger than 127, but what do I care? */"

    why can we used a undefined BUFSIZ?
    we often write language like this:

    "char buf[10]={1};"
    or "#define BUFSIZ 10
    char buf[BUFSIZ]={127};"


  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    BUFSIZ is defined in stdio.h. That's why you can use it.

    Furthermore, what I did on that line was to initialize the first element of the array. Since I didn't define every element of the array, they were by default set to zero. If you initialize some of the indexes of an array, any you do not define will be set to zero automaticly.

    For example:

    int array[10] = { 1, 2, 3, 4, 5 };

    Five elements will be initialized to the specified values, the rest will be zero.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux for GNU/Linux is not correct?
    By password636 in forum Linux Programming
    Replies: 8
    Last Post: 03-31-2009, 08:30 PM
  2. Is this correct : passing strings?
    By socket in forum C Programming
    Replies: 15
    Last Post: 11-25-2008, 02:03 PM
  3. correct order to insert new node?
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2004, 07:51 PM
  4. Replies: 1
    Last Post: 05-26-2004, 12:58 AM
  5. Loop until enter correct value
    By jchanwh in forum C Programming
    Replies: 2
    Last Post: 11-27-2001, 01:23 AM