5 Replies - 903 Views - Last Post: 13 June 2009 - 09:45 AM Rate Topic: -----

#1 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Help with isdigit?

Posted 13 June 2009 - 09:30 AM

This is my code:

	if (isdigit(valuex))
	  {
		  cout << "valuex is a number";
	  }
	 else
	  {
		  cout << "valuex is not a number";
	  }


When i compile it, and it is given a real digit value like "8", the second statement is executed. Am i using this wrong?
Is This A Good Question/Topic? 0
  • +

Replies To: Help with isdigit?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4194
  • View blog
  • Posts: 11,868
  • Joined: 18-April 07

Re: Help with isdigit?

Posted 13 June 2009 - 09:36 AM

How are you storing your value into valuex? What you are showing us there is correct so it has to be how you are setting up valuex. :)
Was This Post Helpful? 0
  • +
  • -

#3 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Help with isdigit?

Posted 13 June 2009 - 09:38 AM

This is my complete code:
#include <iostream>
#include <string>
#include "fubar.h"
#include <stdlib.h>
#include <ctype.h>


using namespace std;

int main()
{   /*** Starting Values ***/
	int x = 0;
	int y = 0;

	/*** Holds the values entered by the user ***/
	int valuex;
	int valuey;

	/*** Pointers to our values ***/
	int *Px = &x;
	int *Py = &y;

	cout << "Please enter value for width of a rectangle(x):\n";
	cin >> valuex;
	cout << "Please enter value for height of a rectangle(y):\n";
	cin >> valuey;

	if (isdigit(valuex))
	  {
		  cout << "valuex is a number";
	  }
	 else
	  {
		  cout << "valuex is not a number";
	  }

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4194
  • View blog
  • Posts: 11,868
  • Joined: 18-April 07

Re: Help with isdigit?

Posted 13 June 2009 - 09:43 AM

valuex has to be setup as a char, not an integer. Doesn't make sense to check if an integer is "a digit" because it stores numbers only.

Just to illustrate my point, change int valuex; to char valuex; and rerun the program with "8" as your input.

isdigit is for evaluating characters to see if they are character digits. :)
Was This Post Helpful? 0
  • +
  • -

#5 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Help with isdigit?

Posted 13 June 2009 - 09:43 AM

has to be string element not int. ANSII conversion takes place before comparison for isdigit(). int is not convertable

EDIT: or a char like char valuex = '8';

This post has been edited by Kanvus: 13 June 2009 - 09:43 AM

Was This Post Helpful? 1

#6 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Re: Help with isdigit?

Posted 13 June 2009 - 09:45 AM

View PostKanvus, on 13 Jun, 2009 - 08:43 AM, said:

has to be string element not int. ANSII conversion takes place before comparison for isdigit(). int is not convertable

EDIT: or a char like char valuex = '8';


I get it :D Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1