Pls help. looping problem..!

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):

askYearNow:
system("cls");
cout<<"\n\nEnter today's 4-digit year: ";
cin>>yearNow;

/*=====================================================================
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;



do{
askYearNow:

cout<<"\n\nEnter today's 4-digit year: ";
cin>>yearNow;

/*========================================================================
Validation of Today's Year
=========================================================================/*/

if (yearNow<1000 || yearNow>9999)
{

cout<<"\n\nPlease Indicate Proper year ";
cout<<endl;
goto askYearNow;

}

askMonthNow:
cout<<"\nEnter today's month number: ";
cin>>monthNow;
/*=======================================================================
Validation of Today's Month
========================================================================/*/
if (monthNow<=0 || monthNow>13)
{
cout<<"\n\nPlease Indicate Proper Month no. " ;
cout<<endl;
goto askMonthNow;
}


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\nPlease Indicate Proper Month of Birth ";
cout<<endl;
goto askMonthofBirth;
}


ageyear= yearCalculated(yearNow, yourYear);
agemonth=monthsCalculated(yourMonth, monthNow);


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);
}
Last edited on
For everyones sake please make use of the [ code ] [ /code ] tags provided.

Just a look @ this made me wonder why not use a while loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout<<"\n\nEnter today's 4-digit year: ";
cin>>yearNow;

/*========================================================================
Validation of Today's Year
=========================================================================/*/

if (yearNow<1000 || yearNow>9999)
{

cout<<"\n\nPlease Indicate Proper year ";
cout<<endl;
goto askYearNow;

}

i.e.
1
2
3
4
5
6
7
8
9
cout<<"\n\nEnter today's 4-digit year: ";
cin>>yearNow;
while (yearNow < 1000 || yearNow >9999)
{
 cout<<"\n\nPlease Indicate Proper year ";
 cout<<endl;
cin >> yearNow;

}
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
Topic archived. No new replies allowed.