I'm just beginning to learn C++, and in the book I'm using as a guide, it asks me--as an exercise-- to modify the code that I've been working with to multiply an input age by 12. However, no matter what I do, I can't seem to get it to multiply.
Initially, this was the program which I was working with.
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include "stdafx.h"
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter your first name and age, then hit enter. \n";
string first_name; // First name is the name of the variable we are expecting.
int age; // Our age
cin >> first_name >> age; //read the characters
cout << "Hello, " << first_name << ' ' << age << '\n';
keep_window_open();
return 0;
}
|
The way the book described it, I should have been able to just place a multiplication operator after age, either in the definition of the variable, the input of the information, or the output. However, if I put "22" in, it always comes out as 22. So I had the idea that maybe I could define a new variable called "month_age" which would display the age in months, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include "stdafx.h"
#include "std_lib_facilities.h"
int main()
{
cout << "Please enter your first name and age, then hit enter. \n";
string first_name; // First name is the name of the variable we are expecting.
int age; // Our age
double month_age = age * 12;
cin >> first_name >> age; //read the characters
cout << "Hello, " << first_name << ' ' << month_age << '\n';
keep_window_open();
return 0;
}
|
However, this failed to work too. What am I doing wrong?