• 0

[C++] Not a number check


Question

hi all, i've completed this c++ project, except for one small tidbit. my program asks for a number, and i need to figure out how to check if it really is a number. for example, if the user enters f, the program has to say "not a valid input" or whatever, and ask for the number again. i know how to do everything except the number check. how do i check if something isn't a number or not? btw, my school uses the default C++ compiler that comes with Visual C++ 2008 express free. thanks!

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Couldn't you just do a quick Decimal/Ascii check? 0-9 have consecutive conversions, so anything outside those ranges will be a char and not an int, ect.

Link to comment
Share on other sites

  • 0
Couldn't you just do a quick Decimal/Ascii check? 0-9 have consecutive conversions, so anything outside those ranges will be a char and not an int, ect.

yeah but i don't exactly know how to do that yet. this is a beginner's class.

Link to comment
Share on other sites

  • 0

maybe this will help. here's the code for the program. all i need to do is have the program figure out if variable x is a number or not. if not, it needs to ask the user to enter the bet again, as a number, and keep asking until they enter a number. this all needs to be done directly after

if(x>score)

{cout<<"Invalid entry. Bet again: "; cin>>x;}

#include&lt;iostream&gt;
#include&lt;string&gt;
#include&lt;ctime&gt;
#include&lt;float.h&gt;
using namespace std;

//function
bool FindHiLo(int hilo, int choice)
{
	bool result;

	if(choice==hilo)
	{result=true;}

	else 
	{result=false;}

	return result;
}

int main()
{
	//variables
	srand((unsigned)time(0));
	int score=1000;
	int x;
	int choice;
	int randnum=0;
	int hilo=0;
	char play;
	bool result;

	//loop
	do{
	randnum=rand()%13;
	cout&lt;&lt;"You have "&lt;&lt;score&lt;&lt;" points."&lt;&lt;endl;
	cout&lt;&lt;"How many points would you like to bet?: "; cin&gt;&gt;x;

	if(x&gt;score)
	{cout&lt;&lt;"Invalid entry. Bet again: "; cin&gt;&gt;x;}

	cout&lt;&lt;"Predict. 1=High, 0=Low. : "; cin&gt;&gt;choice;
	cout&lt;&lt;"The number is "&lt;&lt;randnum&lt;&lt;"."&lt;&lt;endl;

	//hilo loop
	if(randnum==7)
	{hilo=3;}
	else if((randnum&gt;=1)&amp;&amp;(randnum&lt;7))
	{hilo=0;}
	else if((randnum&gt;7)&amp;&amp;(randnum&lt;14))
	{hilo=1;}

	result = FindHiLo(hilo, choice);

	if(result==true)
	{cout&lt;&lt;"You Won!"&lt;&lt;endl;
	score=score+(2*x);}

	else if(result==false)
	{cout&lt;&lt;"You lost."&lt;&lt;endl;
	score=(score-x);}

	cout&lt;&lt;"Play Again? (y/n):"; cin&gt;&gt;play;

	}while((play=='y')&amp;&amp;(score&gt;0));

	system("pause");
	return(0);
}

Link to comment
Share on other sites

  • 0

In C i know you can use the function

int isdigit(int c)

witch is included in ctype.h.

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/

In C++, a locale-specific template version of this function (isdigit) exists in header <locale>.

in c i would do something like

do
   {
	printf("invalid entry, bet again");
	scanf ("%d",&amp;x);
	}
while ( (!isdigit(x)) &amp;&amp; (x&gt;score) )

Hope it helps

Edited by Chester0
Link to comment
Share on other sites

  • 0

Timmah did you actually read the link I posted above? It's doing exactly what you need and it's written in C++. If there's parts you don't understand, ask questions. I'd be more than happy to help.

Link to comment
Share on other sites

  • 0

yeah i read all the posts, the problem was that x was set as an integer. it needed to be a string, so i can convert it to a double if it was indeed a number. if it was a letter or something else, it would ask for input again. i figured out how to do that and got some help from my teacher about the atoi command. she said it's a bit overboard to make all these error checks, but i want to make it unbreakable when she tests it. :x hehe

@ViZioN - that site actually helped me a lot, thanks. i learned a few new tricks in there.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.