Dumb question

This is a discussion on Dumb question within the C Programming forums, part of the General Programming Boards category; Right,t his will eba real dumb qquestion to all you pros reading this, well hear it goes... Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    7

    Question Dumb question

    Right,t his will eba real dumb qquestion to all you pros reading this, well hear it goes...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
        char name;
        
        printf("Please enter your name... ");
        scanf("%c", &name);
        
        
        printf("\n\n");
        printf("Hello
        printf("%c", name);
        printf(", How are you today??? \n\n\n");
        
    }
    Basically, when i compile and execute all i get printed on the screen is the first letter of the defined name. Whats going on, i fuguered its something to do with only putting it down as a "char", is there anything to define an entire word, like string or something??

    Any ideas..... dragon2309

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    A string in C is an array of chars that ends with a 0 value ('\0'). So you could do something like char name[50];. Getting the name from the user is a bit different too. You can use scanf() with the %s specifier, but it will only get one word at a time. Another option is to use fgets().
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    What you are looking for is a char array. And scanf() is not a good way to read a string. Go for fgets() instead.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    ok, thanks, can anyone give any syntax or example structure of how to use fgets, or is it the same as scanf. thanks for the ultra quick replies, dragon2309

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    #define num 20
    char buffer[num];
    fgets( buffer,num,stdin );
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    
    {
        char name[10];
        
        printf("Please enter your name... ");
        fgets(name, 9, stdin);
        
        // Removes the Newline character from the string.
        for (int val = 0; val < 10; val++ )
        {
        if ( name[val] == '\n' )
            name[val] = '\0';
        }
        
        printf("\n\n");
        printf("Hello %s! How are you today? \n\n\n", name);
       
    }
    Sent from my iPadŽ

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > fgets(name, 9, stdin);
    There is no need to subtract 1 from the size
    fgets( name, sizeof name, stdin );
    would be just fine
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    If it try to compile that code sample you provided it comes up with the compile errors shown below, any ideas....?

    http://img452.imageshack.us/img452/2091/error4xg.jpg

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    yeah, declare
    int i;
    at the start of the function rather than inside the loop itself.

    And you can copy/paste those messages you know, no need for big screen pictures.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Code:
    #include <stdio.h>
    #include <stdlib.h> /* why did you include this? */
    
    int main(void)
    
    {
        char name[10];
        
        printf("Please enter your name... ");
        fgets(name, 9, stdin); /* as said, fgets already reads up to one-less than the size */
        
        // Removes the Newline character from the string. /* C99 comment*/
        for (int val = 0; val < 10; val++ ) /* C99 declaration */
        {
        if ( name[val] == '\n' )
            name[val] = '\0'; /* there are easier ways to remove the \n, also you should break here as looping past '\0' is pointless */
        }
        
        printf("\n\n");
        printf("Hello %s! How are you today? \n\n\n", name);
       /* forgot return 0; ?? */
    }
    Not trying to be rude, just letting you know.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    5
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char name[25];                      /* where the name is stored */
    
        printf("Please enter you name: ");  /* ask user to input name */
        fgets(name, sizeof(name), stdin);   /* gets user input */
        
        name[strlen (name) -1] = '\0';      /* get rid of new line */
        
        printf("\n\n");                     /* info for user */
        printf("Hello %s!\n\n", name);      /* info for user */
        
        getchar();                          /* stops it casue your using dev c++ */   
    }

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Quote Originally Posted by nonpuz
    Code:
    #include <stdio.h>
    #include <stdlib.h> /* why did you include this? */
                                   /* Because I copied his code, and didn't bother to remove it */
    
    int main(void)
    
    {
        char name[10];
        
        printf("Please enter your name... ");
        fgets(name, 9, stdin); /* as said, fgets already reads up to one-less than the size */
                                           /* As said, this  has been said. */
    
        // Removes the Newline character from the string. /* C99 comment*/
      /* As has this */
        for (int val = 0; val < 10; val++ ) /* C99 declaration */ /* ...and this in the error posted */
        {
        if ( name[val] == '\n' )
            name[val] = '\0'; /* there are easier ways to remove the \n, also you should break here as looping past '\0' is pointless */
        } /* Really, it can't get much easier than this. Today's computers take what, 100th of a nano second to do this? */
        
        printf("\n\n");
        printf("Hello %s! How are you today? \n\n\n", name);
       /* forgot return 0; ?? */ /* So did he. It still works. */
    }
    Not trying to be rude, just letting you know.
    The only truly rude thing about this was that none of it was new information.
    Sent from my iPadŽ

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    wow, none of that makes any sense but thanks anyway. ive been learning C for about 4 weeks and i really know nothing. God damn, i have to pass an A-Level in this..... :'-(

  15. #15
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Quote Originally Posted by SlyMaelstrom
    The only truly rude thing about this was that none of it was new information.
    Indeed, I was posting it more for the OPs benefit, than yours.

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

Similar Threads

  1. dumb question
    By travis999 in forum C Programming
    Replies: 3
    Last Post: 10-26-2007, 12:57 AM
  2. very dumb question.
    By Blips in forum C++ Programming
    Replies: 14
    Last Post: 11-08-2005, 08:37 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Sorry for a dumb question.
    By Vanished in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2002, 11:39 AM
  5. another dumb question - loop not working?
    By Captain Penguin in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2002, 10:15 PM

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