I started learning C++ today and I'm having a tiny bit of trouble.. I'm trying to make a simple program that takes the users age, asks them to enter a number that they would like to increase their age by then outputs the sum of those two numbers.. here it is:
#include <iostream>
int getAge()
{
using std::cin;
using std::cout;
using std::endl;
cout << "Enter your age: ";
int age;
cin >> age;
cout << endl;
cout << "You are " << age << " years old.";
cout << endl;
return age;
}
int getYearsFromNow()
{
using std::cin;
using std::cout;
using std::endl;
cout << endl;
cout << "Enter how many years you want to increase yours age by: ";
int yearsFN;
cin >> yearsFN;
cout << endl << "Increasing your age by " << yearsFN << " years...";
return yearsFN;
}
int main()
{
using std::cout;
using std::endl;
getAge();
getYearsFromNow();
/*int newAge;
newAge = getAge() + getYearsFromNow();
cout << endl << "In " << getYearsFromNow() << " years from now, you will be
" << newAge; */
return 0;
}
I have the last part of the main function commented out for testing purposes.. when they are uncommented out, the compiler executes the two calls in the main function (getAge() and getYearsFromNow()), then does it again, and one more time, and only then does it perform the rest of the code..
I don‘t understand.. I had the last part in a separate function that just returned the variable 'newAge' but that worked out the same..
Aucun commentaire:
Enregistrer un commentaire