C++ Getting program to deny a character

Status
Not open for further replies.

DJ-CHRIS

Golden Master
Messages
8,010
Okay so almost everytime I make a program that input's a number, if I type a letter it will mess up the program.

Is their a way to deny all leters and symbols except numbers?

Here is the source.


/* Twenty-One
Chris Mayhew */

#include <iostream.h>
#include <string.h>
#include <h:\lvp\random.h>

int main()
{
randomize();

int howmanytheywant;
int totalcomputer = 0;
int totalhuman = 0;
int valuecomputer;
int valuehuman;
int humanwin = 0;
int computerwin = 0;
int drawwin = 0;
char Answer;

do {
totalcomputer = 0;
totalhuman = 0;
cout << "How many cards do you want? ";
cin >> howmanytheywant;
if (howmanytheywant > 22) {
cout << "Please enter a lower number: ";
cin >> howmanytheywant;
}
cout << "You: ";
for (int cardamount=1; cardamount<=howmanytheywant; cardamount++) {
valuehuman = random(10)+1;
cout << valuehuman << " ";
totalhuman = valuehuman+totalhuman;
}
cout << endl;
cout << "Computer: ";
for (int computercard=1; computercard<=3; computercard++) {
valuecomputer = random(10)+1;
cout << valuecomputer << " ";
totalcomputer = valuecomputer+totalcomputer;
}
cout << endl;
cout << "I have " << totalcomputer << " and you have " << totalhuman;

if ((totalcomputer > totalhuman) && ((totalhuman <= 21) && (totalcomputer <= 21))) {
cout << " so I win";
computerwin++;
}

if ((totalcomputer < totalhuman) && ((totalhuman <= 21) && (totalcomputer <= 21))) {
cout << " so you win";
humanwin++;
}

if ((totalhuman >=22) && (totalcomputer <=21)) {
cout << " so I win";
computerwin++;
}
if ((totalcomputer >=22) && (totalhuman <=21)) {
cout << " so you win";
humanwin++;
}
if (totalcomputer == totalhuman) {
cout << " so we draw";
drawwin++;
}
cout << endl;
cout << "Would you like to play again (Y/N)?";
cin >> Answer;
cout << endl;

} while ((Answer == 'Y') || (Answer == 'y'));
cout << "Computer Wins = " << computerwin << endl;
cout << "Human Wins = " << humanwin << endl;
cout << "Draws = " << drawwin << endl;

return(0);
}
 
Well the advanced technique is called exception handling, so you could look that up on google as i don't feel like explaining it right now.
 
Thanks I will give it a shot looking it up

Not really into anything "advanced" as you can see
 
Try this:
Code:
/* Twenty-One
Chris Mayhew */

#include <iostream>
#include <string>
[b]#include <cctype>[/b]

#include <h:\lvp\random.h>

[b]using namespace std;[/b]

int main()
{
	randomize();

	int howmanytheywant;
	int totalcomputer = 0;
	int totalhuman = 0;
	int valuecomputer;
	int valuehuman;	
	int humanwin = 0;
	int computerwin = 0;
	int drawwin = 0;
	char Answer;
	[b]bool humandisqualified = false;
	bool computerdisqualified = false;[/b]

	do 
	{
		totalcomputer = 0;
		totalhuman = 0;

		[b]do
		{
			cout << "How many cards do you want? ";
			cin >> howmanytheywant;
				if ( !isdigit(howmanytheywant) )
				{
					cout << "You must enter one or more digits (0-9)\n";
				}
				else if (howmanytheywant > 22 || howmanytheywant < 0) 
				{
					cout << "Please enter a number between 0 and 21\n";
				}
		}while(howmanytheywant > 22 || howmanytheywant < 0);[/b]
		
		cout << "You: ";
		for (int cardamount=1; cardamount<=howmanytheywant; cardamount++) 
		{
			valuehuman = random(10)+1;
			cout << valuehuman << " ";
			totalhuman = valuehuman+totalhuman;
		}

		cout << endl;
		cout << "Computer: ";

		for (int computercard=1; computercard<=3; computercard++) 
		{
			valuecomputer = random(10)+1;
			cout << valuecomputer << " ";
			totalcomputer = valuecomputer+totalcomputer;
		}

		cout << endl;
		cout << "I have " << totalcomputer << " and you have " << totalhuman;

		[b]if ((totalhuman >=22) && (totalhuman < 0)) 
		{
			humandisqualified = true;
		}
		
		if ((totalcomputer >=22) && (totalcomputer < 0)) 
		{
			computerdisqualified = true;
		}[/b]
		
		if ((totalcomputer > totalhuman) && [b]!computerdisqualified[/b]) 
		{
			cout << " so I win";
			computerwin++;
		}
		[b]else[/b] if ((totalcomputer < totalhuman) && [b]!humandisqualified[/b]) 
		{
			cout << " so you win";
			humanwin++;
		}
		[b]else[/b] if (totalcomputer == totalhuman [b]|| (humandisqualified && computerdisqualified)[/b]) 
		{
			cout << " so we draw";
			drawwin++;
		}

		cout << endl;
		cout << "Would you like to play again (Y/N)?";
		cin >> Answer;
		cout << endl;
		[b]Answer = toupper(Answer);[/b]
		
	} while ((Answer == 'Y') || (Answer == 'y'));
	cout << "Computer Wins = " << computerwin << endl;
	cout << "Human Wins = " << humanwin << endl;
	cout << "Draws = " << drawwin << endl;

	return(0);
}

The parts in bold are what I changed. I changed some of it because you were making the same checks more than once, and others just because you don't need all these IF statments. If one of them turns out to be true, then you don't need to check the rest. So I used IF-ELSEIF statements.
I also used the isdigit() method and toupper() method. The isdigit returns true if the character passed to it is a digit, and false if it is not. toupper() just makes the character an upper case letter, it's just good practice to do that when checking user input.
 
Yeah Chris. Your conditional statements needed to be improved both in efficiency and accuracy. For example, in your original program, what if someone enters "100" and "100" twice. :)

In anycase, it's been a while since I've used isdigit, atoi, etc., but I am pretty sure isdigit(...) returns non-zero only when it's 0-9. If u enetered "15" for example, even though its a valid integer for your program, the behaviour of isdigit is probably to give zero. Try it out. "My" hypothesis is that it will interpret "15" as something out of range of 0-9 and will think it's one of the non-integer characters from the ascii table, and will give zero. After all, the input to isdigit is a single integer....

In anycase, to give you the most power so to speak, I suggest you take a look at the strtol(...) function - a variant of atoi with more error checking. It's part of the <stdlib.h>.

http://man.he.net/man3/strtol

You can read the input as a string, and try to pull out an integer from that. It will tell you "when" it is unable to continue processing the string to obtain a integer as well. It essentially goes character by character from beginning to end to try to construct an integer - if possible. And it will tell you when it can't.
 
Yeah I haven't used isdigit in forever....thanks for clearing that up :)

You know, just for fun you could change this:
Code:
if ((totalhuman >=22) && (totalhuman < 0)) 
{
	humandisqualified = true;
}
		
if ((totalcomputer >=22) && (totalcomputer < 0)) 
{
	computerdisqualified = true;
}
to this:
Code:
humandisqualified = ((totalhuman >= 22) && (totalhuman < 0)) ? true : false;
computerdisqualified = ((totalcomputer >= 22) && (totalcomptuer < 0)) ? true : false;
;) heh
 
Status
Not open for further replies.
Back
Top Bottom