Operators, Variables, Objects

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?
Nevermind folks!

It was reading the wrong source file, thus, none of my new code was being read.

Silly beginner mistake!
closed account (48T7M4Gy)
Your calculation for month_age needs to come after line 10

you need to #include <iostream>

Then tell us what errors you get :)
Thank you for redirecting my math, I was confused on that part! Math comes after input, check.

Stroustrup's book provides a header file that incoporates all the #include stuff to make learning easier. Thus why the std_lib_facilities.h was included. Either way, after fixing the mix up with the source file, I managed to improve it, and complete the exercise with no errors. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Objects, Types, and Values Learning.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "std_lib_facilities.h"

int main()
{
	cout << "Please enter your first name, then hit enter. \n";
	string first_name; // First name is the name of the variable we are expecting.
	double age; // Our age
	double months_age;
	cin >> first_name;
	cout << "Hello, " << first_name << " How old are you? \n";
	cin >> age; //read the characters
	months_age = age * 12;
	cout << age << "?" << ' ' << first_name << " You are, " << months_age << " Months old! \n";
	keep_window_open();
	return 0;
}
Last edited on
closed account (48T7M4Gy)
Well done.

The point about putting it on the right line is the machine hasn't got a clue what your age is until after the cin >> age
Last edited on
Topic archived. No new replies allowed.