|
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
|