Thread: [Dev-C++] Checking data type
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Exuss D. <dra...@ce...> - 2001-12-22 07:10:20
|
Hi, All! :)
How to check input data type from the console? For example, I need
only number, and how to check that some letters wasn't entered?
This code shows doesn't work for me, if I try to enter some number,
it shows a zero in every condition. ://
#include <stdlib.h>
#include <ctype.h>
#include <iostream.h>
void main()
{
int number;
cout << "\n\tInput number or letter: ";
cin >> number;
cout << "\n\tIs alnum?: " << isalnum(number) << endl;
cout << "\tIs digit?: " << isdigit(number) << endl;
cout << "\tIs alpha?: " << isalpha(number) << endl;
cout << "\tIs lower?: " << islower(number) << endl;
cout << endl;
system("PAUSE");
}
Are there other way to check what was entered from console, digital
number or text ???
Thanx,
Exuss :)
--
Nemokama el.pašto dėžutė - http://webmail.delfi.lt/
|
|
From: Exuss D. <dra...@ce...> - 2001-12-27 18:19:27
|
Hi,
Yes, this code above works, but only when ONE(!) simbol is entered.
If I try to enter "4kdkdkd", the output says that the digital number
was entered. I can't use that code in this prototype:
// int weight ??
// char weight ??
// string weight ??
// char[10] weight ??
do
{
cout << "Input Your weight:";
cin >> weight;
// Checking type of entered value
// because the program will crash, if value isn't digital number
// if value is not NUMBER cout << "Please, input number!";
} while ( /* value is bad */ )
// do some calculations with weight
Thanks,
Exuss
>Your code was not proper ANSI C++ too. Your code corrected and fully
>portable:
>
>
>#include <cstdlib>
>#include <cctype>
>#include <iostream>
>
>int main()
>{
> using namespace std;
>
> unsigned char number;
>
> cout << "\n\tInput number or letter: ";
> cin >> number;
>
> if(isalnum(number))
> {
> cout << "\n\tIt is alnum" <<endl;
>
> if(isdigit(number))
> cout << "\tIt is digit" <<endl;
>
> else if(isalpha(number))
> {
> cout << "\tIt is alpha" << endl;
>
> if(islower(number))
> cout << "\tIt is lower"<< endl;
> }
> }
>
>
> cout << endl;
>
> system("PAUSE");
>}
>
>
>
>Ioannis
>
>* Ioannis Vranos
>* Programming pages: http://www.noicys.f2s.com
>* Alternative URL: http://run.to/noicys
>
>
--
Nemokama el.pašto dėžutė - http://webmail.delfi.lt/
|
|
From: Jason H. <jas...@bt...> - 2001-12-28 01:25:57
|
Let's see if I can lay this problem to rest.
First the type you want to use to store the number depends if you need =
to allow for fault detection. If weight is an int then the conversion =
from the typed in text is automatic, however if your user doens't enter =
a valid number then an error will occur. Fortunately you can detect the =
error, i.e.
int value;
cout << "Enter a value: ";
cin >> value;
if(cin.fail())
{
// respond to failure here
}
This will detect if a non-number is typed, however if you type '50dgds' =
(or whatever) then the value 50 will be extracted and the remaining =
characters will be left in the stream (another cin for a string would =
get the 'dgds'). Remember that if the operation fails then the stream is =
left untouched (anything typed will still be there) and value is =
unchanged. Also the failure will need to be cleared to avoid it being =
redetected if your program continues i.e. cin.clear();
The alternative method is to get a string of some sort, either a C++ =
string or C style char array and then convert it yourself. Conversion =
isn't too hard cos you can use the atoi() function. The function atoi() =
will only convert until it reaches a character it can't convert so if =
you passed it the above string (50dgds) then you would still get 50, but =
if you want to have that as invalid then you can go through the string =
with something like this:
int size;
std::string value; // or char value[80];
// get your string here
size =3D value.length(); // or strlen(value);
for(int i =3D 0; i < size; ++i)
{
if(!isdigit(value[i]))
{
// non digit found do whatever i.e. set a flag
break; // or some other termination
}
}
I'll leave you to figure out the rest of that. E-mail again if you need =
help.
As for getting a number with a char, it's only useful for a single digit =
number. You could get a sequence of chars and total up the number, but a =
string would be easier.
Hope this helps, Jason.
----- Original Message -----=20
From: "Exuss Draugas" <dra...@ce...>
To: <Dev...@li...>
Sent: Thursday, December 27, 2001 7:20 PM
Subject: [Dev-C++] Checking data type
>=20
>=20
> Hi,=20
>=20
> Yes, this code above works, but only when ONE(!) simbol is entered.=20
> If I try to enter "4kdkdkd", the output says that the digital number=20
> was entered. I can't use that code in this prototype:=20
> =20
> // int weight ??
> // char weight ??
> // string weight ??
> // char[10] weight ??
>=20
> do
> {
> cout << "Input Your weight:";
> cin >> weight;
> // Checking type of entered value
> // because the program will crash, if value isn't digital number
> // if value is not NUMBER cout << "Please, input number!";
> } while ( /* value is bad */ )
> =20
> // do some calculations with weight
> =20
> Thanks,
>=20
> Exuss
> =20
> =20
> >Your code was not proper ANSI C++ too. Your code corrected and fully
> >portable:
> >
> >
> >#include <cstdlib>
> >#include <cctype>
> >#include <iostream>
> >
> >int main()
> >{
> > using namespace std;
> >
> > unsigned char number;
> >=20
> > cout << "\n\tInput number or letter: ";
> > cin >> number;
> >=20
> > if(isalnum(number))
> > {
> > cout << "\n\tIt is alnum" <<endl;
> > =20
> > if(isdigit(number))
> > cout << "\tIt is digit" <<endl;
> > =20
> > else if(isalpha(number))
> > {
> > cout << "\tIt is alpha" << endl;
> > =20
> > if(islower(number))
> > cout << "\tIt is lower"<< endl;
> > }
> > }
> > =20
> > =20
> > cout << endl;
> >
> > system("PAUSE");
> >}
> >
> >
> >
> >Ioannis
> >
> >* Ioannis Vranos
> >* Programming pages: http://www.noicys.f2s.com
> >* Alternative URL: http://run.to/noicys
> >
> >
>=20
>=20
> --
> Nemokama el.pa=F0to d=EB=FEut=EB - http://webmail.delfi.lt/
>=20
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Kevin W. <ke...@me...> - 2001-12-22 12:19:18
|
Change the type of " number " to char.
The output will probably still be not you want, so I would use e.g. :
cout << "\tIs digit?: ";
if(isdigit(number))
cout << "Yes" << endl;
else
cout << "No" << endl;
----- Original Message -----
From: Exuss Draugas <dra...@ce...>
To: <Dev...@li...>
Sent: Saturday, December 22, 2001 8:10 AM
Subject: [Dev-C++] Checking data type
>
>
> Hi, All! :)
>
> How to check input data type from the console? For example, I need
> only number, and how to check that some letters wasn't entered?
>
> This code shows doesn't work for me, if I try to enter some number,
> it shows a zero in every condition. ://
>
> #include <stdlib.h>
> #include <ctype.h>
> #include <iostream.h>
>
> void main()
> {
>
> int number;
> cout << "\n\tInput number or letter: ";
> cin >> number;
> cout << "\n\tIs alnum?: " << isalnum(number) << endl;
> cout << "\tIs digit?: " << isdigit(number) << endl;
> cout << "\tIs alpha?: " << isalpha(number) << endl;
> cout << "\tIs lower?: " << islower(number) << endl;
> cout << endl;
> system("PAUSE");
> }
>
> Are there other way to check what was entered from console, digital
> number or text ???
>
> Thanx,
>
> Exuss :)
>
|
|
From: Ioannis V. <no...@ya...> - 2001-12-22 15:48:59
|
Your code was not proper ANSI C++ too. Your code corrected and fully
portable:
#include <cstdlib>
#include <cctype>
#include <iostream>
int main()
{
using namespace std;
unsigned char number;
cout << "\n\tInput number or letter: ";
cin >> number;
if(isalnum(number))
{
cout << "\n\tIt is alnum" <<endl;
if(isdigit(number))
cout << "\tIt is digit" <<endl;
else if(isalpha(number))
{
cout << "\tIt is alpha" << endl;
if(islower(number))
cout << "\tIt is lower"<< endl;
}
}
cout << endl;
system("PAUSE");
}
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Exuss Draugas
> Sent: Saturday, December 22, 2001 10:11 AM
> To: Dev...@li...
> Subject: [Dev-C++] Checking data type
>
>
>
>
> Hi, All! :)
>
> How to check input data type from the console? For example, I need
> only number, and how to check that some letters wasn't entered?
>
> This code shows doesn't work for me, if I try to enter some number,
> it shows a zero in every condition. ://
>
> #include <stdlib.h>
> #include <ctype.h>
> #include <iostream.h>
>
> void main()
> {
>
> int number;
> cout << "\n\tInput number or letter: ";
> cin >> number;
> cout << "\n\tIs alnum?: " << isalnum(number) << endl;
> cout << "\tIs digit?: " << isdigit(number) << endl;
> cout << "\tIs alpha?: " << isalpha(number) << endl;
> cout << "\tIs lower?: " << islower(number) << endl;
> cout << endl;
> system("PAUSE");
> }
>
> Are there other way to check what was entered from console, digital
> number or text ???
>
> Thanx,
>
> Exuss :)
>
>
>
> --
> Nemokama el.pasto dezute - http://webmail.delfi.lt/
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|