Help on restricting input to a numbers only (or text only)

This is a discussion on Help on restricting input to a numbers only (or text only) within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I am a beginner at programming in C++, and I just recently finished (to an extent) one of ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    7

    Help on restricting input to a numbers only (or text only)

    Hello everyone. I am a beginner at programming in C++, and I just recently finished (to an extent) one of my first real large scale programs, a craps game. Now while playing the near finished version, I noticed that if I input a character when I'm supposed to input an integer, it spazzes out, and when I'm supposed to input a character and input an integer on accident, it also spazzes out.

    My question is how can I restrict a users input to numbers only or letters only. I'd post my source code, but it's only half commented. Thanks in advance to anyone who helps

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    My question is how can I restrict a users input to numbers only or letters only. I'd post my source code, but it's only half commented.
    Well, it would be good if you posted the smallest and simplest program that demontrates just this part of the program, or at least what you have tried so far.

    Still, on a hunch I can suggest that you look through and try this program:
    Code:
    #include <iostream>
    #include <limits>
    
    int main()
    {
    	int num;
    	std::cout << "Enter an integer: ";
    	while (!(std::cin >> num))
    	{
    		// reset the status of the stream
    		std::cin.clear();
    		// ignore remaining characters in the stream
    		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    		std::cout << "Enter an *integer*: ";
    	}
    	std::cout << "You entered: " << num << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Could that be switched round so that it only continues when the user inputs nothing?

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    Alright, here's a portion of the code I modified to run on it's own.
    Code:
    #include <iostream>
    #include <windows.h>
    #include "ShortColors.h"
    
    using namespace std;
    
    int main()
    {
    	int betloop;
    	DWORD Written;
    
    	do 
    	{
    
    		int money = 20;
    		int amtbet;
    
    		HANDLE hOut;
    		hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    		COORD Position;
    
    		SetConsoleTextAttribute(hOut, FBI);
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"~~~~~~~~~~~~~~~~~~ Test ~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"~~~ Money: $"<<money<<"       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Bet: $        ~~~\n";
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    
    		Position.X = 39;
    		Position.Y = 4;
    
    		SetConsoleCursorPosition(hOut, Position);
    		
    		cin>> amtbet;
    		cin.ignore();
    
    		if ( amtbet > money || amtbet < 1 )
    		{
    			betloop = 0;
    			cout<<"\n\nError\n";
    			cin.get();
    			Position.X = 0;
    			Position.Y = 0;
    			FillConsoleOutputCharacter(hOut, ' ', 5000, Position, &Written);
    			SetConsoleCursorPosition(hOut, Position);
    		}
    		else
    		{
    			betloop = 1;
    		}
    	} while ( betloop == 0 );
    	cout<<"\n\nPress enter to exit";
    	cin.get();
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Code:
    cin>> amtbet;
    That is the code you want to replace with laserlight's loop. The clear() and ignore() parts are the important parts of that loop. You can replace the "Enter an *integer*: " part with whatever error code you want.

    >> Could that be switched round so that it only continues when the user inputs nothing?
    No, since cin will wait until the user types something other than whitespace. You'd have to read in a string or use get() to check for the user hitting enter without typing anything, and you'd have to use platform specific functions to check for the user hitting nothing at all.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    Ok, thanks, I'll try it out and see if it works.

    Also, you wouldn't happen to have a link for how to implement the enter without typing anything on Windows? If not just let me know.

    Thanks a million for being so nice about things like this to newbies like us

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    Thanks you guys. After a little bit of experimenting with it, I was able to get it to do what I wanted.

    Now what do I need to switch if I want it to only read chars and not integers?

    EDIT: Well, I thought it worked. It works in the sense that it solves my input only numbers problem. But now, it won't show my errors for when the user inputs more than they have or less than a dollar.

    Here's the source for the bet() function:
    Code:
    void bet()	// bet() function
    {
    	do {	// start of Do While loop
    		clrscrn();	// Clears the screen
    		SetConsoleTextAttribute(hOut, FBI);	// Sets the text color to bright blue
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";	// ASCII graphics
    		cout<<"~~~~~~~~~ ";	// Ascii graphics
    		SetConsoleTextAttribute(hOut, FWI);	// Sets the text color to bright white
    		cout<<"How much would you like to bet? ";
    		SetConsoleTextAttribute(hOut, FBI);	// Sets the text color to bright blue
    		cout<<"~~~~~~~~\n";						//
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";	//
    		cout<<"~~~                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";	// Rest of ASCII graphics
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~               ~~~\n";	//
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";	//
    		Position.X = 4;
    		Position.Y = 3;
    		SetConsoleCursorPosition(hOut, Position);
    		SetConsoleTextAttribute(hOut, FGI);
    		cout<<"Money: $"<< money;
    		Position.X = 33;
    		Position.Y = 4;
    		SetConsoleCursorPosition(hOut, Position);
    		cout<<"Bet: $";
    		
    		while (!(std::cin >> amountbet))
    		{
    			// reset the status of the stream
    			std::cin.clear();
    			// ignore remaining characters in the stream
    			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    			Position.X = 0;
    			Position.Y = 6;
    			SetConsoleTextAttribute(hOut, FRI);
    			SetConsoleCursorPosition(hOut, Position);
    			std::cout << "\aYou must enter a number.\n";
    			Position.X = 39;
    			Position.Y = 4;
    			SetConsoleCursorPosition(hOut, Position);
    			cout<<" ";
    			SetConsoleCursorPosition(hOut, Position);
    			SetConsoleTextAttribute(hOut, FGI);
    		}
    
    		Position.X = 0;
    		Position.Y = 6;
    		SetConsoleCursorPosition(hOut, Position);
    		if ( amountbet > 0 && amountbet <= money )
    		{
    			betloop1 = 1;
    		}
    		else if ( amountbet <=0 )
    		{
    			betloop1 = 0;
    			SetConsoleTextAttribute(hOut, FRI);
    			cout<<"!!!!!!!!!! You must bet at least $0.01 !!!!!!!!!!!\n\n";
    			cout<<"Press enter to try again.\n";
    			cin.get();
    		}
    		else if ( amountbet > money )
    		{
    			betloop1 = 0;
    			SetConsoleTextAttribute(hOut, FRI);
    			cout<<"!!!!!!!!!! You don't have enough money !!!!!!!!!!!\n\n";
    			cout<<"Press enter to try again.\n";
    			cin.get();
    		}
    		else
    		{
    			error();
    		}
    	} while ( betloop1 !=1 );
    	do {
    		clrscrn();
    		SetConsoleTextAttribute(hOut, FBI);
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"~~~~~~~~~~ "; 
    		SetConsoleTextAttribute(hOut, FWI);
    		cout<<"What number (2-12) to bet on";
    		SetConsoleTextAttribute(hOut, FBI);
    		cout<<" ~~~~~~~~~~\n";
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"~~~                      ~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		Position.X = 4;
    		Position.Y = 3;
    		SetConsoleCursorPosition(hOut, Position);
    		SetConsoleTextAttribute(hOut, FCI);
    		cout<<"Number to bet on: ";
    
    		while (!(std::cin >> numberbet))
    		{
    			std::cin.clear();
    			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    			Position.X = 0;
    			Position.Y = 5;
    			SetConsoleCursorPosition(hOut, Position);
    			SetConsoleTextAttribute(hOut, FRI);
    			std::cout<<"\aYou must enter a number.\n";
    			std::cout<<"Please try again.";
    			Position.X = 22;
    			Position.Y = 3;
    			SetConsoleCursorPosition(hOut, Position);
    			std::cout<<" ";
    			SetConsoleCursorPosition(hOut, Position);
    			SetConsoleTextAttribute(hOut, FCI);
    		}
    
    		Position.X = 0;
    		Position.Y = 5;
    		SetConsoleCursorPosition(hOut, Position);
    		if ( numberbet >= 2 && numberbet <= 12 )
    		{
    			betloop2 = 1;
    		}
    		else if ( numberbet < 2 || numberbet > 12 )
    		{
    			betloop2 = 0;
    			SetConsoleTextAttribute(hOut, FRI);
    			cout<<"!!!!!!!!!!!! You can only choose 2-12 !!!!!!!!!!!!\n\n";
    			cout<<"Press enter to try again.\n";
    			cin.get();
    		}
    		else
    		{
    			error();
    		}
    	} while ( betloop2 != 1 );
    }
    The compiled program is attached for those who want to see what I'm talking about. Just change the .cpp extension to .zip and unzip it like normal. (compiled on WinXP using Borland compiler).
    Attached Files Attached Files
    Last edited by BongoBob; 05-14-2006 at 08:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numbers in Text Format
    By deb_cal2 in forum C Programming
    Replies: 14
    Last Post: 01-24-2008, 03:23 AM
  2. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 10:28 PM
  3. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 02:17 PM
  4. text input in graphics mode
    By jamie in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2003, 09:33 PM
  5. Replies: 1
    Last Post: 10-01-2001, 10:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21