What did i do wrong? i can't output name and year.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
cout << "Please enter your first name and age\n";
string name;
int month = 12;
double year = year*month;
cin >> name >> year;
cout << "Hello " << name << "(age " << year << ")\n";
}
I think the problem is that you are multiplying the year variable to the month as you are defining it. I believe it is compiler/system dependent on what value an uninitialized variable has, so year could be anything when you multiply it to month.
You are then using cin to read the year from the user and overwriting the random value you already assigned to year.
You need to break apart your definition, and assignment of the year variable.
1 2 3 4 5
int month = 12;
double year; //define it here
cin >> name >> year; //set it here
year = year * month; //change it here
I'm not sure that makes sense either though. You are prompting the user for their age, but storing the value in a variable called "year". If I put '30' into that variable, 30*12 = 360... If you are looking to show me how many months old I am, that's fine, but I think I'm a little confused as to what you are attempting to do.
If you want the year the user was born, do something like this:
1 2 3 4
int currentYear = 2014;
int age;
cin >> name >> age;
int birthYear = currentYear - age;