-
validate userinput
Hello ppl
im trying to get user input using getline. i give this statement
while (true)
{
if (!getline(cin, string, '/'))
{
cout<<"invalid input"<<endl;
continue;
}
this program keeps on getting input until i give '/' in cin.
Question:
is there anyway if i dont give '/' and give anyother input and it should give me an error that it was a valid input(should have / at end)...
currently it doest give the error and keeps on getting input even if i hit enter unless i give it '/' delimitor.
-
>currently it doest give the error and keeps on getting input even if i hit enter unless i give it '/' delimitor.
It sounds as if you really want to read a line of input and then check that the line is terminated with a slash character:
Code:
string s;
while ( getline ( cin, s ) && s[s.length() - 1] != '/' ) {
cerr<<"Error: Input must end with '/'\n";
// Other stuff
}
-
thanx man it works....
wondering i couldnt think of it earlier.. perhaps new @ programming
thanx again