-
Check??Newbie..HELP!!
Hi,
Its a newbie Q!!please help........
I want to check if the integers entered are only numbers ,not strings/characters.
I know isdigit function but i suppose its in character-handled library,but I am using class string.So is there any function for checking that input values are only integers not character/strings/symbols etc;
thanks
-
-
thanks...but
Hi,
thanks for replying, but i assume, u gave me a soln in c and not vc++,
if,iam not wrong??I am unsure though.........:(
thankx
-
yes it is C but it works just as well in C++. the header file is conio.h
or at least it works on MY VC++, 4.0 that is
-
you could do it manually... personally, i dont use the string data tye cuz it takes away all your power (muwhahaha). anywayz, use a char array and check the contents:
Code:
int i;
char s[100];
cin.getline( s, '\n' );
for( i = -1; i != strlen( s ) + 1; i++ ){
//strlen checks the length of the string
if( s[i] != '1' && s[i] != '2' && s[i] != '3' ){
//your string has a symbol or whatever
//this just check 1, 2, and 3, but you get the point (i hope)
.
.
.
break; //breaks out of the for loop
}
}
remember to include string.h
im probably gonna receive some criticism for this post because some ppl might think the code "inefficient", but computers now are like 2ghz. welcome to reality folks!
also, scanf is stdio.h not conio.h
-
Yeah.. instead of using the string class, you post O(n^2) code for something that could trivially be O(N) :).
-
????
Hi,
I dont understand what SilentStrike is tryin to say????
Btw,thanks flikm and denethor2000
But as this an assignment, its expected to use a string and not char array.......also only c++ will be allowed for obvious reasons...
So can anyone please tell me how to check if the value entered is an integer and not strings/char/symbols???
thanks..........
-
Please help!
32 reads!!!still waiting!!!!!
-
>>32 reads!!!still waiting!!!!!
...meanwhile do a search on the board for integer input check... it might help
-
If you absoloutely have to have a string you can always typecast a character array into a string.
-
if I haven't misunderstood your request, here is a nifty little while loop that clears and ignores the information that was in the buffer and does exactly what you want...the only thing it will accept is numbers ;)
Code:
int num;
cout <<"pls enter num";
cin >> num;
while (cin.fail())
{
cin.clear();
cin.ignore (BUFFER, '\n');
cout << "\n pls re-enter number";
cin >> num;
}
NB: dont forget to create a BUFFER constant, that is :
const int BUFFER=128; before main.
sophie
-
thanks a bunch
Hi sophie,
it worked liked a charm......thanks a lot
but can u enlighten me about what buffer=128 ,cin.clear(),cin.fail(),cin.ignore() funcns do??????
thanks once again.
-
firstly, Im glad you found what you were after:D as for what they do.
the buffer constant you declared can be any size...I usually use 128...just like the sound of it:p the cin.fail() condition checks and returns a value, that is if the input was anything other than a number cin.fail() would return true (which is the condition for entering a loop).
the cin.clear() and cin.ignore() does exactly as they say...they ignore and clear the buffer thus cin >> num will have a clean and empty buffer to get info from.
sophie
-
ok!got it!!
thanks,
I got it!!!!!!!!!
-
another dumb Q
Hi,
is there any function like cin.fail() to check for the input value is only string and not numbers??????/
thanks..........
-
you can test for int's easily but char's and string's are a different story. you must not forget that a char can be anything that can be inputted via the keyboard....including numbers, symbols and characters.
if you are more specific with what you are after I might be able to help you...but as I said, I need to know exactly what it is you want to do.
ohh...just another thing that just clicked to me....you can do a something like this which will only continue until a valid char input has been made.
Code:
char ch;
do
{
cout << "pls select from the following " << endl;
cout << " a) add another word " << endl;
cout << " b) find word" << endl;
cout << " c) exit " << endl;
cin >> ch;
switch (ch)
{
case 'a' : addData(); //go to a fnt
break;
case 'b' : findData ();
break;
}
} while (ch != 'c');
hope this helps...
-
well...
My expected code:
Code:
string s1="end";
do {
cout << "Enter name:";
cin >> name;
if (name==s1) //this is working
break;
//while ((name doesnt has alphabets))
cout<<"error"
<<"ur string has a number in it"<<endl;
cout<<"please re-enter the name"<<endl;
cin>>name;//
this is my first doubt..
also there is a minor bug,which is buggin me ,as per ur previous code for checking ONLY int ,
Code:
cout << "Enter 1st time:";
cin >> a_time;
while (cin.fail())
{
cin.clear();
cin.ignore (buffer, '\n');
cout << "pls re-enter number";
cin >>a_time; //now for eg if i type 12A the output is "enter 2nd time", pls re-enter number" ie. how should i avoid getting the display message"enter 2nd time"??//
}
cout << "Enter 2nd time:";
cin >> b_time;
while (cin.fail())
{
cin.clear();
cin.ignore (buffer, '\n');
cout << "pls re-enter number";
cin >>b_time;
}
thanks ......
-
if the user was to input 132A your program will accept and only assign 132 to your a_time...if the user had inputted A123 then cin.fail() would return true and ask you to re-enter the value.
if the user was to input 12ABC34, then a_time would hold the value of 12 and ignore the rest.
as for your function with strings...firstly you cant have string s1="end" unless you pre-define it. dont forget a string is just an array of characters terminated with a '\0' character.
string.h library has a variety of string operations you can use. for example, instead of doing name==s1 (which I dont think should work unless everything is implemented properly) you can and should really get use to doing something like :
result=strcmp(str1, str2);
result will contain a -ve number if str1 < str2
result will contain a +ve number if str1 > str2
result will contain a zero if str1==str2
and then you can put in your conditions according to what result returns. you can find alot of information in regards to string handling if you look through a text book dealing with c++.
something I just remembered may also be useful....try something like this :
if (letter < 'a' || letter > 'z')
this may also help with what you want
sophie
-
No,it isnt!
Unfortunately sophie thats not the case,
if i type 13a /13A ,
its showing me the next cout statement; in my case
"enter 2nd TIME"//which is the time in seconds//
and next line its coming "please reenter number"
also checking of strings, i have predefined
string s1="end" ,before int main()
and its working nicely.
i have used find_first_of funcn,which checks that input is a string but i just dont know how to put it in a while loop,to ask the user to reenter the name
my present code:
Code:
in chk=0;
int main()
.
.
chk= name.find_first_of("1234567890");
if(chk!=-1)
{
cout<<"error!"
<<"ur string has a number in it";
}
thanks .......