Thread: new to programming. help with very simple program

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    2

    new to programming. help with very simple program

    I'm new to programming and just learning the basics. Below I have something to calculate a person's age based on their input using scanf. I don't know if I'm doing something wrong or if codeblocks isn't working. I put in any any number and I get back an answer of -82


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
       int age;
       int currentYear;
       int birthYear;
    
    
       age = currentYear - birthYear;
    
    
       printf("what year is it?\n");
       scanf("%d", &currentYear);
       printf("what year were you born?\n");
       scanf("%d", &birthYear);
       printf("you are %d years old\n", age);
    
    
       return 0;
    }

  2. #2
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by mehmin View Post
    I'm new to programming and just learning the basics. Below I have something to calculate a person's age based on their input using scanf. I don't know if I'm doing something wrong or if codeblocks isn't working. I put in any any number and I get back an answer of -82


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
       int age;
       int currentYear;
       int birthYear;
    
    
       age = currentYear - birthYear;
    
    
       printf("what year is it?\n");
       scanf("%d", ¤tYear);
       printf("what year were you born?\n");
       scanf("%d", &birthYear);
       printf("you are %d years old\n", age);
    
    
       return 0;
    }
    Shouldn't you be calculating age after you get the input from the user, not before you get the input from the user?

    The way you wrote the program, there are no values associated with year and birthYear when you calculate age, so the compiler will calculate both of those variables as NULL, and produce a strange output for the age variable. If you calculate age after you get the input from the user, then year and birthYear will have values associated to those variables, and it will calculate age correctly.
    Last edited by Terrance; 03-11-2017 at 06:46 PM.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    2
    Thank you very much for the direction. I suppose I have to think about the process in a more procedural way.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
       system("color 1f");
    
    
    
    
       int currentYear;
       int birthYear;
       int age;
    
    
    
    
    
    
       printf("what year is it?\n");
       scanf("%d", &currentYear);
       printf("what year were you born?\n");
       scanf("%d", &birthYear);
       age = currentYear-birthYear;
       printf("you are %d years old\n", age);
    
    
    
    
       return 0;
    }

  4. #4
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287
    Quote Originally Posted by mehmin View Post
    Thank you very much for the direction. I suppose I have to think about the process in a more procedural way.

    Yes, remember that the values of variables must be stored in memory before any calculations can be made, otherwise the values will be null if nothing is stored in them. Getting input from the user is what gets the values and stores those values in the variables. You have to look at your code as performing one instruction or procedure at a time, so they have to be in order (where your code was out of order).
    Last edited by Terrance; 03-11-2017 at 07:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to Programming - Simple Math Program Issue
    By Cameron Taylor in forum C Programming
    Replies: 4
    Last Post: 06-10-2013, 09:32 PM
  2. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  3. simple shared memory program on c programming
    By kattythebest in forum C Programming
    Replies: 1
    Last Post: 09-09-2009, 02:39 AM
  4. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  5. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM

Tags for this Thread