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