Thread: Help- I'm new to this

  1. #1
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20

    Help- I'm new to this

    Hi, I just started teaching myself C programming.(today)
    So I'm very new and never learned a language before.
    I have several video tutorials that tought the basics.
    But I'm having some trouble.
    My goal-
    Make a simple program that can tell:
    1. How old someone half my age would be.
    2. How many years its been since the age of 10.
    3. What year was I born

    I want to start off by asking the age of the user using the program
    so this is how I started.
    Please explain why it doesn't work lol thanks in advanced

    Code:
    #include <stdio.h>
    
    main()
    {
          int    age; // How old the user is
          int    x; //How old someone half my age would be.
          int    y; //How many years its been since the age of 10
          int    z; // What year was I born in.
          
          printf("How old are you? ");
          scanf("%d", age);
          x = age / 2;
          y = age - 10;
          z = 2008 - age;
          printf("You are %d years old. It's been %d since you were 10. And you were born in %d\n", age, y, z);
          fflush(stdin);
          getchar();
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by LuizCPFL View Post
    Hi, I just started teaching myself C programming.(today)
    So I'm very new and never learned a language before.
    I have several video tutorials that tought the basics.
    But I'm having some trouble.
    My goal-
    Make a simple program that can tell:
    1. How old someone half my age would be.
    2. How many years its been since the age of 10.
    3. What year was I born

    I want to start off by asking the age of the user using the program
    so this is how I started.
    Please explain why it doesn't work lol thanks in advanced

    Code:
    #include <stdio.h>
    
    int main()
    {
          int    age; // How old the user is
          int    x; //How old someone half my age would be.
          int    y; //How many years its been since the age of 10
          int    z; // What year was I born in.
          
          printf("How old are you? ");
          scanf("&#37;d", &age);
          x = age / 2;
          y = age - 10;
          z = 2008 - age;
          printf("You are %d years old. It's been %d since you were 10. And you were born in %d\n", age, y, z);
          fflush(stdin);  //flushing an input stream is ill advised, now. Only output streams
          getchar();
          return 0;  
    }
    scanf() needs an address, main should be int main, with a return of zero in C, and input streams should
    no longer be flushed - only output streams.

    It's very helpful if you name your variables something meaningful - halfAge maybe, instead of x,
    minus10 instead of y, yearMinusAge maybe, instead of z.

    In a simple and short program like this, it's no problem of course. When the programs get long, and maybe you haven't seen the code in a year and now want to add something to it - THEN you'll be very glad you used descriptive variable names.

    And welcome to the forum!
    Last edited by Adak; 12-08-2008 at 12:16 AM.

  3. #3
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    Wow thanks

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, don't use implicit main: http://cpwiki.sourceforge.net/Implicit_main
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Also, realize that on computers, integer division is not the same math you learned in school.

    In school, you learned 5/2 = 2.5

    However, with integer division, 5/2 = 2, as the remainder is thrown away.

    If you want fractions, use the "double" data type and it's formatting controls in printf / scanf.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    What's another way of writing this script without the:

    Code:
    fflush(stdin);

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Like this:
    Code:
    
    
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    Quote Originally Posted by Dino View Post
    Like this:
    Code:
    
    
    What you mean lol you didn't put anything there.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Right, just don't use the flush of stdin!
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    Well when I try to run it without it and I enter my number the program closes.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Do you mean your window that opens, when you run the program, closes right after you enter your value?
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    Yes that's exactly what I mean.

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Use getchar() to pause the window closing.
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    How I do that?
    getchar() is already on there.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Looks like a "segmentation fault" to me.
    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

Popular pages Recent additions subscribe to a feed