Year Born Program

I am trying to write a basic program where the user inputs their name and age and then the program will output the year they were born but my program won't let me input an age...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  // Year Born

#include <cstdio>
#include <cstdlib>
#include <iostream> 

using namespace std;

int main()
{
    int nName;
    int nAge;
    int nDate;
    nDate= 2014 - nAge;
    
    //Ask for name
    cout << "Enter your name: ";
    cin >> nName;
    
    //Ask for Age
    cout << "Enter your age: ";
    cin >> nAge;
    
    //Tell person what year they were born in
    cout << "Hello " << nName << " you were born in " << nDate << endl;
    
    system ("pause");
    return 0;
    
    
}
Line 11: Who do you know that has an integer for a name?

Line 14: You can't calculate the nDate before nAge has been input. nAge contains garbage at this point.
Silly me! Thank you for your help its fixed!
Hi,

First, a golden rule for programming: Always initialise your variables

I like to do this as part of the declaration.

What is the value of nAge on line 14? I am picking quite a large number, maybe even negative. Should line 14 be moved to elsewhere in your program?

Think about the types of your variables - is int a good type for someone's name? For the others consider making them unsigned.

With your variable names:

Does the n in front of the variable name mean number? If so, don't bother with that. It is a form of Hungarian Notation which is generally bad because it can be overdone and confusing, unless used for it's original purpose (which is rather different form the way it was abused by Microsoft, and different to what a lot folks think it is). See the article below.

Having said that I put prefixes on several things (and limit it to that): C for class ; p for pointer ; and m_ for class member variables.

Don't be afraid to make the variable name a bit longer if it improves it's meaning. For example BirthDate is better than Date IMO.

Good variable names help self document code, and comments are good for describing what functions do, expected ranges of values, expected output values etc.

Don't #include header files you don't need.

Finally don't have line 7, put std:: before each std thing - Google to see why that is so. I wish Bjarne Stroustrup and other authors and lecturers would quit doing it.

With this article:
http://www.joelonsoftware.com/articles/Wrong.html

which describes the original intention of HN, the second part entitled "I’m Hungary", describes how it all went wrong.

I still have a problem with the use of us as a prefix. To me that immediately conjures unsigned to others it might mean "United States". IMO it would be ok if it were the prefix Unsafe. Another is sz, to me this is an obvious acronym for "size", not "zero terminated string".

I do think the concept of Safe and UnSafe strings is a good one though.

The stdint.h has types in it that refer to specific widths in bits and sign:

int8_t
int16_t
int32_t
uint8_t
uint16_t
uint32_t


http://en.wikipedia.org/wiki/Hungarian_notation
In the OP`s case, I think
using namespace std
is tolerable as he is still learning. Later when he uses external libraries, he maybe should declare the namespace before each statement.
Please do not use system("PAUSE") in future programs
Refer to console closing down for more info. (or google it)
@Code Apprentice

Sure, that's fine. At least the idea has been mentioned : I think it is worth mentioning as early as possible.

Cheers :+)
Topic archived. No new replies allowed.