jeudi 28 septembre 2017

c++ program does not run completely (if-else statements)

I'm trying to run a C++ program. The first part of it is to calculate someone's age. There are different cout's for different age ranges. But for some reason the program stops after calculating the age in years and doesn't continue with the rest. I want it to calculate the age in months too. When I try to run it in the terminal, it does 2017-birthday. But after that nothing else appears. I think there's a problem with the way I put all the if-else statements for the part of age in years, but I'm not sure. The date is set as 25 September 2017, hence I put 9 minus birthmonth.

#include <iostream>
using namespace std;

int main()
{
int birthday,birthmonth;
int ageinyears;

// birthyear     
cout << "What is your birthyear? ";
cin >> birthyear;
if (birthyear <= 2017) 
{
  cout << "Your age in years is "
       << 2017-birthyear << endl;
  cin >> ageinyears;
  if (ageinyears < 10)
  {
  cout << "You are too young for university." << endl;
  return 0;
  }

  else if (ageinyears > 100)
  {
  cout << "You are too old for university." << endl;
  return 0;
  }

  else 
  cout << "Continue with the next question.";

}
else
  cout << "The year you filled in is invalid. Try again." << endl;
  return 0;

// birthmonth   
cout << "Fill in your birthmonth as numbers.";
cin >> birthmonth;
if (0 < birthmonth && birthmonth < 13)
  cout << "Your age in months is "
       << ((ageinyears * 12) + (9 - birthmonth)) << endl;
else 
  cout << "The month you filled in is invalid. Try again.";
  return 37;

}//main

Aucun commentaire:

Enregistrer un commentaire