Thread: newbie help needed plz

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    newbie help needed plz

    Hi guys, Im a complete begginer and currently working through c for dummies to try and learn this language.

    The trouble is there are a few mistakes in the book. Figuring these out myself is probably helping me learn but, mistakes with the more complex stuff are a nightmare when im trying to teach myself.

    Im currently on this:
    http://www.c-for-dummies.com/sourcecode/learn/madlib1.c

    I have managed to compile and build the program. I no my source code is correct now as i have even swapped it with a official downloaded version and i get the same result. I have searched online and it seems to work for everyone else. But

    For some reason my computer will not display a word for food!?
    My output reads : "Don't touch that hairy !
    I just vacuumed the couch!"

    Any help will be appreciated as i don't want to skip anything! Thanks, Mike
    Last edited by mikedeuk; 08-10-2011 at 04:48 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. You don't actually need the & before the name of an array there.
    2. %s will only take a single word (it will stop on a space).
    3. You hit enter after you type the first word, which gets left behind and consumed by the food line. Try adding a space before the % sign.
    Code:
    scanf( " %s", food );
    4. According to your output, nothing is wrong:
    Quote Originally Posted by you
    My output reads : "Don't touch that hairy waffle!
    I just vacuumed the couch!"
    That's four things.


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

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Hi, Thanks for the help. You were quite right, I mistakenly added the missing word in my output(its been a stressful morning lol) Now edited.

    But im afraid after using your tips and changing my code its still not working.
    Could it be a problem with my compiler or software? It seems to work for everyone else.

    Is this what they call bugs or debugging when you need to test programs on different machines?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mikedeuk View Post
    Is this what they call bugs or debugging when you need to test programs on different machines?
    Yes.

    Another thing you can try here is this:

    Code:
    	printf("Enter an adjective:");		/* prompt */
    	scanf("%s%*c",adjective);			/* input */
    	printf("Enter a food:");
    	scanf("%s%*c",food);
    	printf("Enter a household chore (past tense):");
    	scanf("%s%*c",chore);
    	printf("Enter an item of furniture:");
    	scanf("%s%*c",furniture);
    %*c tells scanf to discard a single character, hopefully the '\n'. It's not the perfect solution, but IMO there is no such thing when using scanf() for keyboard input. fgets() is much better. If you then need to break the input down into various types, you can use sscanf() on the string read in by fgets().
    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

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Thanks for the help. Still missing waffle though! lol

    Maybe my mac or gcc compiler just dosnt work well with this code?
    In which case i'll just skip onto the next chapter in the book and hope it soon explains fgets!

    Any other tips for a begginer trying to learn C with books?
    EDIT: Just seen your ad for "The C Book" so may also use that when i get time! ; )
    Last edited by mikedeuk; 08-10-2011 at 06:19 AM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mikedeuk View Post
    Maybe my mac or gcc compiler just dosnt work well with this code?
    It's more to do with the system itself. But if you use that and do exactly what were asked, without ever hitting the space bar, it should work.

    The issue is that the input is buffered by the system, and what gets read from and left in that buffer.

    But don't get too hung up on this right now; when you really need to get input correctly, you'll find a way, but you do need to understand some more choices first (like fgets).

    The C Book is good, I think.
    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

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    I just had one final go and still no joy with:/*

    insert
    Code:
    MADLIB1.C Source Code
    Written by Dan Gookin
    */
    
    #include <stdio.h>
    
    int main()
    {
    	char adjective[20];
    	char food[20];
    	char chore[20];
    	char furniture[20];
    
    /* Get the words to use in the madlib */
    
    	printf("Enter an adjective:");		/* prompt */
    	scanf("%s%*c",&adjective);			/* input */
    	printf("Enter a food:");
    	scanf("%*s%*c",food);
    	printf("Enter a household chore (past tense):");
    	scanf("%s%*c",&chore);
    	printf("Enter an item of furniture:");
    	scanf("%s%*c",&furniture);
    
    /* Display the output */
    	
    	printf("\n\nDon't touch the %s %s\n",adjective,food);
    	printf("I just %s the %s!\n",chore,furniture);
    
    	return(0);
    }
    I don't even understand why it only has a problem with the first output line, as the second also has 2 words but it works fine!
    Anyway im going to give up and move on now before i get all together fed up with it!
    Thanks for all your help, Im sure i'll be back soon!

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Again...

    Code:
    scanf("%s%*c",&adjective);			/* input */
    ...
    scanf("%s%*c",&chore);.
    ...
    scanf("%s%*c",&furniture);
    Get rid of the &s!

    Code:
    scanf("%*s%*c",food);
    Why the * before the s?

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Edit: Too slow
    Last edited by AndrewHunter; 08-10-2011 at 09:05 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    Sorry, I thought you said the &s weren't necessary, I didnt realise you meant they were causing a problem. I did experiment removing 1 or 2 but it didnt make any difference!

    I have now removed them all along with the extra * and....Your right it works!

    Sorry it took me so long to get that right but, Many thanks! ; )

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your scanf syntax is all messed up...
    Code:
    /* Get the words to use in the madlib */
    
    	printf("Enter an adjective:");		
    	scanf("%s%*c",&adjective);	 	
    	printf("Enter a food:");
    	scanf("%*s%*c",food);
    	printf("Enter a household chore (past tense):");
    	scanf("%s%*c",&chore);
    	printf("Enter an item of furniture:");
    	scanf("%s%*c",&furniture);
    Look up scanf() in your C library documentation and follow the guidelines...
    For example... scanf("%s%*c",&furniture); ... should be... scanf("%s",furniture);

    The & (address of) operator is not needed for strings, strings ARE pointers.
    The attempt to ignore trailing spaces is of no value here. If you have problems with it skipping inputs, use getchar() after each scanf().

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could have done what I told you to do in my first reply:
    Quote Originally Posted by quzah View Post
    Try adding a space before the % sign.
    Code:
    scanf( " %s", food );

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

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Or you could have done what I told you to do in my first reply:
    Quzah.
    I knew that worked with %c... good to know it works with %s too...

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CommonTater View Post
    I knew that worked with %c... good to know it works with %s too...
    (Ssshh: It doesn't, since %s already skips spaces at the beginning.)

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    (Ssshh: It doesn't
    It works fine with the space.
    Quote Originally Posted by tabstop View Post
    , since %s already skips spaces at the beginning.)
    So what you are saying is that he has no actual problem and he's making this all up? This works with our without the space:
    Code:
    #include<stdio.h>
    int main( void )
    {
        char words[4][ BUFSIZ ] = {{0}};
        int x;
        for( x = 0; x < 4; x++ )
        {
            printf( "enter a word, then hit enter: " );
            fflush( stdout );
            scanf( "%s", words[x] );
        }
        printf( "%s, %s, %s, %s\n", words[0], words[1], words[2], words[3] );
        return 0;
    }
    The space has no adverse effect.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  3. Help Needed
    By blackyute in forum C Programming
    Replies: 3
    Last Post: 02-16-2003, 08:12 PM
  4. Newbie: WM_CHAR help needed!
    By Morgan in forum Windows Programming
    Replies: 1
    Last Post: 01-02-2003, 10:31 AM
  5. Newbie needs C++ help with pauses- help needed badly.
    By dgprog in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2002, 05:11 PM