Hai friends, I'm stuck with a input validation where if nothing is entered and enter is pressed, then i need to display a message saying please enter some value.

Recommended Answers

All 6 Replies

depends on whether its a string or numeric value. If you code it to always enter data as strings then you can easily test for empty string, then convert the string to some numeric value if needed.

Thanks for replying..
my code has to test for both strings and numeric values i.e. i have to test for every input in my program...
anyways please tell me how to do it....

>>anyways please tell me how to do it....
I thought I already did.

std::string input;
int value;
while(input == "")
{
    cout << "Enter something";
    cin >> input;
};
// now convert to integer
stringstream sstr(input);
sstr >> value;

Im sorry its not working....
its not displaying the message...
it is waiting for any input to be entered...

My sample code is

string ch;
cout<<"enter value ";
cin>>ch

now if we run it will display
enter value
and if i do not enter anything and press enter a message should display saying enter somevalue...

Thankyou for your reply

>>and if i do not enter anything and press enter a message should display saying enter somevalue

Put on your thinking cap. Don't you see where you can add that message to the code snippet I posted? Have you learned about if statements yet?

Ancent Dragon is right!

I tend to talk through the program, and where I say if the user does !!!! this is generally the best place to put the if statement.

If you are unsure how to use the if statement try using the help facility with the program you are using! its a good reference in itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.