Thread: Please help me out

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    22

    Please help me out

    Hey everyone. This is my first day of learning C. So far I have just learned how to input numbers and have them multiplied/subtracted/divided/added. I've made a few tiny programs to help learn, but I've come across a problem. I want to make a program that will ask you for your name, you enter it, and then it just displays it again. Here is the code I would use:
    Code:
    #include <stdio.h>
    int awesome_name;
    main()
    {
    printf("Please enter your name: ");
    scanf("%d", &awesome_name);
    printf("Your name is %d", awesome_name);
    return 0;
    }
    When the program with that source runs, it says Please enter your name:. I input it then press enter but then it comes up and says Your name is 0. What is up with that? Does C even allow you to use text like that? Is there a certain code I have to use?

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    Quote Originally Posted by CougarElite
    Code:
    #include <stdio.h>
    int awesome_name;
    main()
    {
    printf("Please enter your name: ");
    scanf("%d", &awesome_name);
    printf("Your name is %d", awesome_name);
    return 0;
    }
    When the program with that source runs, it says Please enter your name:. I input it then press enter but then it comes up and says Your name is 0. What is up with that? Does C even allow you to use text like that? Is there a certain code I have to use?

    At the point where you say
    Code:
    int awesome_name;
    the compiler takes a few bytes (in almost cases 4) and he will use them as number-space. They will allways be interpreted like a number.
    So if you want to store a name (a string) you would need to change this to something like "char awesome_name[x]" where x can be any number and stands for the amount of characters in the string.
    Then you need to tell scanf to read in a string instead of a number by changing to
    Code:
    scanf("%1234s", &awesome_name);
    where 1234 is the length x you chose for the string.
    And finally you need to tell printf to put out a string also:
    Code:
    printf("Your name is %s", awesome_name);
    so far, Chris

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    Hmm does anyone have enough time to make a small example of a program that would ask for your name then print it? Like the correct way to make the one I wanted to make.

    If so, thanks alot.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    Just exchange the lines i posted or have a look at the documentation.
    Can you compile the code on your computer now ?
    Then do it and post what goes wrong along with the compiler output.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Hmm does anyone have enough time to make a small example of a program
    We're not here just to simply spoon feed you answers on demand, because that doesn't help anyone in the long run.

    It's about helping you to figure out the answers to your own questions, by pointing you in the right direction, explaining things which you might not understand, helping you understand mistakes in what you've done already (exactly what chris78 did). These are things which will make you a better programmer in the future.
    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.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > Hmm does anyone have enough time to make a small example of a program
    Yet one more person who doesn't bother reading people's posts / the FAQ link provided!.

    In other words:
    I'm too god damn lazy to even click one link to see an example already made for me.
    Go ahead. Add to my reputation, see if I care.

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

  8. #8
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    the program that you must use is the next one: (his/her/its must function perfectly on c and c++)
    #include " stdio.h "
    #include " conio.h "
    #define n 1000
    void hand ()
    {
    chariot nom[n] = {'. ', '. ', '. ', '. ', '. ', '. ', '. ', '. ', '. '} / * it is necessary to repeat points * /
    printf (" enter your nom\n\33 ");
    scanf (" %c",&nom []);
    printf (" your name is:\t ");
    for(i=0;i <n/*long chain * /;i++)
    {
    printf (" %c",nom[i]);
    }
    printf (" \nthanks for the collaboration\n ");
    getchar ();
    }

  9. #9
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    i'msorry about grammatical mistakes
    because i have using a translator

  10. #10
    Welcome to the real world
    Join Date
    Feb 2004
    Posts
    50
    I'll bet you all the money in Tunisia that you didn't even try to compile the project that was given to you (the requirement was worded as if it were a homework assignment).

    1. I hope your using DOS (conio.h).
    2. You never declare ( nom legally.
    3. int i is never declared
    4. You're using nom, in scanf, inappropriately
    5. There is no main function: int main (params need, if any) {

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    This is the new code I tried:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    char awesome_name[20];
    
    main()
    {
    printf("Enter your name: ");
    scanf("%20s", &awesome_name);
    printf("Your name is %s", awesome_name);
    getch();
    return 0;
    }
    My compiler says line 9: & non lvalue 'scanf("%20s", awesome_name);

    Any ideas on this one?

  12. #12
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    When you do input with strings (a char array is a string) the name of the array, in this case awesome_name points to the memory that holds the string. So unlike other variables, you don't need the & before the string name.
    Code:
    scandf("%20s", awesome_name);

  13. #13
    Registered User
    Join Date
    Apr 2004
    Posts
    22
    Thanks a lot Rouss! I now have a program that says your name! Lol thanks a lot everyone! I'll be back with more questions in the future (probably the near future).

  14. #14
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    sorry about mistakes in the program

  15. #15
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    Quote Originally Posted by Rouss
    When you do input with strings (a char array is a string) the name of the array, in this case awesome_name points to the memory that holds the string. So unlike other variables, you don't need the & before the string name.
    Code:
    scandf("%20s", awesome_name);
    Argh. Yes this one again.
    @CougarElite sorry for the faulty advice
    Isnt that with any kind of array ?
    I just need to start thinking of arrays are nonexistent there are only pointers with pre-allocated memory ot them. Maybe that keeps me from doing this.

Popular pages Recent additions subscribe to a feed