Results 1 to 2 of 2

Thread: Problem with code

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    152

    Exclamation Problem with code

    I am writing a program that takes in a string of arbituary size. The only problem is that when it echos the text, all of the characters are smiley faces:

    Mike


    here is the code:


    #include <stdio.h>
    #include <stdlib.h>

    void read_string(void)
    {

    char current_char;
    char *usr_data;
    int stored_chars = -1;
    int needed_chars = 128;

    usr_data = malloc(128);

    printf("Please enter some text to be echoed\n");

    while (current_char = fgetc(stdin) != 10)
    {
    stored_chars++;

    if (stored_chars > needed_chars - 1)
    {
    needed_chars *= 2;
    usr_data = realloc(usr_data, needed_chars);
    }

    usr_data[stored_chars] = current_char;

    }

    if (stored_chars == needed_chars)
    realloc(usr_data, needed_chars+1);

    stored_chars++;
    usr_data[stored_chars] = 0;
    printf("%s\n", usr_data);

    }

    void main(void)
    {
    read_string();
    }

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    STUPID FORUM ***!!!!

    Anyway I fixed up your code. You had a pretty big memory leak. Dont forget to free up your memory allocation.

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>


    void read_string(void)
    {

        
    char current_char;
        
    char *usr_data;
        
    int stored_chars = -1;
        
    int needed_chars 128;

        
    usr_data = (char*) malloc(128);           //<<<----------- I am not sure if you need to do this or not

        
    printf("Please enter some text to be echoed\n");

        while ((
    current_char = (charfgetc(stdin)) != 10)             //<<<----------- Changed
        
    {
            
    stored_chars++;

            if (
    stored_chars needed_chars 1)
            {
                
    needed_chars *= 2;
                
    usr_data = (char*) realloc(usr_dataneeded_chars);           //<<<----------- I am not sure if you need to do this or not

            
    }

            
    usr_data[stored_chars] = current_char;

        }

        if (
    stored_chars == needed_chars)
            
    realloc(usr_dataneeded_chars+1);

        
    stored_chars++;
        
    usr_data[stored_chars] = '\0';    //<<<----------- There should be a \\0 but the code tags are removing it
        
    printf("%s\n"usr_data);

        
    free(usr_data);           //<<<----------- Do NOT forget to free up the memory you use on a alloc

    }

    void main(void)
    {
        
    read_string();

    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width