I am reading and learning from "Programming Principles and Practice Using C++" by Bjarne Stroustrup. This is supposed to be a beginner book for someone with 0 experience. I got the first chapter completed which was building hello, world program. Now we are moving into operators and objects. I then successfully created the name and age program with which is this:
int main()
{
cout<<"Please enter your first name and age\n";
string first_name;
int age;
cin>>first_name;
cin>>age;
cout<<"Hello,"<<first_name<<"(age"<<age<<")\n";
}
This of course produces for example "Hello,kyle(age18)"
The practice even though operators haven't been thoroughly explained yet and there's not practice code or examples to go off of, the book has a practice which wants me to modify this code to write out the age in months by reading the input in years and multiplying by 12 using "*" Then it wants me to change the integer into a double to allow a number like 5.5 (this part makes sense).
This is the code I have came up with and I'm not sure why it's not doing what I'm suppose to do????
#include "std_lib_facilities.h"
int main() //C++ programs start by exeuting the function main
{
cout<<"Please enter your first name and age\n";
string first_name;
cin>>first_name
double age;
cin>>age;
double months=12;
cin>>months;
double age_in_months=age*months;
cin>>age_in_months;
cout<<"Hello,"<<first_name<<" (age "<<age_in_months<<")\n";
keep_window_open();
}
First problem is if I put kyle 18 it will just put "Hello,Kyle(age18) and not multiply which is confusing me but also if i try to put kyle 8.5 it closes really quick so i cant see anything, why would it be doing this even though its a double and not an integer?? Please help! I don't want to move on with the book until i can undertsand this part fully!???
Hi, you have already calculate the result of age_in_months in the line "double age_in_months=age*months;", so you don't need the input line "cin>>age_in_months;".
----Fix the bug by removing this line.
#include "std_lib_facilities.h"
int main() //C++ programs start by exeuting the function main
{
// read the name as a string
cout << "Please enter your first name\n";
string first_name;
cin >> first_name ;
// read the age in years as a double
cout << "Please enter your age in years (decimals are allowed)\n";
double age_in_years ;
cin >> age_in_years ;
// by now, we have read in all the information that we need
// to compute the age in months
constdouble months_per_year = 12 ;
// we don't need the user to tell us that, we know that a year has 12 months
// cin>>months;
// we don't need the user to tell us that either, we can compute it
// cin>>age_in_months;
// compute the age in months
constdouble age_in_months = age_in_years * months_per_year ;
// and print it out
cout<<"Hello, " << first_name << " (aged " << age_in_months << " months)\n";
keep_window_open();
}
Thanks alot JLBorges! Got it to work then I reviewed the code after I got the program running to understand exactly what I was doing wrong. I wasn't understanding the "cin" part and I was had a clue of multiplying but couldnt get it. Thanks a lot now I can move on :)