hi there.. new in c++ and Im currently working on a program that calculates ages (years and months). I declared all variables to int. and i use looping when the input year and month is not valid. Like this (just part of the program):
/*=====================================================================
Validation of Today's Year
======================================================================/*/
if (yearNow<1000 || yearNow>99999)
{
cout<<"\n\nPlease Indicate Proper year "<<endl;
system("pause");
cout<<endl;
goto askYearNow;
}
now everything seems fine but i noticed one flaw that makes my program wrong. once i input a character the program doesn't loop anymore it just stay there doing nothing..
is there anyway i can force the inputed data type to int?
here is my overall code.. used..
#include<iostream>
#include<cctype>
using namespace std;
/*========================================================================
Variable User Inputs
=========================================================================/*/
int yearCalculated(int yearNow, int yourYear);
int monthsCalculated(int yourMonth, int montNow);
char y,Y,N,n,ask;
int main()
{
int yearNow,monthNow,yourMonth ,yourYear, ageyear,agemonth;
/*========================================================================
Validation of Today's Year
=========================================================================/*/
if (yearNow<1000 || yearNow>9999)
{
cout<<"\n\nPlease Indicate Proper year ";
cout<<endl;
goto askYearNow;
askYearofBirth:
cout<<"\n\nEnter 4 digit year of your birth: ";
cin>>yourYear;
/**=======================================================================
Validation of Your Birthday's Year
=========================================================================/*/
if (yourYear<1000 || yourYear>99999)
{
cout<<"\n\nPlease Indicate Proper Year of Birth ";
cout<<endl;
goto askYearofBirth;
}
askMonthofBirth:
cout<<"\nEnter the month no. of your birth: ";
cin>>yourMonth;
/**========================================================================
Validation of your Birthday's Month
=======================================================================/*/
if (monthNow<=0 || monthNow>13)
{
cout<<"\n\n\nYou are now " <<ageyear<< " years and " <<abs(agemonth)<< " months old " <<endl;
cout<<"\nDo you want to enter another data? [y/n]: ";
cin>>ask;
}
while(toupper(ask)=='Y');
system("cls");
cout<<" \n\n\n Thank you ... God Bless ";
/*Closing Pause
/*/
cin.get();
cin.get();
return 0;
}
/**
=================================================================
Function's Section..... To be called
=================================================================
/**/
int yearCalculated(int yearNow, int yourYear)
{
int ageyear= yearNow-yourYear;
return (ageyear);
}
int monthsCalculated(int yourMonth, int monthNow)
{
int agemonth=monthNow-yourMonth;
if(monthNow<yourMonth)
{agemonth=(monthNow+12) - yourMonth;
}
return (agemonth);
}
yeah for one code tags would help a billion. two, goto loops are generally bad practice as the different names of the loops can get jumbled up. try working those into while or do-while loops and then if it's still not working repost your code. also to be entirely honest im not 100% clear on what youre trying to do