incredibly simple problem (I think)

This is a discussion on incredibly simple problem (I think) within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <ctype.h> int main() { char phrase[30]; char *y; printf("What un-leet phrase shall I translate?\n"); scanf("%c", phrase); ...

  1. #1
    Unregistered
    Guest

    Angry incredibly simple problem (I think)

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
    
    	char phrase[30];
    	char *y;
    	
    	printf("What un-leet phrase shall I translate?\n");
    	
    	scanf("%c", phrase);
    
    
    	y = phrase;
    
    	while(*y != '\0')
    	{
    		
    		switch(*y)
    		{
    		case 'e':
    			putchar('3');
    			y++;
    			break;
    		case 'E':
    			putchar('3');
    			y++;
    			break;
    		case ' ':
    			putchar(' ');
    			y++;
    			break;
    		default:
    			putchar(*y);
    			y++;
    			break;
    		}
    						
    	}
    	putchar('\n');
    }
    the problem with this code is when it takes the string and tries to convert it to "leet speak" (Lame, I know, but I'm just practicing!) it will only do one word and stops after that. how do I get it to continue? (I thought that the spaces would be treated as characters but something goes awry in the program!)

    Thanks for any help

  2. #2
    Unregistered
    Guest
    At the end of any group of characters, a \0 is appended. Therefore, in the while loop, when testing *y with '\0', it will end the while loop when it reaches the end of a group of chars. I could be wrong that's my best guess.

  3. #3
    Registered User goran's Avatar
    Join Date
    Sep 2001
    Posts
    68
    use gets(phrase) instead of scanf("%c", phrase).

    hope that helps.

    cheers,
    I don't wait for the future; it comes soon enough.

  4. #4
    Unregistered
    Guest
    Originally posted by goran
    use gets(phrase) instead of scanf("%c", phrase).

    hope that helps.

    cheers,
    Thanks! now, could someone explain why that works and scanf doesn't?

  5. #5
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    If I'm not mistaken, when you use scanf to input a string you use "%s"

    like so:

    Code:
    scanf("%s", phrase)
    Last edited by dbaryl; 12-04-2001 at 12:57 PM.
    This is my signature. Remind me to change it.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >use gets(phrase) instead of scanf("%c", phrase).
    Use fgets instead of gets. gets is buggy and unsafe.
    fgets(phrase, sizeof(phrase), stdin);

    >could someone explain why that works and scanf doesn't?
    Well, for starters the way you posted it scanf was only reading the first character of the phrase. But if you were to use %s it would still only read the first word as scanf uses a space character as a delimiter for when to stop reading.
    gets and fgets use '\n' and EOF as the delimiter, so you read until the end of the line or until the end of the file if there are no newlines.

    >If I'm not mistaken, when you use scanf to input a string you use "%s"
    To input a single string yes, but if you want to input a phrase or sentence you have to either use a line input function or fiddle with scanf and loops.

    To be honest, I'm surprised that you don't get warnings or errors when compiling. But it compiles fine and on my compiler causes some terrible garbage spewing when run.

    -Prelude
    Last edited by Prelude; 12-04-2001 at 12:59 PM.
    My best code is written with the delete key.

  7. #7
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    it worked fine when I did it with %s...
    This is my signature. Remind me to change it.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Yes, but when you ran the program did you enter in a single word and get a single word as output or enter a phrase with spaces and get a single word as output.

    When I ran it with scanf and %s, here's what I got:
    input - Speak to me my aged friend.
    output - Sp3ak

    But with fgets i got this:
    input - Speak to me my aged friend;
    output - Sp3ak to m3 my ag3d fri3nd.

    Somewhat of a critical difference in the proper workings, no?

    -Prelude
    My best code is written with the delete key.

  9. #9
    Unregistered
    Guest
    Originally posted by dbaryl
    If I'm not mistaken, when you use scanf to input a string you use "%s"

    like so:

    Code:
    scanf("%s", phrase)
    yes I originally had that and since it didn't work I tried %c for the hell of it.

    Thanks for the info Prelude, I am not yet familiar with fgets so I didn't know that at all.. and if I may ask, why is gets unsafe? (last night when messing around I used gets and in linux the compiler said it was unsafe.. made me wonder)

    grr.. time to register or remember my old user/pass.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Here's why:
    Bugs:

    Because it is impossible to tell without knowing the data
    in advance how many characters gets() will read, and
    because gets() will continue to store characters past the
    end of the buffer, it is extremely dangerous to use. It
    has been used to break computer security. Use fgets() instead.
    And here's where you can find info on all standard functions in C.

    And last but not least, a way to do it with scanf. I'm still debatting whether or not this is good style though. I still prefer fgets.
    Code:
    /*read everything until you get to a newline into phrase*/
    scanf("%[^\n]", phrase);
    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    I have to agree with prelude on this, scanf is best used by not using it, espicially with number inputs as newline character are left in the input buffer. So NEVER use scanf!!!
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Does that mean you don't usually agree with me?

    Just kidding

    -Prelude
    My best code is written with the delete key.

  13. #13
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Does that mean you don't usually agree with me?
    Well I dont agree with that or do I.
    I cant agree with myself most of the time
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  14. #14
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Can you guys tell me how this works:
    Code:
    scanf("%[^\n]", phrase);
    how does the [^\n] part of it work... I'm kinda new to all this...
    This is my signature. Remind me to change it.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    %[ ]

    This is used to read until a character other than what is enclosed is encountered. Use a ^ before any characters to reverse this effect: to read until one of the enclosed characters is encountered. Thus:

    %[abcdefg]

    Would read those characters, in any order, into a string. For example, the following line of charaacters would all be read and stored:

    abcedefefefefabcegabecabgac

    However, the same would stop at the 'h' character in the following line:

    abaegeabahabaeg ...the 'abaeg' at the end would not be read.

    With the ^ symbol:

    %[^abcdefg]

    We would read until we encounter any of the enclosed characters:

    t34903j3k3j4r2la

    We'd stop when we hit the 'a'.

    Additionally, you can use '*' to read something, but not put it into a variable. For example, if we had this line:

    John 45 dog

    And all we wanted was the second column, we could:

    fscanf( fp, "%*s %d %*s", &age );

    To read only the second column...

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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21