Thread: Printing Extra Characters such as Sigma and Smile Face. HELP

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    Printing Extra Characters such as Sigma and Smile Face. HELP

    This is my code:

    i Input characters then replace it with hyphens. however, sigma and smiley faces are produced when print it.

    Code:
     #include <stdio.h>
    
    void main ( )
    {
    int a;
    char s[80], b[80];
    
    clrscr ();
    
    for (a=0;a<80;a++)
    {
    s[a] = '.';
    }
    
    printf ("Type a string less than 80 characters: ");
    gets (s);
    printf ("The string types is:");
    puts(s);
    
    for (a=0;a<80;a++)
    {
    	if ((s[a]==' ')||(s[a]==' ')||(s[a]=='.'))
    	{
    	b[a]=' ';
    	}
    
    	else
    	{
    	b[a]='-';
    	}
    }
    
    puts(b);
    
    getch ();
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Strings must end in a \0 character, unless you want the random bits of stuff after them to also print out.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just a few other things to take note of:
    • void main() should be int main(), and you should return 0 unless you are compiling with respect to the 1999 edition of the C standard
    • gets() should be avoided because it makes your program vulnerable to buffer overflow. Use fgets() instead, or scanf() with an appropriate field width for the format specifier.
    • The non-standard functions clrscr() and getch() are probably unnecessary here, but if you do want to use them, you should #include <conio.h>
    • You should indent your code more consistently.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Quote Originally Posted by tabstop View Post
    Strings must end in a \0 character, unless you want the random bits of stuff after them to also print out.
    how can i assign the last value to be \0 ?

    Quote Originally Posted by laserlight View Post
    Just a few other things to take note of:
    • void main() should be int main(), and you should return 0 unless you are compiling with respect to the 1999 edition of the C standard
    • gets() should be avoided because it makes your program vulnerable to buffer overflow. Use fgets() instead, or scanf() with an appropriate field width for the format specifier.
    • The non-standard functions clrscr() and getch() are probably unnecessary here, but if you do want to use them, you should #include <conio.h>
    • You should indent your code more consistently.
    i'm using the old Turbo C program. How to use fgets()?
    i tried using scanf() but after the first word i enter, it wont be printed already.
    Last edited by tanpearl; 02-21-2009 at 12:40 AM. Reason: I forgot to ask about scanf

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by tanpearl View Post
    how can i assign the last value to be \0 ?
    I would recommend not assigning anything -- the whole point is, it's already there, if only you didn't write over it in your for loop.

    You should not (necessarily) go 80 places in that last for loop, but only the amount of letters typed in.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    i tried removing the for loop but when it prints, unnecessary hyphens appear and even the sigma and smiley faces aren't removed.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tanpearl
    How to use fgets()?
    It can be as simple as:
    Code:
    fgets(s, 80, stdin);
    Unfortunately, the newline from entering input is almost always placed into the string, so you should remove it, usually with strchr(). However, since you are going to loop over the characters of the string...

    Quote Originally Posted by tanpearl
    how can i assign the last value to be \0 ?
    Instead of using periods ('.') to mark the characters that are unused, I would simply use null characters and thus initialise s to an empty string:
    Code:
    char s[80] = "";
    This means that you can ditch the first loop since it is no longer necessary. Next, after you have read in the string, you should loop with a condition of a < 79, not 80, since you want to leave at least one null character to mark the end of the string. In the loop body, you no longer need to check for spaces and periods, but instead you can check for '\n' and replace it with a '\0', thus getting rid of the unwanted newline (if it is present).

    EDIT:
    tabstop does have a point though: since you intend to ignore unused characters, you should be looping with a condition of a <79 && s[a] != '\0' so that you stop once you reach a null character.
    Last edited by laserlight; 02-21-2009 at 12:43 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    Unnecessary characters disappeared. However...

    I've revised my code. however after the first word, the rest won't print already.

    Code:
    #include <stdio.h>
    
    void main ( )
    {
    int a;
    char s[80], b[80];
    
    clrscr ();
    
    
    
    printf ("Type a string less than 80 characters: ");
    gets (s);
    printf ("The string types is:");
    puts(s);
    
    for (a=0;a<79;a++)
    {
    	if ((s[a]=='\0')||(s[a]==' '))
    	{
    	b[a]='\0';
    	}
    
    	else
    	{
    	b[a]='-';
    	}
    }
    
    puts(b);
    
    getch ();
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is because you insert null characters where you should insert spaces. The way to handle the null character is to copy it to b, and then terminate the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There really should be an Adventures of Sigma and Smiley-Face.

    What's funny is when I ran the Original Post, it worked fine in the sense that there was no garbage in it and it does what it looks like. I changed the last line to this:
    Code:
    printf("\n+%s+\n",b);
    Output:
    Code:
    Type a string less than 80 characters: a string less than...80 characters
    The string types is:a string less than...80 characters
    
    +- ------ ---- ----   -- -----------                                             a string less than...80 characters+
    This might be harder to get without a widescreen monitor
    Last edited by MK27; 02-21-2009 at 01:12 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Thanks for helping. It helped alot. Btw, here's the code.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main ( )
    {
    int a;
    char s[80], b[80];
    
    clrscr ();
    
    printf ("Type a string less than 80 characters: ");
    gets (s);
    printf ("The string types is:");
    puts(s);
    strcpy (b,s);
    
    for (a=0;a<79;a++)
    {
    	if ((b[a]=='\0')||(b[a]==' '))
    	{
    	}
    
    	else
    	{
    	b[a]='-';
    	}
    }
    
    puts(b);
    
    getch ();
    }

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, do something like this:
    Code:
    int a;
    char s[80], b[80] = "";
    
    /* read into s */
    
    for (a = 0; a < 79 && s[a] != '\0'; a++)
    {
        if (s[a] == ' ')
        {
            b[a] = ' ';
        }
        else
        {
            b[a] = '-';
        }
    }
    Notice that the loop condition now accounts for the null character. I chose to initialise b to an empty string instead of copying the null character from s to b. Most of all, notice that I used s[a] in the comparison instead of b[a].

    Note that if you are not going to do anything with s, then you might as well do without b and just use s. By the way, try to use more descriptive names than s and b. It is fine to use a for a loop index, but it can be a little confusing when you also have a char array named b (it can be expected that a and b "go together").

    EDIT:
    Oh wait, I notice that you used strcpy(), in which case it is correct to compare with b[a] instead of s[a]. However, it also makes either s or b redundant.
    Last edited by laserlight; 02-21-2009 at 01:25 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    thanks! the code you entered is easier to understand. and i need two arrays because it is a game im doing and im just doing it step by step. =)

Popular pages Recent additions subscribe to a feed