[C++] Allowing Only Integer Input in Win32 Console App.

Only integers can be input when the app. asks for user's DOB.

Page 1 of 1

3 Replies - 7017 Views - Last Post: 13 May 2008 - 11:45 AM Rate Topic: -----

#1 CalumJR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 10-May 08

[C++] Allowing Only Integer Input in Win32 Console App.

Post icon  Posted 12 May 2008 - 09:32 PM

I'm creating a simple Win32 console application in C++, in order to help me with my revision and overall understanding of C++ basics. I am going to try & incorporate as many C++ functions (arrays, pointers, etc) as possible, so my knowledge increases.

I am currently coding the first bit where the program will only allow people who are 18 to continue to use it. I have written the code, with a little help from another thread, to ask the user to input their age, calculate it and display the correct message to the user; however, when the user enters a symbol or other character, the program functions incorrectly (as is expected).

I would like to be able to have it so if the user enters a symbol or character, nothing will happen & it will display a message to the user telling them ti enter a valid integer.

Here is the important part of the code I have at the moment & it is the last "else" statement which I tried to see if that would work & it doesn't :wacko: lol.

//USER INPUT OF BIRTHDATE:
	SelectTextColour( clDarkCyan);
	SelectBackColour( clBlack);
	Gotoxy(21, 10);
	cout << "Please input the date of your birth ";  
	Gotoxy(35, 8);
	SelectTextColour( clGrey);
	cin  >> birthDate; 
	Gotoxy(37, 8);
	SelectTextColour( clMagenta);
	cout << "/";
	Gotoxy(21, 10);
	SelectTextColour( clDarkCyan);
	cout << "Please input the month of your birth ";  
	Gotoxy(38, 8);
	SelectTextColour( clGrey);
	cin  >> birthMonth; 
	Gotoxy(40, 8);
	SelectTextColour( clMagenta);
	cout << "/";
	Gotoxy(21, 10);   
	SelectTextColour( clDarkCyan);
	cout << "Please input the year of your birth ";  
	Gotoxy(41, 8);
	SelectTextColour( clGrey);
	cin  >> birthYear;  
   

	//CALCULATING AND DISPLAYING THE USER'S AGE: 
	if (birthMonth >= 5) 
		userAge  = currentYear - birthYear - 1;
	else
		userAge = currentYear - birthYear;

	Gotoxy(21, 10);
	cout << "												 "; 
	SelectTextColour( clMagenta);
	Gotoxy(23, 10);
	cout << "You are currently " << userAge << " years of age.";  
  

	//INFORMING THE USER WHETHER THEY ARE OLD ENOUGH OR NOT
	if (userAge < 18)
		{
			Gotoxy(10, 15);
			SelectTextColour( clGrey);
			SelectBackColour( clDarkRed);
			cout << "You are underage, as you have to be 18 to use this program.";
			Gotoxy(28, 16);
			cout << "Please do not continue.";
			Gotoxy(10, 19);
			cout << "The program will now exit when you press any keyboard button.";
			
			while (! _kbhit());

		}
				else if (userAge >= 18) 
				{
					Gotoxy(20, 15);
					SelectTextColour( clGrey);
					SelectBackColour( clDarkGreen);
					cout << "You are old enough to use this program.";
					Gotoxy(32, 16);
					cout << "Please continue";
		
					while (! _kbhit());
		}

				else
				{
					Gotoxy(28, 15);
					SelectTextColour( clGrey);
					SelectBackColour( clDarkRed);
					cout << "Please enter a";
					Gotoxy(28, 16);
					cout << "valid integer.";

					while (! _kbhit());
				}
	




This is how I have declared my variables in the program:


int birthDate, birthMonth, birthYear;				 //the user's date of birth
	int userAge;										//the user's current age 




Can anybody help me please and guide me in the direction of what I am doing wrong?

Also, is it possible to only allow the input of valid dates/months/years. (E.g. the user can only input the integers '1' - '12' when asked to insert the month of their birth and integers '1900 - 2008' when asked about thier year of birth)?

Thank you in advance :)
cJr.




----------------
Now playing on iTunes: EMINEM - Guilty Consience (Feat. Dr. Dre)
via FoxyTunes

Is This A Good Question/Topic? 0
  • +

Replies To: [C++] Allowing Only Integer Input in Win32 Console App.

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3122
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: [C++] Allowing Only Integer Input in Win32 Console App.

Posted 12 May 2008 - 10:50 PM

A while loop would clear this up. Have it loop while the numbers are bigger or smaller then 1-12 and if the response is not an integer.
Was This Post Helpful? 0
  • +
  • -

#3 CalumJR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 10-May 08

Re: [C++] Allowing Only Integer Input in Win32 Console App.

Posted 13 May 2008 - 06:07 AM

Right I see what you mean. Thank you very much.

It's just I don't understand how to say "the number is not an inetger" e.g. this won't work:

birthMonth != int


& I can't think of anything else that would work in the while loop?
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1385
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: [C++] Allowing Only Integer Input in Win32 Console App.

Posted 13 May 2008 - 11:45 AM

You could use the isdigit(var) function to determine whether it is an int value or not :)
Was This Post Helpful? 1

Page 1 of 1